spring boot 的事务管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
    http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    ">
	<!-- mybatis事务管理 -->
	 <!--<tx:annotation-driven transaction-manager="transactionManager" />-->

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>

	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="set*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="load*" read-only="true" />
			<tx:method name="count*" read-only="true" />
			<tx:method name="save*"  rollback-for="Exception"/>
			<tx:method name="add*" rollback-for="Exception" />
			<tx:method name="update*"  rollback-for="Exception"  />
			<tx:method name="delete*" rollback-for="Exception" />
			<tx:method name="merage*" rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="serviceOperation" expression="execution(* com.jointem..*service..*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
	</aop:config>
</beans>

方法一:注解的事物管理

在进行数据库操作的时候就会遇到事务管理,插入的时候要加@Transactional注解进行事务的回滚。两个插入语句如果其中一个执行失败,则两条插入语句都不能执行。

方法二:spring配置文件的事务管理,如上面代码所示

1声明式事务管理

1)配置事物管理器

2)配置注解驱动

3)在方法或类上面添加@Transactional,事务传播性,就是一个事务调用另外一个事务,是用当前事务,还是重新开启一个事务,举个例子就是你去餐厅吃饭,遇到熟人,有两种选择,一种是加入他们做一桌吃饭,一起吃饭,一起离开。另外一种是重新安排一桌吃饭。一个去赋予一个买多本书的操作,他的钱买三不够,买两个够,在付款的时候就给事务添加一个属性,propagation=Propagation.REQUIRED,开启新事物REQUIRED_NEW

这个注解用到了一个切面编程,用了这个注解开启一个事务,函数体处理请求,然后自动提交函数,sessionFactory整合mybaties和spring。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值