Spring @Transactional注解不生效
在spring mvc + mysql的项目中,使用Transactional的时候不回滚。
在方法上加的是@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
- 首先先检查了mysql的存储引擎是
Innodb
,支持事务的 然后不生效的原因可能就是配置不对了。查了Spring的官方文档之后,可以看到
You can omit the transaction-manager attribute in the tag if the bean name of the PlatformTransactionManager that you want to wire in has the name transactionManager. If the PlatformTransactionManager bean that you want to dependency-inject has any other name, then you have to use the transaction-manager attribute explicitly, as in the preceding example.
The @EnableTransactionManagement annotation provides equivalent support if you are using Java based configuration. Simply add the annotation to a @Configuration class. See the javadocs for full details.
需要配置
<tx:annotation-driven />
元素或者使用java配置@EnableTransactionManagement
spring boot中已经自动配置,自动配置的类是DataSourceTransactionManagerAutoConfiguration