spring boot 的事务管理

来源:互联网 发布:淘宝网韩国身体去角质 编辑:程序博客网 时间:2024/06/01 08:20

<?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。


原创粉丝点击