我的应用程序使用Spring Boot + MyBatis,当我使用@Transactional注释时,它在抛出异常时不会回滚。
我的MybatisConfiguration.java是:
@Configuration
@EnableTransactionManagement
@MapperScan("com.mode.dao")
public class MybatisConfiguration implements EnvironmentAware {
private RelaxedPropertyResolver properties;
@Override
public void setEnvironment(Environment env) {
this.properties = new RelaxedPropertyResolver(env, "mode.jdbc.");
}
@Bean(name = "datasource", destroyMethod = "close", initMethod = "init")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(properties.getProperty("driverClassName"));
dataSource.setUrl(properties.getProperty("url"));
dataSource.setUsername(properties.getProperty("username"));
dataSource.setPassword(properties.getProperty("password"));
dataSource.setMaxActive(properties.getProperty("maxActive", Integer.class, 1));
dataSource.setInitialSize(properties.getProperty("initialSize", Integer.class, 1));
return dataSource;
}
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
return sessionFactory.getObject();
}
}
,服务代码快照为:
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public Map<String, String> productImport(Product product) throws Exception {
.....
}
Application.java是:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
build.gradle是:
// Spring Boot MVC
compile("org.springframework.boot:spring-boot-starter-web")
// Spring Boot jdbc
compile("org.springframework.boot:spring-boot-starter-jdbc")
// Mybatis
compile("org.mybatis:mybatis:${mybatisVersion}")
compile("org.mybatis:mybatis-spring:${mybatisSpringVersion}")
之前我使用过spring + mybatis,而@Transactional注释效果很好,但是当我改为spring-boot时它没有用......
非常感谢任何帮助或想法,提前致谢!