2

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

Long Thai
  • 807
  • 3
  • 12
  • 34
  • Are you sure you are able to view resources associated with Tx1 if Tx1 is suspended and Tx2 is running? I don't think JTA / Java EE supports nested transactions. – Piotr Nowicki Oct 17 '12 at 08:13
  • 1st transaction isn't suspended as it'll do other operation after calling 2nd transaction, I update the question to reflect it. Thanks. – Long Thai Oct 17 '12 at 08:18
  • Doesn't what you said mean that the 1st Tx is suspended? Tx1 starts (default: REQUIRED), associates the resource with it; Tx2 starts (REQUIRES_NEW) - at this point Tx1 is suspended until Tx2 finishes. Resources visible for Tx1 are visible only to Tx1. Tx2 finishes, Tx1 resumes. – Piotr Nowicki Oct 17 '12 at 08:34
  • Sorry, I mistook between "suspend" and "terminate". You're right, 1st transaction is suspended and 2nd transaction isn't able to view resource from it. And I'm looking for an alternative solution so that resource from 1st transaction can be shared to 2nd transaction. – Long Thai Oct 17 '12 at 08:40
  • Ok, so we agreed on this one. I **think** this is what's called nested transactions. These are not supported by the Java EE transaction manager implementing JTA. Unfortunately, I am not aware of any workarounds for your issue. – Piotr Nowicki Oct 17 '12 at 09:53
  • It is not a nested transaction because 1st transaction is suspended, according to this: http://stackoverflow.com/questions/10817838/ejb-3-0-nested-transaction-requires-new – Long Thai Oct 17 '12 at 10:14
  • I was not referring to the actual state. I was referring to the state you want to achieve (Tx2 be able to see Tx1-only resources) I think **this** is a nested transaction functionality. Would be nice if someone could agree or disagree upon that. – Piotr Nowicki Oct 17 '12 at 10:36

1 Answers1

0

It's quite a bit of overhead but you could have them produce/consume payloads from JMS message queues.

d33j
  • 434
  • 3
  • 9
  • Hi, do you mean that 1st transaction should send a message, containing resource, to 2nd transaction? I don't really want to do that as 2nd transaction can be reused somewhere else, so I prefer it to be as generic as a normal method call as possible. – Long Thai Oct 17 '12 at 09:47