---
title: "Spring提供的Hibernate申明式事务管理有两种办法"
author: "jervis"
date: "2005-06-17T17:28:00Z"
tags: []
slug: "spring-hibernate"
draft: false
lastmod: "2005-06-17T17:28:00Z"
id: 32036
url: "https://jervis.iteye.com/blog/32036"
---

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

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

**代码:**&lt;beans&gt;  
        ...  
        &lt;bean id="myTransactionManager"  
            class="org.springframework.orm.hibernate.HibernateTransactionManager"&gt;  
            &lt;property name="sessionFactory"&gt;  
            &lt;ref bean="mySessionFactory"/&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
        &lt;bean id="myTransactionInterceptor"  
                class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
            &lt;property name="transactionManager"&gt;  
                &lt;ref bean="myTransactionManager"/&gt;  
            &lt;/property&gt;  
            &lt;property name="transactionAttributeSource"&gt;  
                &lt;value&gt;  
                    product.ProductService.increasePrice\*=PROPAGATION\_REQUIRED  
                    product.ProductService.someOtherBusinessMethod=PROPAGATION\_MANDATORY  
                &lt;/value&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
        &lt;bean id="myProductServiceTarget" class="product.ProductServiceImpl"&gt;  
            &lt;property name="productDao"&gt;  
                &lt;ref bean="myProductDao"/&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
        &lt;bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean"&gt;  
            &lt;property name="proxyInterfaces"&gt;  
                &lt;value&gt;product.ProductService&lt;/value&gt;  
            &lt;/property&gt;  
            &lt;property name="target"&gt;  
                &lt;ref local="myProductServiceTarget&lt;"/&gt;  
            &lt;/property&gt;  
            &lt;property name="interceptorNames"&gt;  
                &lt;list&gt;  
                &lt;value&gt;myTransactionInterceptor&lt;/value&gt;  
                &lt;/list&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
    &lt;/beans&gt;

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中的例子

**代码:**  
    &lt;beans&gt;  
        ...  
        &lt;bean id="myTransactionManager"  
            class="org.springframework.orm.hibernate.HibernateTransactionManager"&gt;  
            &lt;property name="sessionFactory"&gt;  
                &lt;ref bean="mySessionFactory"/&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
        &lt;bean id="myProductServiceTarget" class="product.ProductServiceImpl"&gt;  
            &lt;property name="productDao"&gt;  
                &lt;ref bean="myProductDao"/&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
        &lt;bean id="myProductService"  
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;  
            &lt;property name="transactionManager"&gt;  
                &lt;ref bean="myTransactionManager"/&gt;  
            &lt;/property&gt;  
            &lt;property name="target"&gt;  
                &lt;ref bean="myProductServiceTarget"/&gt;  
            &lt;/property&gt;  
            &lt;property name="transactionAttributes"&gt;  
                &lt;props&gt;  
                    &lt;prop key="increasePrice\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
                    &lt;prop key="someOtherBusinessMethod"&gt;PROPAGATION\_MANDATORY&lt;/prop&gt;  
                &lt;/props&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
    &lt;/beans&gt;  
   

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

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