Vanilla Spring批处理管理员和JTA-为什么提交会突然停止



我有一个围绕spring-batch和spring-batch-admim构建的小应用程序,它大部分都在工作,然后我遇到了一个尴尬的时刻,不得不添加XA事务处理。没什么大不了的,这是一个足够常见的模式。我一这样做,任何事务管理器上的提交都停止了。

spring批处理启动器执行其工作,日志显示工作已提交,但提交从未发生。

我通过spring代码追踪到JDBC驱动程序中。在调试器中,我已经确认提交方法一直被调用到驱动程序中,但在调用驱动程序的提交方法之前,连接协议状态已经处于空闲状态,因此没有执行任何提交。在beforeCommit触发器中,连接似乎在提交之前返回到池。

我已经确认,在org.springframework.jdbc.datasource.DataSourceTransactionManager和org.apache.commons.dbcp.BasicDataSource下,以及在BitronixTransactionManager和bitronix.tm.resource.jdbc.PoolingDataSource 下,都表现出相同的行为

我确信我遗漏了一些明显的东西,但我看不出是什么。如果这是正常的行为,那么产品就永远不会起作用。有人有什么想法吗?

驱动程序提交的伪代码

if (protocolstate != IDLE) {
    sendCommand(COMMIT);
}

环境:

OS: Windows 7
Java: 1.8.0_25
Spring: 3.2.13-RELEASE
Spring-Batch: 3.0.2-RELEASE
Spring-batch-admin: 1.3.1-RELEASE
JDBC: postgresql 9.3-1102-jdbc41
Database: postgresql 9.4

事务管理器配置(XML):

<tx:annotation-driven transaction-manager="jtaTransactionManager" />
<!--  Bitronix Transaction Manager embedded configuration -->
<bean id="btmConfig" factory-method="getConfiguration" class="bitronix.tm.TransactionManagerServices">
    <property name="serverId" value="spring-btm" />
    <property name="logPart1Filename" value="C:TEMPbit-btm1.tlog}" />
    <property name="logPart2Filename" value="C:TEMPbit-btm2.tlog}" />
</bean>
<!-- create BTM transaction manager -->
<bean id="BitronixTransactionManager" factory-method="getTransactionManager"
    class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig" destroy-method="shutdown" />
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"
    depends-on="BitronixTransactionManager">
    <property name="transactionManager" ref="BitronixTransactionManager" />
    <property name="userTransaction" ref="BitronixTransactionManager" />
</bean>  
<alias alias="transactionManager" name="jtaTransactionManager"/>
<alias alias="dataSource" name="myDataSource"/>
<bean id="myDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource"
    init-method="init" destroy-method="close" depends-on="jtaTransactionManager">
    <property name="className" value="org.postgresql.xa.PGXADataSource" />
    <property name="uniqueName" value="maxisIlmDataSource" />
    <property name="maxPoolSize" value="30" />
    <property name="allowLocalTransactions" value="true"/>
    <property name="driverProperties">
        <props>
            <prop key="user">me</prop>
            <prop key="password">mypass</prop>
            <prop key="url">jdbc:postgresql://localhost:5432/mydb</prop>
            <prop key="serverName">localhost</prop>
            <prop key="portNumber">5432</prop>
            <prop key="databaseName">mydb</prop>
        </props>
    </property>
</bean>

执行日志摘录:

21:48:45.929 [tomcat-http--10] DEBUG b.tm.BitronixTransactionManager - begun new transaction at Wed Dec 31 22:50:41 CST 1969
21:48:45.929 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
21:48:45.929 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]
21:48:45.929 [tomcat-http--10] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
21:48:45.929 [tomcat-http--10] DEBUG o.s.jdbc.datasource.DataSourceUtils - Registering transaction synchronization for JDBC Connection
21:48:45.930 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
21:48:45.930 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]
21:48:45.933 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update
21:48:45.935 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [INSERT into BATCH_JOB_INSTANCE(JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (?, ?, ?, ?)]
21:48:45.938 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - SQL update affected 1 rows
21:48:45.940 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update
21:48:45.941 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [INSERT into BATCH_JOB_EXECUTION(JOB_EXECUTION_ID, JOB_INSTANCE_ID, START_TIME, END_TIME, STATUS, EXIT_CODE, EXIT_MESSAGE, VERSION, CREATE_TIME, LAST_UPDATED, JOB_CONFIGURATION_LOCATION) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]
21:48:45.942 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - SQL update affected 1 rows
21:48:45.977 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update
21:48:45.977 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [INSERT INTO BATCH_JOB_EXECUTION_CONTEXT (SHORT_CONTEXT, SERIALIZED_CONTEXT, JOB_EXECUTION_ID) VALUES(?, ?, ?)]
21:48:45.980 [tomcat-http--10] DEBUG o.s.jdbc.core.JdbcTemplate - SQL update affected 1 rows
21:48:45.980 [tomcat-http--10] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource
21:48:45.980 [tomcat-http--10] DEBUG b.tm.BitronixTransactionManager - committing transaction a Bitronix Transaction with GTRID [737072696E672D62746D00000000010A22560000000F], status=ACTIVE, 0 resource(s) enlisted (started Wed Dec 31 22:50:41 CST 1969)
21:48:45.980 [tomcat-http--10] DEBUG bitronix.tm.timer.TaskScheduler - cancelling transaction timeout task on a Bitronix Transaction with GTRID [737072696E672D62746D00000000010A22560000000F], status=ACTIVE, 0 resource(s) enlisted (started Wed Dec 31 22:50:41 CST 1969)
...
21:48:45.981 [tomcat-http--10] DEBUG b.tm.journal.TransactionLogAppender - between 52346 and 52405, writing a Bitronix TransactionLogRecord with status=COMMITTED, recordLength=51, headerLength=28, time=17441418, sequenceNumber=45, crc32=-367977821, gtrid=737072696E672D62746D00000000010A22560000000F, uniqueNames=
21:48:45.981 [tomcat-http--10] DEBUG b.tm.journal.TransactionLogAppender - disk journal appender now at position 52405
21:48:45.981 [tomcat-http--10] DEBUG bitronix.tm.BitronixTransaction - transaction status is changing from COMMITTING to COMMITTED - executing 0 listener(s)
21:48:45.981 [tomcat-http--10] DEBUG bitronix.tm.twopc.Committer - phase 2 commit succeeded with no interested resource
21:48:45.981 [tomcat-http--10] DEBUG bitronix.tm.BitronixTransaction - successfully committed a Bitronix Transaction with GTRID [737072696E672D62746D00000000010A22560000000F], status=COMMITTED, 0 resource(s) enlisted (started Wed Dec 31 22:50:41 CST 1969)

到底出了什么问题在另一个配置文件中,有一个BasicDataSource类的bean。一些实例化的bean正在获取这个数据源,而不是事务管理器中的数据源。

我是如何解决的

步骤1:一夜好眠

步骤2:更多的咖啡因

步骤3:我通过跟踪初始代码并确定正在创建的JDBCTemplate附加到错误类型的DataSource来跟踪这一点。我删除了这个库,然后Spring抱怨它无法初始化指向错误数据源的bean。

我用正确的类重新定义了那些数据源bean,一切都恢复了正常。

相关内容

  • 没有找到相关文章

最新更新