Java EE中的事务混合(容器管理的bean方法,在bean管理方法中调用)

时间:2015-04-17 08:21:48

标签: jdbc container-managed bean-managed-transactions

除了互联网上的无用教程以及规范之外,

找不到任何类型的相关信息。

如果你能帮忙的话,我现在就有一件事情在困难。

这就是事情。

假设我们有两个EJB版本3.0,其中包含有关其事务类型的注释,一个是bean托管(让它是BeanManaged),第二个是容器管理(让它成为ContainerManaged,有多么有创意)。

然后发生这种情况:

@TransactionBlahBlah(Type.BEAN)
class BeanManaged {

   @Inject
   private ContainerManaged contMngt; // here's the implicit container managed trnsactional bean ( not annotated or anything )

    void someMethod() {
         // some transaction creation and a bit of inserts and updates      
        contMngt.callingMethodThatIsGoingToCreateContainerManagedTransaction();

         // some batches that are inserts
        for( int y = 0 ; y < 100 ; y++ ) {
             for( int i = 100 ; i < 200 ; i ++ ) {
                    magicPreparedStatementOutOfNowhere.setParameter(666, "hell");
                    magicPreparedStatementOutOfNowhere.addBatch();
             }
             magicPreparedStatementOutOfNowhere.executeBatch();
        }
        transaction.commit(); // let's pretend it is not here
    }
}

这一切的机制是什么,bean管理的交易是否成为某种“孤立的”容器管理交易?他们混在一起吗?如果有的话,他们如何互动?一个事务是否与另一个事务分开

这是我的推论,但还有更多的东西。

最后,当我尝试提交bean事务时,它说“你好先生,管理这个事务,禁止手动提交它”,SQLException为沙漠。

然后有批次的东西,我以某种方式收集。在添加了100个批次之后,我想执行它们,但实际上只执行了最后一个,看起来像addBatch根本不起作用。

任何人都可以联系,或者遇到类似的事情,欢迎每个精灵获得免费的cookie。

1 个答案:

答案 0 :(得分:0)

您在容器管理的bean上指定了什么TransactionAttribute?我建议强制性。使用Mandatory时,容器不会为您启动事务。相反,它将通过确保无法调用来保护您的bean,除非事务正在进行中。

This related question也有一些有用的信息。

相关问题