Vanilla Spring Batch Admin和JTA - 为什么承诺突然停止发生?

时间:2015-02-05 05:01:57

标签: postgresql spring-batch spring-jdbc spring-batch-admin bitronix

我有一个小应用程序围绕spring-batch和spring-batch-admim构建,主要是工作,然后我有一个facepalm时刻,并不得不添加XA事务处理。没什么大不了的,这是一种常见的模式。一旦我这样做,提交就停止在任何事务管理器上发生。

spring-batch启动程序执行它的工作,日志说工作已经提交,但提交从未发生过。

我将Spring代码追溯到JDBC驱动程序中。在调试器中,我已经确认提交方法一直被调用到驱动程序中,但是在调用驱动程序的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)

1 个答案:

答案 0 :(得分:0)

真正的错误 在另一个配置文件中,有一个BasicDataSource类的bean。实例化的一些bean正在获取此数据源而不是事务管理器中的数据源。

我是如何解决的

第1步:晚安睡觉

第2步:更多咖啡因

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

我使用正确的类重新定义了这些数据源bean,并且一切都很好。