项目中配置了两种数据源,一种是hana, 一种是mysql
场景:
在方法中,使用了hana数据源从hana数据库中查出数据,然后将数据写到mysql数据库中,对数据库进行修改操作时,我们一般会添加事务回滚的操作,以便程序出错时可以还原数据库。
问题:
在方法中添加能使程序异常的代码(比如:int i = 1/0;)进行测试,并在方法中贴上以下注解:
@Transactional(rollbackFor = Exception.class)
但是发现事务并没有回滚
检查了项目的配置, 两个数据源配置了事务管理器:
hana数据源配置:
import javax.sql.DataSource;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import top.ibase4j.core.Con