我试图在 JobListener 实现的 afterJob() 方法中插入一些记录。我的听众如下所示,
public class SampleListener2 implements JobListener {
@Autowired
DataSource dataSource;
@Override
public void beforeJob() throws Exception {}
@Override
public void afterJob() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Object[]> objectList = new ArrayList<Object[]>();
Object[] objectArray = {System.currentTimeMillis(), "After Job", "After Job", "1.0"};
objectList.add(objectArray);
jdbcTemplate.batchUpdate("insert into RAW_REPORT(DATE,IMPRESSIONS,CLICKS,EARNINGS) values (?,?, ?, ?)", objectList);
System.out.println("After Job 2");
}
在调试时,我发现 Spring-Batch 没有提交事务,导致记录没有显示在数据库中。如果我手动尝试提交事务,我可以通过数据库中的记录找到
public class SampleListener2 implements JobListener {
@Autowired
DataSource dataSource;
@Inject
private PlatformTransactionManager transactionManager;
@Override
public void beforeJob() throws Exception {}
@Override
public void afterJob() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
List<Object[]> objectList = new ArrayList<Object[]>();
Object[] objectArray = {System.currentTimeMillis(), "Before Job", "Before Job", "1.0"};
objectList.add(objectArray);
jdbcTemplate.batchUpdate("insert into RAW_REPORT(DATE,IMPRESSIONS,CLICKS,EARNINGS) values (?,?, ?, ?)", objectList);
transactionManager.commit(status);
System.out.println("Before Job 1");
}
}
有人可以让我知道这种行为的详细信息吗,我不希望手动处理事务并希望 spring-batch 将我的记录提交到侦听器中。请求您对此问题的帮助
编辑: 测试场景的示例代码: https ://github.com/rbutti/SpringBatch/tree/master/SpringJSR352