您的位置:首页 > 编程语言 > Java开发

Spring配置hibernate事物管理

2017-05-20 10:35 549 查看
<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: