gpt4 book ai didi

java - Spring Batch CompositeItemWriter事务回滚问题

转载 作者:行者123 更新时间:2023-12-01 10:56:03 26 4
gpt4 key购买 nike

所有,我在使用 Spring Batch CompositeItemWriter 时遇到事务回滚问题。 Spring批处理的版本是2.2.7。

我的配置与此处的帖子相同:https://stackoverflow.com/questions/32432519/compositeitemwriter-doesnt-roll-back-in-spring-batch

<bean id="compositeItemWriter" class="org.springframework.batch.item.support.CompositeItemWriter">
<property name="delegates">
<list>
<ref bean="firstItemWriter"/>
<ref bean="secondItemWriter"/>
<ref bean="thirdItemWriter"/>
</list>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${batch.jdbc.driver}" />
<property name="url" value="${batch.jdbc.url}" />
<property name="username" value="${batch.jdbc.user}" />
<property name="password" value="${batch.jdbc.password}" />
</bean>

当thirdItemWriter抛出异常时,之前的写入器写入数据库的数据不会回滚。

不确定我错过了什么,但我的理解是复合项目编写器共享相同的事务,在发生异常时所有数据都应该回滚。

如有任何建议,我们将不胜感激

最佳答案

经过几天的努力,终于找到了解决方案。关键是将所有编写器放入一个事务中并关闭自动提交。

public class TransactionAwareCompositeItemWritter<T> extends CompositeItemWriter<T> {
private static Logger LOG = LoggerFactory.getLogger(TransactionAwareCompositeItemWritter.class);

private List<ItemWriter<? super T>> delegates;

private PlatformTransactionManager transactionManager;

private JdbcTemplate jdbcTemplate;

@Override
public void write(final List<? extends T> item) throws Exception {

Connection con = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());

if (con.getAutoCommit()) {

LOG.debug("Switching JDBC Connection [" + con + "] to manual commit");

con.setAutoCommit(false);
}

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setName("CompositeItemWriter Transaction");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

TransactionStatus status = transactionManager.getTransaction(def);
try {
for (ItemWriter<? super T> writer : delegates) {
writer.write(item);
}
} catch (Exception e) {
transactionManager.rollback(status);
throw e;
}

transactionManager.commit(status);

if (!con.getAutoCommit()) {

LOG.debug("Switching JDBC Connection [" + con + "] to auto commit");

con.setAutoCommit(true);
}

}

public void setDelegates(List<ItemWriter<? super T>> delegates) {
super.setDelegates(delegates);
this.delegates = delegates;
}

public PlatformTransactionManager getTransactionManager() {
return transactionManager;
}

public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

}

关于java - Spring Batch CompositeItemWriter事务回滚问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com