@Transactional事务不起作用的解决



问题

        使用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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值