首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当存在现有事务时,spring-boot MyBatisBatchItemWriter无法更改ExecutorType

当存在现有事务时,spring-boot MyBatisBatchItemWriter无法更改ExecutorType
EN

Stack Overflow用户
提问于 2016-05-09 19:22:42
回答 1查看 3.3K关注 0票数 1

我用的是spring boot和mybatis MyBatisBatchItemWriter。使用demo将数据(Mysql)写入数据库,当没有问题时。但在我的项目org.springframework.dao.TransientDataAccessResourceException: Cannot change the ExecutorType when there is an existing transaction at org.mybatis.spring.SqlSessionUtils.getSqlSession(SqlSessionUtils.java:91) ~[mybatis-spring-1.2.2.jar:1.2.2] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:353) ~[mybatis-spring-1.2.2.jar:1.2.2] at com.sun.proxy.$Proxy45.update(Unknown Source) ~[na:na]中使用

这是我的演示:

代码语言:javascript
复制
 @Bean
public MyBatisBatchItemWriter<Hfbank> writer() {
    MyBatisBatchItemWriter<Hfbank> writer = new MyBatisBatchItemWriter<Hfbank>();
    writer.setSqlSessionFactory(sqlSessionFactory);
    String statementId = "com.springboot.dao.HfbankDao.insertSelective";
    writer.setStatementId(statementId);
    CompositeItemWriter compositeItemWriter  = new CompositeItemWriter();
     List delegates = new ArrayList();
     delegates.add(writer);
     compositeItemWriter.setDelegates(delegates);
     writer.setAssertUpdates(false);
    return writer;
}

这是我的MyBatisBatchItemWriter:

代码语言:javascript
复制
@Bean
 @StepScope
public MyBatisBatchItemWriter<ChannelDataInfo> writer(@Value("#{jobParameters[channelid]}")  Long channelid) {
    MyBatisBatchItemWriter<ChannelDataInfo> writer = new MyBatisBatchItemWriter<ChannelDataInfo>();
    SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);

    writer.setSqlSessionFactory(sqlSessionFactory);
    String statementId = "com.kaigejava.fundcheck.repository.ChannelDataInfoRepository.insertSelective";

    writer.setStatementId(statementId);
    CompositeItemWriter compositeItemWriter  = new CompositeItemWriter();
     List delegates = new ArrayList();
     delegates.add(writer);
     compositeItemWriter.setDelegates(delegates);
     writer.setAssertUpdates(false);
    return writer;
}

为什么演示没问题,但我的项目有错误?

EN

回答 1

Stack Overflow用户

发布于 2016-11-12 00:56:07

因为它是这样说的:您不能在事务中更改executor类型。

看起来您尝试过将某些内容作为包含其他SQL操作的更广泛事务的一部分进行批处理编写,但该事务是以简单(默认)或重用executor类型启动的。

很明显,批处理写需要批处理执行器类型,但是一旦事务启动,它的执行器类型就不能改变。因此,如果您的RDBMS允许,请在单独的事务中执行批处理操作,或者运行嵌套事务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37114516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档