我有 2 个数据源,在一个方法中,我需要从 2 个数据库中写入和读取。数据库是 PostgreSQL,我的 EAR 应用程序在 wildfly-16 上运行。
我无法以同一方法使用 2 个数据库连接
好的,我知道,因为系统无法管理跨不同数据库的事务。
所以我拆分了 ejb 中的方法:
@TransactionAttribute(TransactionAttributeType.NEVER)
public Response mainMethod(String parameter) {
method1();
method2()
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void method1(){
...write on database 1...
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void method2(){
...write on database 2...
}
为什么我仍然遇到同样的错误?调用不是由两个不同的事务管理吗?
ARJUNA012140: Adding multiple last resources is disallowed.
Trying to add LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@594d9ba8[connectionListener=677e78 connectionManager=2a095926 warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffffc0a86e69:-9dc6f57:5d08b51e:f58, node_name=1, branch_uid=0:ffffc0a86e69:-9dc6f57:5d08b51e:f8a, subordinatenodename=null, eis_name=java:/jboss/datasource/db1 > productName=PostgreSQL productVersion=10.8 (Ubuntu 10.8-0ubuntu0.18.04.1) jndiName=java:/jboss/datasource/db1])),
but already have LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@39fc2dc2[connectionListener=3724f31c connectionManager=39a995fb warned=false currentXid=< formatId=131077, gtrid_length=29, bqual_length=36, tx_uid=0:ffffc0a86e69:-9dc6f57:5d08b51e:f58, node_name=1, branch_uid=0:ffffc0a86e69:-9dc6f57:5d08b51e:f64, subordinatenodename=null, eis_name=java:/jboss/datasource/db2 > productName=PostgreSQL productVersion=10.8 (Ubuntu 10.8-0ubuntu0.18.04.1) jndiName=java:/jboss/datasource/db2]))