Java 对Mybatis批处理使用Spring事务

Java 对Mybatis批处理使用Spring事务,java,spring,mybatis,spring-mybatis,Java,Spring,Mybatis,Spring Mybatis,我有一个@Transactional方法,它调用公共Springbean上的另一个方法,该方法长期以来一直用于跨映射程序执行批处理操作。现在的问题是,如果@Transactional方法中存在错误,则在批处理上执行的DML不会回滚,因为它们是在不同的会话上使用自己的事务执行的 public class HamsterRepo{ ... @Autowired BatchOperationBean<Hamsters> idioticBatchBean; @Transaction

我有一个
@Transactional
方法,它调用公共Springbean上的另一个方法,该方法长期以来一直用于跨映射程序执行
批处理
操作。现在的问题是,如果
@Transactional
方法中存在错误,则在
批处理
上执行的DML不会回滚,因为它们是在不同的会话上使用自己的事务执行的

public class HamsterRepo{
...

 @Autowired 
 BatchOperationBean<Hamsters> idioticBatchBean;
 @Transactional
 public void saveHamsters(List<Hamsters> hams){
  idioticBatchBean.executebatch("saveHamsters", hams);
 }
}
 public class BatchOperationBean<T>{
 @Autowired 
 SqlSessionFactory sqlFactory; 

 public void executebatch(String mapperId, List<T> ts){
  SqlSession sqlSession = 
  this.sqlSessionFactory.openSession(ExecutorType.BATCH,
                TransactionIsolationLevel.SERIALIZABLE); 
  try(sqlSession){
   for(T t in ts){
    sqlSession.update(mapperId , t);
   }
  sqlSession.commit();
  // Clean Up stuff and Exception Handling...
  }
 }
}
公共类仓鼠{
...
@自动连线
BatchOperationBean独特的BatchBean;
@交易的
公共空间仓鼠(列出火腿){
白痴BatchBean.executebatch(“拯救仓鼠”,火腿);
}
}
公共类BatchOperationBean{
@自动连线
SqlSessionFactory;
public void executebatch(字符串mapperId,列表ts){
SqlSession SqlSession=
此.sqlSessionFactory.openSession(ExecutorType.BATCH,
TransactionIsolationLevel.SERIALIZABLE);
try(sqlSession){
对于(T在ts中){
sqlSession.update(mapperId,t);
}
提交();
//清理内容和异常处理。。。
}
}
}
现在,有没有办法将这两个
Spring Tx和SqlSessionFactory关联起来?是否使用SqlSession而不是factory帮助?或者有没有办法从SpringTX获取SqlSession?或者Spring中的一种方法,在没有SqlSesion的情况下跨映射器识别和执行查询


PS:使用Spring 4.1.7、Mybatis:3.4.4、Mybatis-Spring:1.3.1看起来像是链接Spring事务和Mybatis-SqlSession的方法

线程安全、由Spring管理的SqlSession,它与Spring事务管理一起工作,以确保使用的实际SqlSession是与当前Spring事务关联的SqlSession。此外,它还管理会话生命周期,包括根据Spring事务配置根据需要关闭、提交或回滚会话


看起来链接Spring事务和Mybatis SqlSession的方法是

线程安全、由Spring管理的SqlSession,它与Spring事务管理一起工作,以确保使用的实际SqlSession是与当前Spring事务关联的SqlSession。此外,它还管理会话生命周期,包括根据Spring事务配置根据需要关闭、提交或回滚会话