【发布时间】:2014-12-01 19:18:00
【问题描述】:
我尝试在 Websphere 中以 JSR 352 模式使用 Spring Batch。 (Websphere 8.0,Spring Batch 3.0.1)
据我了解文档,spring 应该处理事务,即在调用步骤的 ItemReader 之前开始事务,在调用 ItemWriter 之后提交事务等。
但是,在我的情况下,调用 ItemReader 时没有事务处于活动状态(userTransaction.getStatus() == 6)。如果我自己在 itemReader 中启动事务,我的代码就可以工作,但我的理解是我不应该这样做。
我怀疑问题出在我设置批次的方式上。
这是显示问题的示例代码:
META_INF/batch.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:jta-transaction-manager />
</beans>
META-INF/batch-jobs/samplebatch3.xml:
<?xml version="1.0" encoding="UTF-8"?>
<job version="1.0"
id="samplebatch3"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd">
<step id="step1">
<chunk checkpoint-policy="item"
item-count="5">
<reader ref="my.jbatchtest.samplebatch3.SampleReader" />
<processor ref="my.jbatchtest.samplebatch3.SampleProcessor"/>
<writer ref="my.jbatchtest.samplebatch3.SampleWriter" />
</chunk>
</step>
</job>
ItemReader:
package my.jbatchtest.samplebatch3;
import java.io.Serializable;
import javax.batch.api.chunk.ItemReader;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
import org.xadisk.connector.outbound.XADiskConnectionFactory;
public class SampleReader implements ItemReader {
private UserTransaction utx;
public SampleReader() {
// TODO Auto-generated constructor stub
}
@Override
public void open(Serializable checkpoint) throws Exception {
utx = (UserTransaction) new InitialContext().lookup("jta/usertransaction");
System.out.println("Status before begin:"+utx.getStatus());
utx.begin();
System.out.println("Status after begin:"+utx.getStatus());
}
@Override
public void close() throws Exception {
// TODO Auto-generated method stub
}
@Override
public Object readItem() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override
public Serializable checkpointInfo() throws Exception {
// TODO Auto-generated method stub
return null;
}
}
ItemReader 的调试输出:
[07.10.14 12:52:48:881 CEST] 00000039 SystemOut O Status before begin:6
[07.10.14 12:52:48:881 CEST] 00000039 SystemOut O Status after begin:0
我的问题是:
- 我的理解是否正确,Spring Batch 应该管理事务?
- 那为什么不这样做呢?
【问题讨论】:
标签: java spring-batch websphere-8