Spring与Hibernate整合(二)

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的实现有要求,必须使用注解而不是映射文件。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值