通过打印事务管理器,发现Mybatis环境下的事务管理器实例为JpaTransactionManager对象,明显不对。
Mybatis事务中使用Jpa的事务管理导致事务失效回滚失败问题;
ORM事务管理器对应关系:
添加个@Bean配置,注册jdbc事务管理器DataSourceTransactionManager,其中dataSource由框架自动注入;
项目中jpa、mybatis混用时,需要在使用@Bean(“xxx”)注解注册对象时,对dataSource、sqlSessionFactory、DataSourceTransactionManager显示配置;
//注册
@Configuration
public class MybatisConfig {
@Bean("jdbcTransactionManager")
public PlatformTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
//使用
@Transactional(value = "jdbcTransactionManager",rollbackFor = {Exception.class})
public void TestTran4Salf(){
...
}
三种配置方式:
mybatis:
configuration:
#事务注册日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus:
mapper-locations: classpath:/mapper/**/**.xml
configuration:
#事务注册日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
logging:
level:
root: info
#事务提交回滚日志
org.springframework.jdbc.datasource.DataSourceTransactionManager: debug
@Configuration
public class MybatisConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@PostConstruct
public void addSqlInterceptor() {
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
sqlSessionFactory.getConfiguration().setCallSettersOnNulls(true);//返回查询中的null值
sqlSessionFactory.getConfiguration().setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);//输出执行SQL
}
}
}
@Configuration
public class MybatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> {
configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
configuration.setCallSettersOnNulls(true);//返回查询中的null值
configuration.setMapUnderscoreToCamelCase(false);//表字段使用下划线命名
configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);//输出执行SQL
};
}
}
注解@EnableTransactionManagement标记启用事务,没加事务不会起作用,手动调用session对象的rollback()也无效;
@EnableTransactionManagement
@SpringBootApplication()
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
注入一个事务管理器实例,打印出来看看实例对象是否匹配当前ORM;
@Autowired
PlatformTransactionManager platformTransactionManager;
@Test
public String testTran() {
System.out.print(">>>>>事务管理器:"+platformTransactionManager);
....
}
打开Mybatis执行SQL日志,方便滚出事务执行情况;
>>>>>事务管理器:org.springframework.jdbc.datasource.DataSourceTransactionManager@2fbbdcaCreating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] --事务
JDBC Connection [ProxyConnection[PooledConnection[org.postgresql.jdbc.PgConnection@14fc5be1]]] will be managed by Spring
==> Executing: select * from mytable;
<== Total: 2
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] --事务
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] from current transaction --事务
==> Preparing: insert into mytable(log_id, create_date) VALUES ('1678872584952_1',now());
==> Parameters:
<== Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] --事务
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] from current transaction --事务
==> Preparing: insert into mytable(log_id, create_date) VALUES ('1678872584952_2',now());
==> Parameters:
<== Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] --事务
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] from current transaction --事务
==> Preparing: insert into mytable(log_id, create_date) VALUES ('1678872584952_2',now());
==> Parameters:
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] --事务
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] --事务
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1149ccc6] --事务
启动类上需要显示加入@EnableTransactionManagement注解,否则事务不生效。
Mybatis和JPA事务管理器对应关系:
@Transactional() 未指定时,只对运行时异常回滚;
@Transactional(rollbackFor = {Exception.class})此时会对所有异常回滚;
try {
mapper.insert(sql1);
mapper.insert(sql2);
}catch (Exception e){
e.printStackTrace();
throw e; //注释掉这行将不回滚
}