Spring声明式事物配置

事物配置:

<!-- 拦截器方式配置事物 ,不在列表的方法名称spring不会管理事物-->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />

			<tx:method name="get*"  read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="select*"  read-only="true" />
			<tx:method name="query*" read-only="true" />

		</tx:attributes>
	</tx:advice>
	<!-- Spring aop事务管理 -->
	<aop:config>
		<aop:pointcut id="transactionPointcut"
			expression="execution(* s.s.m.service..*Impl.*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut"
			advice-ref="transactionAdvice" />
	</aop:config>

如果Service里的方法是在配置的上半部分,如insertGoods,则Spring会管理事物,抛出异常回滚,没异常提交;


如果Service里的方法是在配置的下半部分,如getGoods,则 Spring会管理事物,不过此时只允许读数据里的数据,不允许修改数据库里的数据且抛出如下异常:

### SQL: insert into t_bs_goods_back(goods_name) values(?)### Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed; SQL []; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

说明:此种情况不允许配置propagation。


如果Service里的方法是不在配置里,则Spring不会管理事物,无论是否抛出异常都会提交事物。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值