MyBatis Spring回滚无法与多个事务管理器一起工作
我已将应用程序配置为与MyBatis Spring一起使用,我希望连接到多个数据库 为此,在我的applicationContext.xml中,我定义了一个数据源、一个事务管理器(org.springframework.jdbc.datasource.DataSourceTransactionManager)、一个Sql会话工厂(org.mybatis.spring.SqlSessionFactoryBean)和一个MapperScannerconfiguer(org.mybatis.spring.mapper.MapperScannerconfiguer)每一个人 然后,在我的服务类中我想对同一方法中的多个数据库执行CRUD操作。由于我必须指出正确的事务管理器,我已经完成了下面的评论:MyBatis Spring回滚无法与多个事务管理器一起工作,spring,mybatis,transactional,Spring,Mybatis,Transactional,我已将应用程序配置为与MyBatis Spring一起使用,我希望连接到多个数据库 为此,在我的applicationContext.xml中,我定义了一个数据源、一个事务管理器(org.springframework.jdbc.datasource.DataSourceTransactionManager)、一个Sql会话工厂(org.mybatis.spring.SqlSessionFactoryBean)和一个MapperScannerconfiguer(org.mybatis.sprin
@Service("myServiceDB")
public class MyServiceDB implements MyService {
[...]
@Transactional(value = "TransactionManager1", rollbackFor = MyCustomException.class)
public MyUser multipleMethod(final int idUser) throws MyCustomException {
MyUser myUser = null;
int rowsAffected1 = -1;
int rowsAffected2 = -1;
try {
myUser = this.mapperOne.getById(idUser);
if (myObject != null) {
rowsAffected1 = this.mapperOne.deleteByIdUser(idUser);
}
if (rowsAffected1 == 1) {
insertUser(myUser);
}
} catch (DataAccessException dae) {
throw new MyCustomException(TRANSACTION_ERROR, dae);
}
if ((myUser == null) || (rowsAffected1 != 1)) {
throw new MyCustomException(TRANSACTION_ERROR);
}
return myUser;
}
@Transactional(value = "TransactionManager2", rollbackFor = MyCustomException.class)
public void insertUser(final MyUser myUser) throws MyCustomException{
int rowsAffected = -1;
try {
rowsAffected = this.mapperTwo.insert(myUser);
**throw new MyCustomException();**
} catch (DataAccessException dae) {
throw new MyCustomException(TRANSACTION_ERROR, dae);
}
//if (rowsAffected != 1) {
// throw new MyCustomException(TRANSACTION_ERROR);
//}
}
[...]
}
因此,每个方法都指向其对应的事务管理器
如果我在插入后的第二个方法中抛出自定义异常,我将正确回滚在第一个方法中进行的删除。但是,第二个事务管理器执行的插入没有按照我的要求正确回滚。(即,用户插入到第二个数据库中,但没有在第一个数据库中删除)
我的问题是:
- 有可能实现我想要的吗
- 如何配置@Transactional注释