@Transactional事务不起作用的解决
问题
使用spring的配置事物注解@Transactional,在测试的时候发现不起作用。
环境
配置文件
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代码
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
浙公网安备 33010602011771号