您的位置:首页 > 编程语言 > Java开发

java.sql.SQLException: Connection has already been created in this tx context for pool named ...

2013-08-23 13:19 726 查看
在EJB开发中碰到一个问题,如果在一个方法里同时使用了两个数据源:例如

DbZhUtil zhdb = DbZhUtil.getInstance();

DbYsUtil ysdb = DbYsUtil.getInstance();

调用该方法时会报这个错java.sql.SQLException: Connection has already been created in this tx context for pool named <first pool's name>. Illegal attempt to create connection from another pool: <second pool's name>" when you attempt to get the connection from the second
non-XA connection pool.

Q:Can I use more than one non-XA connection pool in distributed transactions?

A:No. Even if you set EnableTwoPhaseCommit=true for both TxDataSources of the connection pools, attempting to use two non-XA connection pools in the same distributed transaction will result in "java.sql.SQLException: ...."

分布式事务中不支持多个non-XA的数据源

解决办法有三个:

1. 使用XA connection Pool。在weblogic数据源配置时使用带XA的数据库驱动,但是XA因为要支持全局事务效率可能会不如不带XA的好。



2. 不使用全局事务。在weblogic数据源配置时仍配置non-XA的数据源,但在事物处理选项卡中将支持全局事物处理的选择勾掉,将不再支持多数据源的全局 (XA) 事务处理,只支持非全局 (本地) 事务处理。



3.指定bean或method不使用事务。在ejb-jar.xml中配置指定bean或method的
<assembly-descriptor>

<container-transaction>

<method>

<ejb-name>CpxxBean</ejb-name>

<method-intf>Remote</method-intf>

<method-name>getTjzcs</method-name>

</method>

<trans-attribute>NotSupported</trans-attribute>

</container-transaction>

</assembly-descriptor>
,或者在方法上注入
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public String getTjzcs(String khdh, String cpdm) {
}
NotSupported 这种模式下,组件方法不支持事务,同时也会忽略掉调用程序中的事务对象,组件方法中的逻辑不受客户程序事务的提交或回滚的影响。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐