简体   繁体   中英

Java Spring - Mybatis - Mysql Transaction not working

We are using the Spring boot/ MySQL/ mybatis combination for our new project. mybatis-spring for the transaction management.

The MySQL engine we are using is innoDB. We are for the first time using the MySQL DB for a micro service.

The issue we are facing is that the transactions are not rolling back on DB even though the spring Transaction trace logs says they are. The log

TRACE TransactionInterceptor:545 - Completing transaction for [service.EntityActionServiceImplementation.updateApplicationStatus] after exception: business.exception.BusinessException: / by zero 2018-11-14 16:22:32 TRACE RuleBasedTransactionAttribute:134 - Applying rules to determine whether transaction should rollback on business.exception.BusinessException: / by zero 2018-11-14 16:22:32 TRACE RuleBasedTransactionAttribute:151 - Winning rollback rule is: RollbackRuleAttribute with pattern [business.exception.BusinessException] 2018-11-14 16:22:32 TRACE TransactionSynchronizationManager:248 - Removed value [org.mybatis.spring.SqlSessionHolder@6a1568d6] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@3db65c0d] from thread [main] 2018-11-14 16:22:32 TRACE TransactionSynchronizationManager:142 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@73bb573d] for key [org.apache.commons.dbcp.BasicDataSource@76cdafa3] bound to thread [main] 2018-11-14 16 :22:32 TRACE TransactionSynchronizationManager:248 - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@73bb573d] for key [org.apache.commons.dbcp.BasicDataSource@76cdafa3] from thread [main] 2018-11-14 16:22:32 DEBUG DataSourceUtils:340 - Returning JDBC Connection to DataSource 2018-11-14 16:22:32 DEBUG DataSourceTransactionManager:836 - Initiating transaction rollback 2018-11-14 16:22:32 DEBUG DataSourceTransactionManager:341 - Rolling back JDBC transaction on Connection [HikariProxyConnection@16957838 wrapping oracle.jdbc.driver.T4CConnection@11c78080] 2018-11-14 16:22:32 TRACE TransactionSynchronizationManager:336 - Clearing transaction synchronization

CODE Block

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = BusinessException.class)
            public boolean updateApplicationStatus(){
    try{
    }catch(Exception ex){
     throw new BusinessException(ex.getMessage(), ex.getCause());
    }
}

The behaviour changes if I set an auto-commit false for the data source. in this scenario, the Transaction is not committing at all.

The BusinessException extends a RunTimeException.

The Schema and table engines are InnoDB.

Are we missing any mysql Specific settings?

I was using multiple transaction managers for the multiple databases. Added the transaction manager identifier in the annotation solved the issue.

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