Java Spring @Transactional method not rolling back as expected

后端 未结 3 2109
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 00:09

Below is a quick outline of what I\'m trying to do. I want to push a record to two different tables in the database from one method call. If anything fails, I want everythin

3条回答
  •  再見小時候
    2020-12-29 00:37

    I think the behavior you encounter is dependent on what ORM / persistence provider and database you're using. I tested your case using hibernate & mysql and all my transactions rolled back alright.

    If you do use hibernate enable SQL and transaction logging to see what it's doing:

    log4j.logger.org.hibernate.SQL=DEBUG
    log4j.logger.org.hibernate.transaction=DEBUG
    // for hibernate 4.2.2 
    // log4j.logger.org.hibernate.engine.transaction=DEBUG
    

    If you're on plain jdbc (using spring JdbcTemplate), you can also debug SQL & transaction on Spring level

    log4j.logger.org.springframework.jdbc.core=DEBUG
    log4j.logger.org.springframework.transaction=DEBUG
    

    Double check your autocommit settings and database specific peciular (eg: most DDL will be comitted right away, you won't be able to roll it back although spring/hibernate did so)

提交回复
热议问题