Spring配置hibernate事物管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 1、事务管理器,PlatformTransactionManager接口的某个实现类,实现事务的常规提交和回滚操作, Hibernate框架对应的实现类是HibernateTransactionManager,需要注入sessionFactory。 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <!-- 2、事务通知,实现了事务切面操作的环绕通知,负责隐式提供事务支持,需要注入事务管理器和事务策略的配置列表, 事务策略可在TransactionDefinition接口中查找到,将只读方法加上readOnly标签(注意大小写)可
        解决OpenSessionInViewFilter在延迟加载中增删改问题。 -->
    <bean id="transactionAdvice"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager"/>

        <property name="transactionAttributes">
            <props>
                <!-- 1.NOT_SUPPORTED:声明方法不需要事物 2.REQUIRED:业务方法需要在一个事物中运行 3.REQUIRESNEW:不管是否存在事物,业务方法总是会为自己开启一个新事物。
                    4.SUPPORTS:如果业务方法在某个事物中调用,则成为某一事物的一部分。否则不会有事物 5.Never:指定业务方法绝对不能再事物中运行 6.Nested:如果有一个活动的事物存在,则在嵌套的事物中运行,否则按REQUIRED事物执行。 -->

                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="login">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <!-- 3、事务装配器,决定哪些类的哪些方法需要声明式事务支持,抽象事务(粗粒度事务)一般来说都是发生在业务层, 所以我们一般都是给业务层的所有方法提供声明式事务支持。 -->
    <bean id="transactionAdvisor"
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <value>*Service</value>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionAdvice</value>
            </list>
        </property>
    </bean>
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值