本人博客已迁移至 www.shangyang.me 欢迎大家访问
1. xaDataSource 配置如下. (DB2)
<bean id="xaDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <!-- Set unique name for this datasource --> <property name="uniqueResourceName"><value>db2</value></property> <!-- Set XADatasource class name--> <property name="xaDataSourceClassName"> <value>com.ibm.db2.jcc.DB2XADataSource</value> </property> <property name="xaProperties"> <props> <!-- Server name of DB2 server --> <prop key="serverName">localhost</prop> <!-- Port number of DB2 server --> <prop key="portNumber">50000</prop> <prop key="databaseName">myDb</prop> <prop key="user"></prop> <prop key="password"></prop> <prop key="driverType">2</prop> <!-- For DB2 data source,the dufault isolation level is TRANSACTION_READ_COMMITTED, --> <!-- so we can comment out the line below --> <!-- prop key="defaultIsolationLevel">2</prop--> </props> </property> <!-- set properties for datasource connection pool --> <property name="maxPoolSize"><value>10</value></property> <property name="minPoolSize"><value>2</value></property> <property name="loginTimeout"><value>30</value></property> <property name="reapTimeout"><value>20000</value></property> <!-- set a SQL for testing connection --> <property name="testQuery"> <value>select 1 from MyDb.table</value> </property> </bean>
配置xaDataSource的时候应注意设置 reapTimeout, 其他的可以用默认值.
com.atomikos.jdbc.AtomikosDataSourceBean 继承自 com.atomikos.jdbc.AbstractDataSourceBean
AbstractDataSourceBean 将 reapTimeout定义如下
/**
* Sets the amount of time (in seconds) that the connection pool will allow a connection
* to be in use, before claiming it back. Optional.
*
* @param reapTimeout The timeout in seconds. Zero means unlimited.
* Note that this value is only an indication;
* the pool will check regularly as indicated by the maintenanceInteval property.
* Default is 300 seconds (5 minutes).
*/
public void setReapTimeout(int reapTimeout) {
this.reapTimeout = reapTimeout;
}
可以看到 reapTimeout 是管理 Connection 被占用的时间.
如果不设置这个值,Atomikos使用默认的5分钟,那么在处理大批量数据读取的时候,一旦超过5分钟,就会抛出类似 Resultset is close 的错误.
2. Transaction 配置如下
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown"> <value>true</value> </property> </bean> <!-- use com.atomikos.icatch.jta.UserTransactionImp as transaction, --> <!-- "transactionTimeout" property should be change accord to product environment, --> <!-- it defined in millisecond unit--> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout"> <value>20000</value> </property> </bean> <!-- define JtaTransactionManager--> <bean id="interfaceTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"> <ref bean="atomikosTransactionManager" /> </property> <property name="userTransaction"> <ref bean="atomikosUserTransaction" /> </property> </bean>
配置 Atomikos 的 JtaTransaction 的时候需注意, 单这样配置, 这里的设置的 20000 秒有可能不会生效。
Atomikos 对它的Transaction Manager 有个宏观上的设置. 可以参考
http://www.atomikos.com/Documentation/JtaProperties
注意 com.atomikos.icatch.max_timeout 这个参数. 他的作用是给所有的 Transaction 的 Timeout设置一个最大值(单位是毫秒),默认值是5分钟,如果小于它生效,如果大于它不生效(还是5分钟)。
所以,如果没有设置 com.atomikos.icatch.max_timeout 这个参数的话,设置20000秒是不会生效的.
如何设置 com.atomikos.icatch.max_timeout , 文档里面描述,部署一个 jta.properties 文件在 classpath的根目录下(bin目录),但是不同的atomikos可能不同,我部署的是 transactions.properties文件.
如何设置
#Set the max timeout (in milliseconds) for local transactions com.atomikos.icatch.max_timeout = 20000000
这样的话, atomikosUserTransaction 设置的 Timeout 20000 才能生效.
Reference: http://www.atomikos.com/Documentation
附: Atomikos 源码