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

     在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  这种模式下,组件方法不支持事务,同时也会忽略掉调用程序中的事务对象,组件方法中的逻辑不受客户程序事务的提交或回滚的影响。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值