`

事务传播行为种类

 
阅读更多
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");

 

 

分享到:
评论

相关推荐

    Spring在Transaction事务传播行为种类

    Spring在Transaction事务传播行为种类,希望对大家有所帮助

    Spring.html

    传播行为:A--&gt;B,在B上声明是否一定需要事务管理 requerd:必须的(默认),如果A有事务那么就加入A的事务,如果A没有事务那么单独创建一个事务 supports,如果A有事务则加入,如果没有就算了 隔离级别 default:...

    《计算机应用基础》.docx

    《计算机应用基础》全文共2页,当前为第1页。《计算机应用基础》全文共2... (7)、网络通信 利用计算机网络实现信息的传递、交换和传播。 五、课后作业: 1、计算机分类? 2、计算机的组成有哪些? 《计算机应用基础》

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

    4.3 事务隔离、传播属性的设置 198 4.3.1 并发访问和隔离 198 4.3.2 事务属性 199 4.4 EJB的事务管理 201 4.4.1 容器管理事务(CMT) 201 4.4.2 Bean管理事务(BMT) 201 4.5 事务超时设置 201 4.6 本章小结 203 第5章 ...

    Hibernate+中文文档

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识...

    hibernate 框架详解

    传播性持久化(transitive persistence) 11.12. 使用元数据 12. 事务和并发 12.1. Session和事务范围(transaction scopes) 12.1.1. 操作单元(Unit of work) 12.1.2. 应用程序事务(Application transactions) ...

    最全Hibernate 参考文档

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scopes) 11.1.1. 操作单元(Unit of work) 11.1.2. 应用程序事务(Application transactions) ...

    Hibernate3+中文参考文档

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scopes) 11.1.1. 操作单元(Unit of work) 11.1.2. 应用程序事务(Application transactions) ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识...

    HibernateAPI中文版.chm

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识...

    hibernate3.04中文文档.chm

    11.11. 传播性持久化(transitive persistence) 11.12. 使用元数据 12. 事务和并发 12.1. Session和事务范围(transaction scopes) 12.1.1. 操作单元(Unit of work) 12.1.2. 应用程序事务(Application ...

    hibernate3.2中文文档(chm格式)

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识...

    Hibernate教程

    11.11. 传播性持久化(transitive persistence) 11.12. 使用元数据 12. 事务和并发 12.1. Session和事务范围(transaction scopes) 12.1.1. 操作单元(Unit of work) 12.1.2. 应用程序事务(Application ...

    Hibernate参考文档

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering ...

    hibernate 体系结构与配置 参考文档(html)

    传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering ...

    网上书店系统源码

     开办网上书店系统也可以为在校大学生提供方便、廉价、高效的书店方式,促进文化传播,帮助国家和学校培养优秀的人才,具有长远的社会效益和经济效益。 第二章 系统的技术综述 2.1 Browser/Server结构  网上书店...

    Hibernate 中文 html 帮助文档

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering ...

    Hibernate中文详细学习文档

    10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识...

    青果校园兼职网,阿赛企业网站管理

    4、《阿赛企业网站系统》所有程序版权均归阿赛工作室所有,藉由江苏律师事务所提供全程法律支持。 阿赛企业网站系统AsaiCoEV6 通用说明书 一、系统简介 程序名称:阿赛企业网站系统 程序版本:V6 英文名称:...

Global site tag (gtag.js) - Google Analytics