Skip to main content
 首页 » 编程设计

mysql之MyBatis Spring在Mysql上的事务,不回滚

2023年09月26日56myhome

我会在这里尽可能简洁。我浏览了网络上报告的许多类似问题,并根据这些问题评估了我的问题。但是,它并没有解决我的问题。所以最后我把这个放出来了。

我有一个带有默认值的 spring 注释事务服务(在实现的类方法上注释)。该服务通过直接调用 mybatis 映射器方法(在服务中 Autowiring )来执行一些插入(在循环内)和无参数存储过程调用。

我发现事务没有在异常时回滚(运行时异常,正如 Spring 所期望的那样)。我通过注释存储过程调用进行测试,在不使用循环的情况下插入,但这没有帮助。

查看日志,我可以发现虽然事务同步按预期进行了回滚,但 Spring 正在切换到手动提交。我不确定这是否导致了问题?

我在 Tomcat 上使用 JNDI 连接进行了测试,并在驱动程序管理器连接单元测试中进行了测试,两者的行为相同。以下是日志的摘录:

  • Spring 3.1.1
  • mybatis 3.2.1
  • mybatis-spring 1.2.2
  • mysql-connector 5.1.30
  • Mysql 数据库 5.5.24
====================================== 
Using DriverManagerDataSource testing  
====================================== 
2014-10-17 12:34:17 [main] DEBUG DataSourceTransactionManager - Creating new transaction with name [com.ericsson.prf.web.service.impl.ReportingService.saveHeadcountCorrectionsJSON]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
2014-10-17 12:34:17 [main] DEBUG DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/PRFDB] 
2014-10-17 12:34:17 [main] DEBUG DataSourceTransactionManager - Acquired Connection [com.mysql.jdbc.JDBC4Connection@728cacdc] for JDBC transaction 
2014-10-17 12:34:17 [main] DEBUG DataSourceTransactionManager - Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@728cacdc] to manual commit 
2014-10-17 12:34:17 [main] DEBUG SqlSessionUtils - Creating a new SqlSession 
2014-10-17 12:34:17 [main] DEBUG SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@114d4c11] 
2014-10-17 12:34:17 [main] DEBUG SpringManagedTransaction - JDBC Connection [com.mysql.jdbc.JDBC4Connection@728cacdc] will be managed by Spring 
2014-10-17 12:34:17 [main] DEBUG updateHeadcountPlanned - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@728cacdc] 
2014-10-17 12:34:17 [main] DEBUG updateHeadcountPlanned - ==>  Preparing: UPDATE PRF_HEADCOUNT_PLANNED SET COUNT=? WHERE ID=?  
2014-10-17 12:34:17 [main] DEBUG updateHeadcountPlanned - ==> Parameters: 43(Integer), 1629(Long) 
2014-10-17 12:34:17 [main] DEBUG SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@114d4c11] 
2014-10-17 12:34:17 [main] DEBUG SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@114d4c11] from current transaction 
2014-10-17 12:34:17 [main] DEBUG callLoadHCCostPlanned - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@728cacdc] 
2014-10-17 12:34:17 [main] DEBUG callLoadHCCostPlanned - ==>  Preparing: {call load_sap_hc_cost_planned()}  
2014-10-17 12:34:17 [main] DEBUG SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@114d4c11] 
 
2014-10-17 12:34:18 [main] DEBUG SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@114d4c11] 
2014-10-17 12:34:18 [main] DEBUG SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@114d4c11] 
2014-10-17 12:34:18 [main] DEBUG DataSourceTransactionManager - Initiating transaction rollback 
2014-10-17 12:34:18 [main] DEBUG DataSourceTransactionManager - Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@728cacdc] 
2014-10-17 12:34:18 [main] DEBUG DataSourceTransactionManager - Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@728cacdc] after transaction 
2014-10-17 12:34:18 [main] DEBUG DataSourceUtils - Returning JDBC Connection to DataSource 
 
 
====================================== 
Using JndiObjectFactoryBean on Tomcat 
====================================== 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG AnnotationTransactionAttributeSource - Adding transactional method 'saveHeadcountCorrectionsJSON' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DefaultListableBeanFactory - Returning cached instance of singleton bean 'transactionManager' 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DataSourceTransactionManager - Creating new transaction with name [com.ericsson.prf.web.service.impl.ReportingService.saveHeadcountCorrectionsJSON]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DataSourceTransactionManager - Acquired Connection [jdbc:mysql://localhost:3306/PRFDB?autoReconnect=true&useUnicode=true&characterEncoding=UTF8, UserName=prf@localhost, MySQL Connector Java] for JDBC transaction 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DataSourceTransactionManager - Switching JDBC Connection [jdbc:mysql://localhost:3306/PRFDB?autoReconnect=true&useUnicode=true&characterEncoding=UTF8, UserName=prf@localhost, MySQL Connector Java] to manual commit 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SqlSessionUtils - Creating a new SqlSession 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46ee04fb] 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SpringManagedTransaction - JDBC Connection [jdbc:mysql://localhost:3306/PRFDB?autoReconnect=true&useUnicode=true&characterEncoding=UTF8, UserName=prf@localhost, MySQL Connector Java] will be managed by Spring 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG updateHeadcountPlanned - ooo Using Connection [jdbc:mysql://localhost:3306/PRFDB?autoReconnect=true&useUnicode=true&characterEncoding=UTF8, UserName=prf@localhost, MySQL Connector Java] 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG updateHeadcountPlanned - ==>  Preparing: UPDATE PRF_HEADCOUNT_PLANNED SET COUNT=? WHERE ID=?  
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG updateHeadcountPlanned - ==> Parameters: 43(Integer), 1629(Long) 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46ee04fb] 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46ee04fb] from current transaction 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG callLoadHCCostPlanned - ooo Using Connection [jdbc:mysql://localhost:3306/PRFDB?autoReconnect=true&useUnicode=true&characterEncoding=UTF8, UserName=prf@localhost, MySQL Connector Java] 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG callLoadHCCostPlanned - ==>  Preparing: {call load_sap_hc_cost_planned()}  
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46ee04fb] 
 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46ee04fb] 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@46ee04fb] 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DataSourceTransactionManager - Initiating transaction rollback 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DataSourceTransactionManager - Rolling back JDBC transaction on Connection [jdbc:mysql://localhost:3306/PRFDB?autoReconnect=true&useUnicode=true&characterEncoding=UTF8, UserName=prf@localhost, MySQL Connector Java] 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DataSourceTransactionManager - Releasing JDBC Connection [jdbc:mysql://localhost:3306/PRFDB?autoReconnect=true&useUnicode=true&characterEncoding=UTF8, UserName=prf@localhost, MySQL Connector Java] after transaction 
2014-10-17 12:53:51 [http-bio-8080-exec-3] DEBUG DataSourceUtils - Returning JDBC Connection to DataSource 
 

请您参考如下方法:

因为我以为我错过了一些愚蠢的事情! DB引擎是MyISAM,需要改为InnoDB。已解决。