Transactional on additional datastrore, but with active transaction on main datastore

Hi,
In my jmix project I have two datastores, and I’m trying to implement the following idea:

I woul like to react to a change in an entity X in the main datastore, performing several operation on entities of the additional datastore (insert, delete and update of different entities A B).
Those operations must be performed transactionally (I mean: if something fails, I would like to rollback all changes in A B)

I’ve ended up creating a listener on X, which call a service (annotated with @Transactional(“AdditionalDatastore”)) that perform the CRUDs on A and B.

I’ve also tried using programmatic transactions as shown in the documentation…
but either ways it always complains that the active transaction is from the main datastore

Am I doing it wrong, or there is no simple way to do it?

thank you

Hi Michelle,

This use case works for me.

I have Department entity in main data store and Foo in additional one called ads.

When Department is changed, I create an instance of Foo:

@Component
public class DepartmentEventListener {

    @Autowired
    private FooBean fooBean;
    @Autowired
    private DataManager dataManager;

    @EventListener
    public void onDepartmentChangedBeforeCommit(EntityChangedEvent<Department> event) {
        if (event.getType() == EntityChangedEvent.Type.UPDATED) {
            fooBean.updateFoo(dataManager.load(event.getEntityId()).one());
        }
    }
}
@Component
public class FooBean {

    @Autowired
    private DataManager dataManager;

    @Transactional("adsTransactionManager")
    public void updateFoo(Department department) {
        Foo foo = dataManager.create(Foo.class);
        foo.setName("Updated: " + department.getName() + " at " + LocalDateTime.now());
        dataManager.save(foo);
        // throw new RuntimeException("Test error");
    }
}

If I uncomment the exception, both changes in Department and Foo are rolled back.

Could you demonstrate your problem on a test project?