michael nesterenko
michael nesterenko

Reputation: 14439

using programmatic transaction control in CMT bean

I need to commit transactions from CMT bean by hand. There is a loop which processes multiple records and each record should be processed in its own transaction. I wanted to mark method transaction support as NOT_SUPPORTED and then control transaction from method. However I could not retrieve a UserTransaction instance neither from SessionContext neither injecting it as a JNDI resource java:/module/UserTransaction.

Are there any chance to process multiple records in CMT bean in their own transactions without introducing new BMT bean for such processing?

Upvotes: 1

Views: 760

Answers (1)

Aviram Segal
Aviram Segal

Reputation: 11120

You should not mess around transactions yourself if you use CMT.

I recommend you create a method for the operation needs to be in transaction, mark it as REQUIRES_NEW, then call it from the loop.

Everytime the method is called, the current transaction (if any) will be suspended and a new transaction will be started for the operation.

Something like this:

@EJB
SomeEJBLocal anotherme;

public void loop() {
    for(/* something */) {
        anotherme.single();
    }
}

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void single() {
    // do stuff
}

You will have to inject another instance of the EJB and call single in order for the container to process the transaction aspects.

Upvotes: 1

Related Questions