1

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?

4

1 回答 1

1

如果你使用 CMT,你不应该自己搞乱交易。

我建议您为需要在事务中的操作创建一个方法,将其标记为REQUIRES_NEW,然后从循环中调用它。

每次调用该方法时,都会暂停当前事务(如果有),并为该操作启动一个新事务。

像这样的东西:

@EJB
SomeEJBLocal anotherme;

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

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

您必须注入另一个 EJB 实例并调用 single 以便容器处理事务方面。

于 2012-12-26T12:13:20.287 回答