XA交易应使用哪种连接类型

时间:2019-10-08 02:06:10

标签: spring-boot jdbc jboss transactionmanager

使用Arjuna(JBoss)TransactionManager并使用DriverManager.getConnection(...)获得连接时,在不同的线程中获得相同的连接并生成异常。

String dsJndiTestDb = TransactionalDriver.arjunaDriver + dataSourceName;
conn = DriverManager.getConnection(dsJndiTestDb, new Properties());

该日志显示该连接的类型为com.arjuna.ats.internal.jdbc.ConnectionImple@719fdd2f

但是,如果另一个线程并行运行,则会给出相同的连接,并且应用程序将引发异常。

Caused by: com.monoplus.mcd.exception.ApplicationException: ConnectionImple.registerDatabase - ARJUNA017017: enlist of resource failed
        at com.monoplus.mcd.sql.DbQueryRepository.sqlQueryForPreparedStatement(DbQueryRepository.java:182)
        at com.monoplus.mcd.sql.DbQueryRepository.executeSelectStatement(DbQueryRepository.java:77)
        ... 68 more
Caused by: java.sql.SQLException: ConnectionImple.registerDatabase - ARJUNA017017: enlist of resource failed
        at com.arjuna.ats.internal.jdbc.ConnectionImple.registerDatabase(ConnectionImple.java:961)
        at com.arjuna.ats.internal.jdbc.ConnectionImple.prepareStatement(ConnectionImple.java:173)
        at com.monoplus.mcd.sql.DbQueryRepository.sqlQueryForPreparedStatement(DbQueryRepository.java:144)
        ... 69 more

现在,如果我改为使用以下命令来建立连接:

XADataSource ds = InitialContext.doLookup("java:/comp/env/jdbc/" + dataSourceName);
conn = ds.getXAConnection();

线程从池中获得不同的连接,应用程序正确运行。但是,现在Oracle中连接的数据类型是

XA-CONNECTION: oracle.jdbc.driver.T4CXAConnection@db5cf3a
CONNECTION: oracle.jdbc.driver.LogicalConnection@2224555

和PostgreSQL

XA-CONNECTION: org.postgresql.xa.PGXAConnection@42872923
CONNECTION: Pooled connection wrapping physical connection org.postgresql.jdbc.PgConnection@6759de66

问题:

1-使用InitialContext.doLookup(...)获取连接是否正确?

2-如何使Arjuna为不同的线程返回不同的连接。这是我的连接池:

    <bean id="application" primary="true" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.arjuna.ats.jdbc.TransactionalDriver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/scuserapp" />
        <property name="username" value="scuserapp" />
        <property name="password" value="abc" />
        <property name="initialSize" value="10" />
        <property name="maxActive" value="20" />
        <property name="maxIdle" value="10" />
    </bean>

0 个答案:

没有答案