Spring与Hibernate整合(二)

来源:互联网 发布:北京师范大学 知乎 编辑:程序博客网 时间:2024/06/15 04:51

2.事务的四种配置方式

  由于事务管理器只需要引用一下sessionFactory,然后供事务调用,所以它就两句话:

   

<bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <!-- 这个name的sessionFactory是实际存在的不可以更改,ref的sessionFactory是与上面的sessionFactory的id对应,可以更改 -->        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>


 

  1)使用tx标签声明事务

   

<!-- 对事务的描述:tx标签 -->    <tx:advice id="txadvice" transaction-manager="transactionManager">        <tx:attributes>            <!-- propagation是值事务隔离级别,rollback-for是指定异常回滚,no-rollback-for是指定异常不回滚 -->            <tx:method name="add*" propagation="REQUIRED"rollback-for="Exception"/>            <tx:method name="modify*" propagation="REQUIRED"no-rollback-for="RuntimeException"/>            <tx:method name="del*" propagation="REQUIRED"/>            <tx:method name="*" read-only="true"/>        </tx:attributes>    </tx:advice>       <!-- 指定哪些方法使用事务,并引入事务描述 -->    <aop:config>        <aop:pointcut expression="execution(* com.dao.*.*(..))" id="daoMethod"></aop:pointcut>        <aop:advisor advice-ref="txadvice" pointcut-ref="daoMethod"/></aop:config>


 

 

  2)使用代理声明事务

   

<!-- 第二种配置事务的方式,代理 -->    <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"abstract="true">         <property name="transactionManager" ref="transactionManager"></property>        <property name="transactionAttributes">            <props>                <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>                <prop key="modify*">PROPAGATION_REQUIRED,+RuntimeException</prop>                <prop key="del*">PROPAGATION_REQUIRED</prop>                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>            </props>        </property>    </bean>        <bean id="userDao" parent="transactionProxy">        <property name="target"ref="userDao"></property></bean>


  3)使用拦截器声明事务

  

  <!-- 第三种配置事务的方式:拦截器 -->    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">        <property name="transactionManager" ref="transactionManager"></property>        <property name="transactionAttributes">            <props>                <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>                <prop key="modify*">PROPAGATION_REQUIRED,+Exception</prop>                <prop key="del*">PROPAGATION_REQUIRED</prop>                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>            </props>        </property>    </bean>        <bean id="proxyFactory" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">        <property name="interceptorNames">            <list>                <value>transactionInterceptor</value>            </list>        </property>        <property name="beanNames">            <list>                <value>*Dao</value>            </list>        </property></bean>


  4)使用注解声明事务

   

<!-- 第四种配置事务的方法:注解 --><!--启用注解 --><context:annotation-config/><tx:annotation-driven transaction-manager="transactionManager"/>


  该方法对Dao的实现有要求,必须使用注解而不是映射文件。

原创粉丝点击