I'm working on an application which starts a transaction, registers some resources, starts another transactions and performs processing based on resources registered by previous transaction. The example is:
Register:
@Stateless
@LocalBean
public class Register {
@Resource TransactionSynchronizationRegistry tsr;
public void registerResource(String id, Resource r) {
tsr.putResource(id, r);
}
}
First transaction
@Stateless
@LocalBean
public class FirstTransaction {
@Inject Register r;
@Inject SecondTransaction st;
public void doRegistering(Resource r) {
r.registerResource("key", r);
st.doProcess();
// do other operations...
}
}
Second transaction
@Stateless
@LocalBean
public class SecondTransaction {
@Resource TransactionSynchronizationRegistry tsr;
/*
* start new transaction in order to ensure that
* there won't be any rollback on any operations
* performed by this method if its caller fails
*/
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void doProcess() {
Resource r = (Resource) tsr.getResource("key");
// start processing resource...
}
}
But, as a new transaction is created after resource is registered, I cannot access to the same TransactionSynchronizationRegistry again. I know that TransactionSynchronizationRegistry is for one transaction only, so the question is if there is another method, e.g. resource registry, which I can use across different transaction.
Thanks.
L