事務傳播行爲種類spring
Spring在TransactionDefinition接口中規定了7種類型的事務傳播行爲,它們規定了事務方法和事務方法發生嵌套調用時事務如何進行傳播:數據庫
事務傳播行爲類型dom |
說明spa |
PROPAGATION_REQUIREDhibernate |
若是當前沒有事務,就新建一個事務,若是已經存在一個事務中,加入到這個事務中。這是最多見的選擇。代理 |
PROPAGATION_SUPPORTSorm |
支持當前事務,若是當前沒有事務,就以非事務方式執行。接口 |
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");