我有一个围绕 spring-batch 和 spring-batch-admim 构建的小应用程序,它大部分都在工作,然后我有一个面部表情,不得不向它添加 XA 事务。没什么大不了的,这是一个足够常见的模式。一旦我这样做了,提交就停止在任何事务管理器上发生。
spring-batch 启动器执行它的工作,日志说工作已提交,但提交从未发生。
我通过 spring 代码跟踪到 JDBC 驱动程序。在调试器中,我已经确认commit方法一直被调用到驱动程序中,但是在调用驱动程序的commit方法之前连接协议状态已经处于空闲状态,所以没有执行提交。在 beforeCommit 触发器中,连接似乎在提交之前返回到池中。
我已经确认在带有 org.apache.commons.dbcp.BasicDataSource 的 org.springframework.jdbc.datasource.DataSourceTransactionManager 和带有 bitronix.tm.resource.jdbc.PoolingDataSource 的 BitronixTransactionManager 下都表现出相同的行为
我确信我遗漏了一些明显的东西,但我看不出它是什么。如果这是正常行为,那么该产品将永远无法工作。有没有人有任何想法?
驱动程序提交的伪代码
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:\TEMP\bit-btm1.tlog}" />
<property name="logPart2Filename" value="C:\TEMP\bit-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)