使用spring的配置事物注解@Transactional,在测试的时候发现不起做用。html
<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" />
@Transactional(value="studentMGTxManager",rollbackFor=java.lang.Exception.class) public void saveStudentDto(List<StudentDto> dtoList, String classId) { }
数据库使用的存储引擎是MyISam,MyISam不支持事物,应该用InnoDB引擎java
@Transactional注解事务不起做用的解决
可能的缘由:
1.数据库引擎要支持事务
若是是mysql,注意表要使用支持事务的引擎,好比innodb,若是是myisam,事务是不起做用的
2.是否开启了对注解的解析
配置文件必须加<tx:annotation-driven />,不然不解析@Transactional mysql