简体   繁体   English

Java EE中的事务混合(在bean受管方法内部称为容器受管bean方法)

[英]Transaction mixing in Java EE ( container-managed beans method called inside bean managed method )

couldn't find any kind of relevant information besides useless tutorials on the internet as well as in the specs. 除了互联网和规范中无用的教程外,找不到任何类型的相关信息。

There's one thing that I struggle with right now, if you can please help. 我目前正在努力解决的一件事,如果可以的话,请帮忙。

Here's the thing. 就是这个

lets say we have two EJB version 3.0 with annotations about their transaction type, one is bean managed ( let it be BeanManaged ) second is container managed ( let it be ContainerManaged, how creative ). 可以说我们有两个EJB 3.0版,其中带有关于它们的事务类型的注释,一个是Bean管理的(让它成为BeanManaged),第二个是容器管理的(让它成为ContainerManaged,多么有创意)。

Then this happens: 然后发生这种情况:

@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
    }
}

What is going on in the mechanics of it all, does the bean managed transaction becomes some kind of 'orphaned' container managed transaction? 这一切的机理是什么,bean管理的事务是否成为某种“孤立的”容器管理的事务? Do they mix? 他们混合吗? How do they interact if at all? 它们之间如何相互作用? Does one transaction separates from the other 一笔交易是否与另一笔交易分开

That's my deduction but, there's something more to that. 那是我的推论,但还有更多。

At the end when I try to commit the bean transaction it says " hello sir, this transaction is managed, and it is forbidden to commit it manually " with SQLException as desert. 最后,当我尝试提交bean事务时,它说“您好,先生,此事务已被管理,并且禁止手动提交”,SQLException为沙漠。

Then there's the thing with batches, that i collect somehow. 然后有一些东西,我以某种方式收集。 After 100 batches added I want to execute them, but only the last one is executed actually, seems like addBatch does not work at all. 添加100个批次后,我想执行它们,但实际上仅执行了最后一个,似乎addBatch根本不起作用。

Anybody can relate, or met something similar is welcomed, every wizard gets free cookies. 任何人都可以联系或遇到类似的问题,每个向导都会获得免费的cookie。

What TransactionAttribute do you have specified on the container-managed bean? 您在容器管理的Bean上指定了什么TransactionAttribute? I would suggest Mandatory. 我建议必须。 With Mandatory the container will not start transactions for you. 使用强制性,容器将不会为您启动交易。 Instead it will protect your bean by ensuring it cannot be called unless a transaction is in progress. 相反,它将通过确保除非正在进行事务才能调用它来保护您的bean。

This related question has some useful information as well. 这个相关问题也有一些有用的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM