简体   繁体   中英

No rollback on transaction - Spring Boot 2.0

I have a problem with Spring Boot 2.0.0. When I throw an RuntimeException it is not rollback the transaction. I was using Spring Boot 1.5.9 with the same settings and it worked. It just migrated to Spring Boot 2 and stopped working.

My Configuration class:

@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);
    }

}

My Business class:

@Service
public class TestBusinessImpl implements TestBusiness {

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

        throw new RuntimeException("Test rollback");
    }

}

Does anyone know what may be happening?

Which dialect are you using? You must be specifying spring.jpa.properties.hibernate.dialect

Springboot 2.0 @Transaction doesn't get supported by org.hibernate.dialect.MySQL5Dialect

Rather use org.hibernate.dialect.MySQL5InnoDBDialect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM