调用weblogic连接池时遇到的XA异常

我的问题是:weblogic connection pool driver name :“oracle.jdbc.xa.client.OracleXADataSource”,调用这个连接池时。con.setAutoCommit(false),con.commit()报使用XA的错误。。解决方法如下。。。。

转自:http://anthonliu.blogbus.com/logs/514546.html

weblogic 分布式事务处理经常遇到的XA异常处理方法前提:用Weblogic XA类型的驱动程序,选中'SupportLocalTransaction'.
这些错误都默名奇妙,费了好大尽才找到答案.

错误1:
java.sql.SQLException: XA error: XAER_PROTO : Routine was invoked in an inproper context start() failed on resource 'InitialPool': XAER_PROTO : Routine was invoked in an inproper context
javax.transaction.xa.XAException
at oracle.jdbc.xa.OracleXAResource.disallowLocalTxnMode(OracleXAResource.java:1047)
at oracle.jdbc.xa.client.OracleXAResource.start(OracleXAResource.java:153)
at weblogic.jdbc.wrapper.VendorXAResource.start(VendorXAResource.java:50)
at weblogic.jdbc.jta.DataSource.start(DataSource.java:604)
at weblogic.transaction.internal.XAServerResourceInfo.sta

解决:
If the XA driver supports performing SQL operations with no global transaction, explicitly allow it by setting "SupportsLocalTransaction" JDBC connection pool property to true. In this case, also remember to complete the local transaction before using the connection again for global transaction, else a XAER_OUTSIDE XAException may result. To complete a local transaction, you can either set auto commit to true or call Connection.commit() or Connection.rollback().
也就是说,如果在同一个线程中,对于同一个连接池,先执行本地事务,接着取连接执行分布式事务要么明确用con.setAutoCommit(true),要么用con.setAutoCommit(false)/con.begin/con.rollback来完成本地事务。一般在前台页面开发时会遇到这样的问题:比如先通过InitialWebPublicPara来初始化页面,接着调用别人的EJB方法

最明显的是用下面的例子会出错:
java.sql.Connection con1 =
ConnectionManager.getConnection("BossInitDS");
//con1.setAutoCommit(true);
java.sql.Statement stmt = con1.createStatement();
stmt.executeQuery("select 1 from dual");
stmt.close();
con1.close();

//直接这样,或调用一个EJB方法(EJB方法中取连接)
javax.transaction.UserTransaction ut = weblogic.transaction.TxHelper.getUserTransaction();
ut.begin();
java.sql.Connection con =
ConnectionManager.getConnection("BossInitDS");
ut.commit();
con.close();

错误2:
错误a.ORA-01000: maximum open cursors exceeded
错误b.[java.sql.SQLException: XA error: XAER_RMERR : A resource manager error has occured in the transaction branch start() failed on resource 'weblogic.jdbc.jta.DataSource': XAER_RMERR : A resource manager error has occured in the transaction branch
oracle.jdbc.xa.OracleXAException

at oracle.jdbc.xa.OracleXAResource.checkError(OracleXAResource.java:1159)

at oracle.jdbc.xa.client.OracleXAResource.start(OracleXAResource.java:311)

at weblogic.jdbc.wrapper.VendorXAResource.start(VendorXAResource.java:50)

at weblogic.jdbc.jta.DataSource.start(DataSource.java:604)

at weblogic.transaction.internal.XAServerResourceInfo.start(XAServerResourceInfo.java:1069)

at weblogic.transaction.internal.XAServerResourceInfo.xaStart(XAServerResourceInfo.java:1001)

at weblogic.transaction.internal.XAServerResourceInfo.enlist(XAServerResourceInfo.java:203)

at weblogic.transaction.internal.ServerTransactionImpl.enlistResource(ServerTransactionImpl.java:419)


解决:
To prepare the database for XA, perform these steps:
1. Log on to sqlplus as system user, e.g.
sqlplus sys/CHANGE_ON_INSTALL@<DATABASE ALIAS NAME>
2. Execute the sql: grant select on DBA_PENDING_TRANSACTIONS to user where user is the user account used to create database connections in a connection pool. Execute this command for each user account used in XA connection pools in your WebLogic Server domain.
If the above steps are not performed on the database server, normal XA database queries and updates may work fine. However, when the Weblogic Server Transaction Manager performs recovery on a re-boot after a crash, recover for the Oracle resource will fail with XAER_RMERR. Crash recovery is a standard operation for an XA resource.

你可能感兴趣的:(java,oracle,weblogic,jdbc,SQL Server)