首页 > 编程语言 > 详细

Spring事务管理

时间:2018-08-09 11:51:54      阅读:152      评论:0      收藏:0      [点我收藏+]

---恢复内容开始---

声明式事务

applicationContext.xml配置

第一种,使用tx标签方式

<!-- 配置事务管理器 -->

<bean id="txManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource"  ref="数据源deBean的id"/>
</bean>

<!-- 第一种配置事务的方式 ,tx-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
    <tx:attributes>
     <!-- 设置事务增强的属性 --> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="modify*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice>
<!-- 定义切入点 -->
<aop:config>
     <aop:pointcut id="serviceMethod" expression="execution(* com.dao.*.*(..))"/>
     <aop:advisor pointcut-ref="
serviceMethod" advice-ref="txadvice"/>

</aop:config>
 

Spring事务类型详解:


 


PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。


PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。


PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。


PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。


PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。


PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。


PROPAGATION_NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。


第二种,使用注解方式

<!-- 开启注解来配置的事务 -->
<tx:annotation-driven transaction-manager="txManager"/>

使用@Transactional注解注解类或方法

@Transactional   //注解配置

public class UserServiceImpl implements UserService{}

在类上注解为整个的所有方法都配置了事务

 

@Transactional   //注解配置

public boolean add(Bill bill);

在方法上注解单独为这个方法配置事务

 

 

 

Spring事务管理

原文:https://www.cnblogs.com/boy-Li/p/9447859.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!