mysql版本需要5.0以上,5.0才会支持XA Transaction.
connector/J版本需要5.1.15以上
把jbossall-client.jar, jboss-logging-spi.jar,jnp-client.jar放到classpath下
1.创建mysql-xa-ds.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: mysql-ds.xml 41017 2006-02-07 14:26:14Z acoliver $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<xa-datasource>
<jndi-name>EjbTestDS</jndi-name>
<xa-datasource-property name="Url">jdbc:mysql://127.0.0.1:3306/ejbtest</xa-datasource-property>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
<user-name>ejbtest</user-name>
<password>123</password>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<max-pool-size>5</max-pool-size>
<min-pool-size>1</min-pool-size>
<blocking-timeout-millis>2000</blocking-timeout-millis>
<idle-timeout-minutes>2</idle-timeout-minutes>
<track-connection-by-tx>true</track-connection-by-tx>
<no-tx-separate-pools>true</no-tx-separate-pools>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</xa-datasource>
<xa-datasource>
<jndi-name>TestPbookDS</jndi-name>
<use-java-context>false</use-java-context>
<xa-datasource-property name="Url">jdbc:mysql://127.0.0.1:3306/testpbook</xa-datasource-property>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
<user-name>testadm</user-name>
<password>testadm</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- should only be used on drivers after 3.22.1 with "ping" support
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
-->
<!-- sql to call when connection is created
<new-connection-sql>some arbitrary sql</new-connection-sql>
-->
<!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</xa-datasource>
</datasources>
2. 我使用的是EJB,在api里用JDBC对不同的数据库进行操作,代码如下:
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void testXATransaction() throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering testXATransactoin!");
Connection ejbTestConn = JDBCUtil.getEjbTestConnection();; //此处请注意,如果get connection的代码写在这里会有exception
Connection testPbookConn = JDBCUtil.getTestPbookConnection(); //出现
UserTransaction userTx = getUserTransaction(0);
try {
userTx.begin();
updateGreeting(ejbTestConn, "andy");
updateMaxContact(testPbookConn, 89);
userTx.commit();
} catch (Exception e) {
log.error("testXATransactoin failed", e);
userTx.rollback();
} finally {
if (ejbTestConn != null) {
ejbTestConn.close();
if (testPbookConn != null) {
testPbookConn.close();
private void updateMaxContact(Connection conn, int maxContact) throws SQLException {
String sql = "update testpbook_svccfg set maxContact=? where svcName='test'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, maxContact);
int result = ps.executeUpdate();
if (log.isDebugEnabled()) {
log.debug("updateMaxContact result=" + result);
}
}
private void updateGreeting(Connection conn, String greeting) throws SQLException {
String sql = "update greeting set message=? where id=1";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, greeting);
int result = ps.executeUpdate();
if (log.isDebugEnabled()) {
log.debug("updateGreeting result=" + result);
}
}
public class JDBCUtil {
public static Connection getTestPbookConnection() {
Connection conn = null;
try {
InitialContext context = new InitialContext();
DataSource ds = (DataSource)context.lookup("TestPbookDS");
conn = ds.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Connection getEjbTestConnection() {
Connection conn = null;
try {
InitialContext context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:/EjbTestDS");
conn = ds.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
3. Client端代码如下:
public class Test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InitialContext ctx = new InitialContext();
// EjbTestProxy proxy = (EjbTestProxy) ctx.lookup("ejbtest");
EjbTestProxy proxy = (EjbTestProxy) ctx.lookup("com.andy.proxy.EjbTestProxy/remote-com.andy.proxy.EjbTestProxy");
// List<GreetingEbo> greets = proxy.getAllGreetings();
// for (GreetingEbo greeting : greets) {
// System.out.println(greeting.getGreeting());
// }
proxy.testXATransaction();
// proxy.testJpa();
// proxy.greet("Andy");
System.out.println("Done");
测试后发现有如下MysqlXAException
[02/19 02:31:42,743 DEBUG WorkerThread#0[127.0.0.1:2695] org.jboss.slf4j.JBossLoggerAdapter:104] opening JDBC connection
[02/19 02:31:42,759 WARN WorkerThread#0[127.0.0.1:2695] com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple:895] [com.arjuna.ats.internal.jta.transaction.arjunacore.xastart] [com.arjuna.ats.internal.jta.transaction.arjunacore.xastart] TransactionImple.enlistResource - xa_start - caught: XAException.XAER_INVAL for < 131075, 27, 25, 494545511025355102101579858975649585210251102101101551015854534551102535510210157985897564958521025110210110155101585455 >
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] com.mysql.jdbc.jdbc2.optional.MysqlXAException: XAER_INVAL: Invalid arguments (or unsupported command)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at com.mysql.jdbc.jdbc2.optional.MysqlXAConnection.mapXAExceptionFromSQLException(MysqlXAConnection.java:601)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at com.mysql.jdbc.jdbc2.optional.MysqlXAConnection.dispatchCommand(MysqlXAConnection.java:584)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at com.mysql.jdbc.jdbc2.optional.MysqlXAConnection.start(MysqlXAConnection.java:524)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.adapter.jdbc.xa.XAManagedConnection.start(XAManagedConnection.java:213)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.enlistResource(TransactionImple.java:889)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.enlistResource(TransactionImple.java:500)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.connectionmanager.TxConnectionManager$TxConnectionEventListener$TransactionSynchronization.enlist(TxConnectionManager.java:908)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.connectionmanager.TxConnectionManager$TxConnectionEventListener.enlist(TxConnectionManager.java:662)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.connectionmanager.TxConnectionManager.managedConnectionReconnected(TxConnectionManager.java:403)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.connectionmanager.BaseConnectionManager2.reconnectManagedConnection(BaseConnectionManager2.java:625)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:499)
[02/19 02:31:42,759 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:941)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:89)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:46)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.prepare(IdentityGenerator.java:90)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:54)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.jpa.tx.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:187)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at com.andy.proxy.EjbTestProxyBean.testJpa(EjbTestProxyBean.java:91)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at java.lang.reflect.Method.invoke(Method.java:597)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
[02/19 02:31:42,774 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at java.lang.reflect.Method.invoke(Method.java:597)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_32900144.invoke(InvocationContextInterceptor_z_fillMethod_32900144.java)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_32900144.invoke(InvocationContextInterceptor_z_setup_32900144.java)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:114)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:159)
[02/19 02:31:42,790 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:65)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:486)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.ejb3.session.InvokableContextClassProxyHack._dynamicInvoke(InvokableContextClassProxyHack.java:56)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:91)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:908)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.remoting.transport.socket.ServerThread.completeInvocation(ServerThread.java:742)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:695)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:522)
[02/19 02:31:42,805 ERROR WorkerThread#0[127.0.0.1:2695] org.jboss.logging.util.LoggerStream:156] at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:230)
[02/19 02:31:42,805 DEBUG WorkerThread#0[127.0.0.1:2695] org.jboss.util.NestedThrowable$Util:139] org.jboss.util.NestedThrowable.parentTraceEnabled=true
[02/19 02:31:42,805 DEBUG WorkerThread#0[127.0.0.1:2695] org.jboss.util.NestedThrowable$Util:139] org.jboss.util.NestedThrowable.nestedTraceEnabled=false
[02/19 02:31:42,805 DEBUG WorkerThread#0[127.0.0.1:2695] org.jboss.util.NestedThrowable$Util:139] org.jboss.util.NestedThrowable.detectDuplicateNesting=true
[02/19 02:31:42,821 DEBUG WorkerThread#0[127.0.0.1:2695] org.jboss.slf4j.JBossLoggerAdapter:136] Cannot open connection [???]
org.jboss.util.NestedSQLException: Could not enlist in transaction on entering meta-aware object!; - nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: -3f57fe9b:a81:4f3fee7e:65 status: ActionStatus.ABORT_ONLY >); - nested throwable: (org.jboss.resource.JBossResourceException: Could not enlist in transaction on entering meta-aware object!; - nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: -3f57fe9b:a81:4f3fee7e:65 status: ActionStatus.ABORT_ONLY >))
at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:95)
at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:46)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.prepare(IdentityGenerator.java:90)
经过几天的检查试验后,最终问题解决了,原来是代码有误,最终得出如下结论:
在JTA中,所有参与的资源(这里以数据库为类,还有其它一些支持事务的资源,如JMS、MQ等)的connection都要在事务内部(start后,commit前)获取。所以我ejb api的方法应该改成如下(注意标颜色部分的代码与之前有什么不同):
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void testXATransaction() throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering testXATransactoin!");
Connection ejbTestConn = null;
Connection testPbookConn = null;
UserTransaction userTx = getUserTransaction(0);
try {
userTx.begin();
ejbTestConn = JDBCUtil.getEjbTestConnection();
testPbookConn = JDBCUtil.getTestPbookConnection();
updateGreeting(ejbTestConn, "andy");
updateMaxContact(testPbookConn, 89);
userTx.commit();
} catch (Exception e) {
log.error("testXATransactoin failed", e);
userTx.rollback();
} finally {
if (ejbTestConn != null) {
ejbTestConn.close();
if (testPbookConn != null) {
testPbookConn.close();
--转自