PROPAGATION_REQUIRED

(2009-05-13 13:26:52)

 

事务传播行为种类

Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播:

表1事务传播行为类型

事务传播行为类型

说明

PROPAGATION_REQUIRED

如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。

PROPAGATION_SUPPORTS

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

PROPAGATION_MANDATORY

使用当前的事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW

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

PROPAGATION_NOT_SUPPORTED

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

PROPAGATION_NEVER

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

PROPAGATION_NESTED

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

当使用PROPAGATION_NESTED时,底层的数据源必须基于JDBC 3.0,并且实现者需要支持保存点事务机制。

<!--Hibernate事务管理器-->
<bean id="transactionManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref bean="sessionFactory" />
   </property>
</bean>

<!-- 定义事务拦截器bean-->
<bean id="transactionInterceptor"
   class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <!-- 事务拦截器bean需要依赖注入一个事务管理器-->
   <property name="transactionManager" ref="transactionManager" />
   <property name="transactionAttributes">
    <!-- 下面定义事务传播属性-->
    <props>
     <prop key="save*">PROPAGATION_REQUIRED</prop>
     <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="delete*">PROPAGATION_REQUIRED</prop>
     <prop key="update*">PROPAGATION_REQUIRED</prop>
     <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
   </property>
</bean>

<bean id="managerTemplate" abstract="true" lazy-init="true">
<property name="teamDao">
   <ref bean="teamDao" />
</property>
<property name="studentDao">
   <ref bean="studentDao" />
</property>    
</bean>

<bean id ="manager" class="com.zd.service.impl.Manager" parent="managerTemplate" />

<!-- 定义BeanNameAutoProxyCreator-->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
     <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
     <property name="beanNames">
            <!-- 下面是所有需要自动创建事务代理的bean-->
            <list>
                <value>manager</value>
            </list>
            <!-- 此处可增加其他需要自动创建事务代理的bean-->
     </property>
        <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
        <property name="interceptorNames">
            <list>
                <!-- 此处可增加其他新的Interceptor -->
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>

<!-- 基本数据库操作 -->
<bean id="baseDao" class="com.zd.service.impl.BaseDao">
    <property name="hibernateTemplate">
      <ref bean="hibernateTemplate"/>
    </property>
</bean>

<!-- 班级 -->
<bean id="teamDao" class="com.zd.service.impl.TeamDao">
    <property name="baseDao">
       <ref bean="baseDao" />
    </property>
</bean>

<!-- 学生 -->
<bean id="studentDao" class="com.zd.service.impl.StudentDao">
    <property name="baseDao">
       <ref bean="baseDao" />
    </property>
</bean>

public void testSaveTeam() {
   Team team = new Team();
   team.setTeamId(DBKeyCreator.getRandomKey(12));
   team.setTeamName("Class CCC");
   IManager manager = (IManager) SpringContextUtil.getContext().getBean("manager");

Student student = new Student();
   student.setStudentId(DBKeyCreator.getRandomKey(13));
   student.setSex(Student.SEX_FEMALE);
   student.setStudentName("Tom");
   student.setTeamId("60FHDXDIG5JQ");
   manager.saveTeamAndStu(team, student);
   System.out.println("Save Team and Student Success");

PROPAGATION_REQUIRED的更多相关文章

  1. Transaction事务传播行为种类PROPAGATION_REQUIRED

    事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 表1事务传播行为类型 事务传 ...

  2. spring 中事务的PROPAGATION_REQUIRED,Readonly的解释

                 一.事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播 ...

  3. PROPAGATION_REQUIRED事务管理

    转载:http://blog.csdn.net/l345480242/article/details/7588393 采用编程式事务 1. getCurrentSession()与openSessio ...

  4. Spring事务的传播:PROPAGATION_REQUIRED

    PROPAGATION_REQUIRED-- 支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. ServiceA { void methodA() { ServiceB.method ...

  5. Spring中 PROPAGATION_REQUIRED 解释 事物是在一个方法里调用其他的方法,一起成功或者一起失败,是方法之间的关系,而不是某一个方法内部的问题。而且要以抛异常的方式来表明方法的失败,以此来导致事物起作用,大家全失败。

    事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播: 事务传播行为类型 事务传播 ...

  6. Spring7种事务传播行为类型--PROPAGATION_REQUIRED及其他6种事务传播行为种类

    PROPAGATION_REQUIRED及其他6种事务传播行为种类,有需要的朋友可以参考下. Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务 ...

  7. spring事务传播特性实验(2):PROPAGATION_REQUIRED实验结果与分析

    本文延续上一文章(spring事务传播特性实验(1):数据准备),在已经准备好环境的情况下,做如下的实验,以验证spring传播特性,加深对spring传播特性的理解. 本次主要验证PROPAGATI ...

  8. Spring中 PROPAGATION_REQUIRED 解释

    转自:https://blog.csdn.net/bigtree_3721/article/details/53966617 事务传播行为种类 Spring在TransactionDefinition ...

  9. PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.Exception

    转https://stackoverflow.com/questions/29117679/spring-transactional-management-propagation-required-i ...

随机推荐

  1. 对武汉-and-IT软件开发的看法

    本编是一个武汉农村娃子,2015年毕业到现在算上实习 差不多快三年的时间了.在软件行业混的也就一般水平,从开心在学校学习的winform+DBHelper 的开发模式,到现在MVC+EF 的开发模式. ...

  2. border-radius 知识点

    border-radius:50px; 边框半径 CSS度量值都:em.px.百分比如果设置1个值,表示4个圆角都使用这个值.如果设置两个值,表示左上角和右下角使用第一个值,右上角和左下角使用第二个值 ...

  3. 【Web】十步教你搭建完整免费的个人网站(花生壳+XAMPP+WordPress)

    1.从花生壳官网(http://www.oray.com/peanuthull/download.php)下载最新版本的客户端. 下载完成后安装,注册护照(需手机验证码验证),注册完成后获取免费域名并 ...

  4. myeclipse 配置weblogic 异常

    java.lang.UnsupportedClassVersionError: Bad version number in .class file当前JDK与weblogic版本不匹配.

  5. JS封装移动端触摸滑动插件应用于导航banner【精装版】

    自己封装了一个小插件,一个小尝试. 可用于类似于京东导航等效果,效果多样化,很方便. 欢迎大家提点意见. mrChenSwiper(  {parent, child, type, parentN, c ...

  6. 删除Lb重复的数,用La输出(顺序表)

    #include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCRMENT=10; type ...

  7. PHP新手必须掌握的入门与实战技巧

    作为当今主流的开发语言,PHP集简单.免费.高效等特点于一身.对于想加入PHP大军的新手来说,从何学起.如何学习? 你需要掌握PHP的基础知识.常用功能模块.面向对象.MVC等相关技能.学会了这些技能 ...

  8. Yii2.0中文开发向导——删除数据

    直接 model 删除 $model = User::find($id); $model->delete(); 带有条件的删除 $connection ->createCommand() ...

  9. JAVA中把ResultSet转换成LIST

    项目中老是遇到数据库异常关闭的情况,真烦, 想用hibernate呢,那个玩意儿又太笨重,感慨C#和PHP的舒适方便性,模拟TP写了个数据处理层,将就用着先代码里有很多项目中的东西,不要直接COPY了 ...

  10. Symfony框架系列----1.入门安装

    一.安装    (1)Composer安装(可选) $ curl -s https://getcomposer.org/installer | php $ php composer.phar crea ...