问题
使用spring的配置事物注解@Transactional,在测试的时候发现不起作用。
环境
配置文件
<bean id="studentMGDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${student_MG_jdbc.driver}" />
<property name="url" value="${student_MG_jdbc.url}" />
<property name="username" value="${student_MG_jdbc.username}" />
<property name="password" value="${student_MG_jdbc.password}" />
<property name="initialSize" value="${student_MG_jdbc.initialSize}" />
<property name="maxActive" value="${student_MG_jdbc.maxActive}" />
<property name="maxIdle" value="${student_MG_jdbc.maxIdle}" />
<property name="maxWait" value="${student_MG_jdbc.maxWait}" />
<property name="defaultAutoCommit" value="${student_MG_jdbc.defaultAutoCommit}" />
</bean>
<bean id="studentMGSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/mybatis-studentMG-config.xml" />
<property name="dataSource" ref="studentMGDataSource" />
</bean>
<bean id="studentMGSqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="studentMGSqlSessionFactory" />
</bean>
<bean id="studentMGTxManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="studentMGDataSource" />
</bean>
<tx:annotation-driven proxy-target-class="true" transaction-manager="studentMGTxManager" />
Java代码
@Transactional(value="studentMGTxManager",rollbackFor=java.lang.Exception.class)
public void saveStudentDto(List<StudentDto> dtoList, String classId) {
}
原因
数据库使用的存储引擎是MyISam,MyISam不支持事物,应该用InnoDB引擎
TIPS
@Transactional注解事务不起作用的解决
可能的原因:
1.数据库引擎要支持事务
如果是mysql,注意表要使用支持事务的引擎,比如innodb,如果是myisam,事务是不起作用的
2.是否开启了对注解的解析
配置文件必须加<tx:annotation-driven />,否则不解析@Transactional