事务上没有回滚 - Spring Boot 2.0

时间:2018-03-24 21:35:11

标签: java spring spring-boot

我遇到Spring Boot 2.0.0的问题。当我抛出RuntimeException时,它不会回滚事务。我使用相同的设置使用Spring Boot 1.5.9并且它有效。它刚刚迁移到Spring Boot 2并停止工作。

我的配置类:

@Configuration
@EnableJpaRepositories(basePackages = "com.test.repository")
@EnableTransactionManagement
public class DatabaseConfiguration {

    public static final String MODEL_PACKAGE = "com.test.model";

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DataSource dataSource() throws SQLException {
        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl(this.url);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

我的商务舱:

@Service
public class TestBusinessImpl implements TestBusiness {

    @Override
    @Transactional
    public void save(final Test test) {
        this.testRepository.save(test);

        throw new RuntimeException("Test rollback");
    }

}

有谁知道可能发生的事情?

1 个答案:

答案 0 :(得分:8)

你正在使用哪种方言?您必须指定spring.jpa.properties.hibernate.dialect

org.hibernate.dialect.MySQL5Dialect不支持SpringTraed 2.0 @Transaction

而是使用org.hibernate.dialect.MySQL5InnoDBDialect