java 即使没有事务,在 jboss 中检索到多个数据库的连接也出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12290285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:19:25  来源:igfitidea点击:

Error retrieving connections in jboss to multiple databases even though there is no transaction

javadatabase-connectionjboss7.x

提问by Yishai

We are in the process of upgrading from JBoss 6 to JBoss 7. The nature of our application has a separate database instance per customer, as well as a core configuration database common to all customers. We often have EJB code which will at least reference the core database plus the individual customer's data in one call, and even some general background processes that go through all customers.

我们正在从 JBoss 6 升级到 JBoss 7。我们的应用程序的本质是每个客户都有一个单独的数据库实例,以及一个所有客户通用的核心配置数据库。我们经常有 EJB 代码,它至少会在一次调用中引用核心数据库和单个客户的数据,甚至一些通用的后台进程会贯穿所有客户。

In JBoss 6 this was handled without issue by setting the method with a NOT_SUPPORTED transaction attribute. However, JBoss 7 complains about this with the following error:

在 JBoss 6 中,通过使用 NOT_SUPPORTED 事务属性设置方法,可以毫无问题地处理此问题。但是,JBoss 7 对此提出了以下错误:

ARJUNA012140: Adding multiple last resources is disallowed. Trying to add LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@74bec54d[connectionListener=d3ce980 connectionManager=25b47a05 warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffff0a2c28d1:-5a4c1f9a:504689c9:11, node_name=1, branch_uid=0:ffff0a2c28d1:-5a4c1f9a:504689c9:14, subordinatenodename=null, eis_name=unknown eis name >])), but already have LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@518d0191[connectionListener=1a05d94a connectionManager=135f1cfe warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffff0a2c28d1:-5a4c1f9a:504689c9:11, node_name=1, branch_uid=0:ffff0a2c28d1:-5a4c1f9a:504689c9:13, subordinatenodename=null, eis_name=unknown eis name >]))

ARJUNA012140:不允许添加多个最后的资源。尝试添加 LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@74bec54d[connectionListener=d3ce980 connectionManager=25b47a05 warn=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:295ac5c5ac18c18f10c5c18c18c18c18c18c18c18c18c18c18c50c18c100000000000000000000000) 1、branch_uid=0:ffff0a2c28d1:-5a4c1f9a:504689c9:14,submissionnodename=null,eis_name=unknown eis name >])),但是已经有了LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@518d0191f9a:504689c9:14) =< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffff0a2c28d1:-5a4c1f9a:504689c9:11, node_name=1, branch_uid=0:ffff0a2c28d1:-98a95e1:-5a95c0name=0未知 eis 名称 >]))

How can we work around this problem without wrapping every call to the different databases in a separate EJB call and transaction. Is there a way to actually turn off the JBoss transaction management in an EJB call or something similar?

我们如何解决这个问题,而无需将每个对不同数据库的调用都包装在单独的 EJB 调用和事务中。有没有办法在 EJB 调用或类似的东西中实际关闭 JBoss 事务管理?

Note: This question was modified after the bounty started based on additional discovery to sharpen the focus on the specific problem and remove other possibilities that were excluded.

注意:这个问题在赏金开始后根据额外的发现进行了修改,以突出对特定问题的关注并删除被排除的其他可能性。

采纳答案by Yishai

OK, it turns out that unlike JBoss6, a NOT_SUPPORTED transaction is still a transaction as far as the validation logic for retrieving data sources is concerned.

好吧,事实证明,与 JBoss6 不同的是,就检索数据源的验证逻辑而言,NOT_SUPPORTED 事务仍然是一个事务。

The way to work around this is to make the whole EJB a bean managed transaction:

解决这个问题的方法是使整个 EJB 成为 bean 管理的事务:

@TransactionManagement(TransactionManagementType.BEAN)

This unfortunately limits some flexibility in that some times you would rather control this method-by-method, but it isn't too painful a workaround.

不幸的是,这限制了一些灵活性,因为有时您宁愿逐个方法地控制这种方法,但它并不是一种很痛苦的解决方法。

回答by Alexander Torstling

Two suggestions:

两个建议:

  1. Consider updating the datasources to their XA equivalents. This will solve all your problems. I suspect you are hindered here?
  2. Consider setting com.arjuna.ats.arjuna.allowMultipleLastResourcesto true in the server conf. This will allow the behaviour you want, but unfortunately for the whole app, not just the method.
  1. 考虑将数据源更新为其 XA 等效项。这将解决您的所有问题。我怀疑你在这里受阻?
  2. 考虑com.arjuna.ats.arjuna.allowMultipleLastResources在服务器 conf 中设置为 true。这将允许您想要的行为,但不幸的是对于整个应用程序,而不仅仅是方法。

Update:

更新:

I don't recommend enabling multiple one-phase resources since it weakens the transactional properties of you app substantially. But if you want to do this in JBoss 7 you need to modify standalone.xmland add:

我不建议启用多个单阶段资源,因为它会大大削弱应用程序的事务属性。但是如果你想在 JBoss 7 中做到这一点,你需要修改standalone.xml并添加:

<system-properties>
        <property name="com.arjuna.ats.arjuna.allowMultipleLastResources" value="true"/>
</system-properties>

You now have a system which is not far away from one without transactions. What it will still do though, is to warn you if you get heuristic outcomes.

您现在拥有一个与没有交易的系统相距不远的系统。不过,它仍然会做的是,如果您得到启发式结果,就会警告您。

My recommendation is still to use XA datasources if you can.

如果可以,我的建议仍然是使用 XA 数据源。

Update 2:

更新 2:

Oh, and if someone comes along to read this I want to add that if you can divide your code into different methods, unlike the OP, I would recommend restructuring your code and use @TransactionAttribute(REQUIRES_NEW)to create parallel transactions. This is better than turning on multiple 1PC, albeit not as good as turning on XA.

哦,如果有人来阅读本文,我想补充一点,如果您可以将代码划分为不同的方法,与 OP 不同,我建议您重构代码并用于@TransactionAttribute(REQUIRES_NEW)创建并行事务。这比打开多个 1PC 好,尽管不如打开 XA。