@Transactional事务不起作用的解决

问题

        使用spring的配置事物注解@Transactional,在测试的时候发现不起作用。

环境

       

         配置文件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <bean id="studentMGDataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  2.     destroy-method="close">  
  3.     <property name="driverClassName" value="${student_MG_jdbc.driver}" />  
  4.     <property name="url" value="${student_MG_jdbc.url}" />  
  5.     <property name="username" value="${student_MG_jdbc.username}" />  
  6.     <property name="password" value="${student_MG_jdbc.password}" />  
  7.     <property name="initialSize" value="${student_MG_jdbc.initialSize}" />  
  8.     <property name="maxActive" value="${student_MG_jdbc.maxActive}" />  
  9.     <property name="maxIdle" value="${student_MG_jdbc.maxIdle}" />  
  10.     <property name="maxWait" value="${student_MG_jdbc.maxWait}" />  
  11.     <property name="defaultAutoCommit" value="${student_MG_jdbc.defaultAutoCommit}" />  
  12. </bean>  
  13.   
  14. <bean id="studentMGSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  15.     <property name="configLocation" value="classpath:mybatis/mybatis-studentMG-config.xml" />  
  16.     <property name="dataSource" ref="studentMGDataSource" />  
  17. </bean>  
  18.   
  19. <bean id="studentMGSqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
  20.     <constructor-arg index="0" ref="studentMGSqlSessionFactory" />  
  21. </bean>  
  22.   
  23. <bean id="studentMGTxManager"  
  24.     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  25.     <property name="dataSource" ref="studentMGDataSource" />  
  26. </bean>  
  27.   
  28. <tx:annotation-driven proxy-target-class="true" transaction-manager="studentMGTxManager" />  

        Java代码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Transactional(value="studentMGTxManager",rollbackFor=java.lang.Exception.class)  
  2. public void saveStudentDto(List<StudentDto> dtoList, String classId) {  
  3.       
  4. }  

原因

        数据库使用的存储引擎是MyISam,MyISam不支持事物,应该用InnoDB引擎

TIPS

        @Transactional注解事务不起作用的解决
         可能的原因:
        1.数据库引擎要支持事务
        如果是mysql,注意表要使用支持事务的引擎,比如innodb,如果是myisam,事务是不起作用的

        2.是否开启了对注解的解析
        配置文件必须加<tx:annotation-driven />,否则不解析@Transactional

发布了90 篇原创文章 · 获赞 21 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/yx13649017813/article/details/47661257