欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring提供的Hibernate申明式事务管理有两种办法 HibernateSpringBeanAOPORM 

程序员文章站 2022-07-12 18:31:48
...

Spring提供的Hibernate申明式事务管理有两种办法

a) 配合使用org.springframework.transaction.interceptor.TransactionInterceptor和org.springframework.orm.hibernate.HibernateTransactionManager,下面是spring reference的例子

代码:
<beans>
        ...
        <bean id="myTransactionManager"
            class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory">
            <ref bean="mySessionFactory"/>
            </property>
        </bean>
        <bean id="myTransactionInterceptor"
                class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager">
                <ref bean="myTransactionManager"/>
            </property>
            <property name="transactionAttributeSource">
                <value>
                    product.ProductService.increasePrice*=PROPAGATION_REQUIRED
                    product.ProductService.someOtherBusinessMethod=PROPAGATION_MANDATORY
                </value>
            </property>
        </bean>
        <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
            <property name="productDao">
                <ref bean="myProductDao"/>
            </property>
        </bean>
        <bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces">
                <value>product.ProductService</value>
            </property>
            <property name="target">
                <ref local="myProductServiceTarget<"/>
            </property>
            <property name="interceptorNames">
                <list>
                <value>myTransactionInterceptor</value>
                </list>
            </property>
        </bean>
    </beans>


HibernateInterceptor和事务无关,它的用途在javadocs中描述如下:
引用:
This interceptor binds a new Hibernate Session to the thread before a method
call, closing and removing it afterwards in case of any method outcome.
If there already was a pre-bound Session (e.g. from HibernateTransactionManager,
or from a surrounding Hibernate-intercepted method), the interceptor simply
takes part in it.


b)使用TransactionProxyFactoryBean,下面是Spring Reference中的例子
代码:

    <beans>
        ...
        <bean id="myTransactionManager"
            class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref bean="mySessionFactory"/>
            </property>
        </bean>
        <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
            <property name="productDao">
                <ref bean="myProductDao"/>
            </property>
        </bean>
        <bean id="myProductService"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref bean="myTransactionManager"/>
            </property>
            <property name="target">
                <ref bean="myProductServiceTarget"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="increasePrice*">PROPAGATION_REQUIRED</prop>
                    <prop key="someOtherBusinessMethod">PROPAGATION_MANDATORY</prop>
                </props>
            </property>
        </bean>
    </beans>
   


在没有其他AOP interceptor情况下,使用TransactionProxyFactoryBean是比较方便的。
事务划分一般是的业务层,而不是在DAO一层。

2.代理工厂返回的是接口AddressDao的应用,通过接口最终调用target的方法。
3.TransactionDefinition定义了所有的事务属性