---
title: "Spring结合Hibernate声明式事务配置"
author: "freeglad"
date: "2009-03-02T17:58:00Z"
tags: []
slug: "spring-hibernate"
draft: false
lastmod: "2009-03-02T17:58:00Z"
id: 586398
url: "https://freeglad.iteye.com/blog/586398"
---

spring提供的事务管理可以分为两类：编程式的和声明式的。编程式的，比较灵活，但是代码量大，存在重复的代码比较多；声明式的比编程式的更灵活。很多时候都会用到他的声明式事务，简单的在配置文件中进行一些规则配置，利用Spring的AOP功能就能轻松搞定事务问题；这里面涉及到一个事务的传播属性问题【Propagation】，他在TransactionDefinition接口中定义，共有7种选项可用：

PROPAGATION\_REQUIRED：支持当前事务，如果当前没有事务，就新建一个事务。这是最常见的选择。  
PROPAGATION\_SUPPORTS：支持当前事务，如果当前没有事务，就以非事务方式执行。  
PROPAGATION\_MANDATORY：支持当前事务，如果当前没有事务，就抛出异常。  
PROPAGATION\_REQUIRES\_NEW：新建事务，如果当前存在事务，把当前事务挂起。  
PROPAGATION\_NOT\_SUPPORTED：以非事务方式执行操作，如果当前存在事务，就把当前事务挂起。  
PROPAGATION\_NEVER：以非事务方式执行，如果当前存在事务，则抛出异常。  
PROPAGATION\_NESTED：支持当前事务，新增Savepoint点，与当前事务同步提交或回滚。

以下为一例：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans  
xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;

&lt;bean id="dataSource"  
class="org.apache.commons.dbcp.BasicDataSource"&gt;  
&lt;property name="driverClassName"  
value="oracle.jdbc.driver.OracleDriver"&gt;  
&lt;/property&gt;  
&lt;property name="url"  
value="jdbc:oracle:thin:@192.168.1.1:1521:ASDF"&gt;  
&lt;/property&gt;  
&lt;property name="username" value="ASDFF"&gt;&lt;/property&gt;  
&lt;property name="password" value="ASDFF"&gt;&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="sessionFactory"  
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
&lt;property name="dataSource"&gt;  
&lt;ref bean="dataSource" /&gt;  
&lt;/property&gt;  
&lt;property name="hibernateProperties"&gt;  
&lt;props&gt;  
&lt;prop key="hibernate.dialect"&gt;  
org.hibernate.dialect.Oracle9Dialect  
&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;property name="mappingResources"&gt;  
&lt;list&gt;  
&lt;value&gt;.../vo/Attachment.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Author.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Docs.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/FileForder.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Files.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Users.hbm.xml&lt;/value&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- DAO接口的实现类 --&gt;  
&lt;bean id="fileDaoTarget" class=".......dao.FileDAO"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- Serv接口的实现类 --&gt;  
&lt;bean id="fileService" class="......serv.FileService"&gt;  
&lt;property name="fileDao"&gt;  
&lt;ref bean="fileDao"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;bean id="fileDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;  
&lt;property name="proxyInterfaces"&gt;  
&lt;list&gt;

&lt;!-- 代理接口 --&gt;  
&lt;value&gt;.......dao.IFileDAO&lt;/value&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;property name="transactionManager"&gt;  
&lt;ref bean="transactionManager"/&gt;  
&lt;/property&gt;  
&lt;property name="target"&gt;  
&lt;ref bean="fileDaoTarget"/&gt;  
&lt;/property&gt;  
&lt;property name="transactionAttributeSource"&gt;  
&lt;ref bean="transactionAttributeSource"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- 事务属性配置 --&gt;  
&lt;bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"&gt;  
&lt;property name="properties"&gt;  
&lt;props&gt;  
&lt;prop key="edit\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
&lt;prop key="insert\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
&lt;prop key="get\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
&lt;prop key="load\*"&gt;PROPAGATION\_REQUIRED,readOnly&lt;/prop&gt;  
&lt;prop key="delete\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;

使用AOP：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans  
xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;

&lt;bean id="dataSource"  
class="org.apache.commons.dbcp.BasicDataSource"&gt;  
&lt;property name="driverClassName"  
value="oracle.jdbc.driver.OracleDriver"&gt;  
&lt;/property&gt;  
&lt;property name="url"  
value="jdbc:oracle:thin:@192.168.1.1:1521:asdf"&gt;  
&lt;/property&gt;  
&lt;property name="username" value="asdf"&gt;&lt;/property&gt;  
&lt;property name="password" value="asdf"&gt;&lt;/property&gt;  
&lt;/bean&gt;  
&lt;bean id="sessionFactory"  
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
&lt;property name="dataSource"&gt;  
&lt;ref bean="dataSource" /&gt;  
&lt;/property&gt;  
&lt;property name="hibernateProperties"&gt;  
&lt;props&gt;  
&lt;prop key="hibernate.dialect"&gt;  
org.hibernate.dialect.Oracle9Dialect  
&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;property name="mappingResources"&gt;  
&lt;list&gt;  
&lt;value&gt;.../vo/Attachment.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Author.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Docs.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/FileForder.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Files.hbm.xml&lt;/value&gt;  
&lt;value&gt;.../vo/Users.hbm.xml&lt;/value&gt;  
&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;tx:advice id="txAdvice"&gt;  
&lt;tx:attributes&gt;  
&lt;tx:method name="get\*" read-only="true"/&gt;  
&lt;tx:method name="\*"/&gt;  
&lt;/tx:attributes&gt;  
&lt;/tx:advice&gt;

&lt;aop:config&gt;  
&lt;aop:pointcut id="daoOperation" expression="execution(* dao.\*.\*(..))"/&gt;  
&lt;aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation"/&gt;  
&lt;/aop:config&gt;

&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;bean id="fileDao" class="..dao.FileDAO"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;bean id="fileService" class="..FileService"&gt;  
&lt;property name="fileDao"&gt;  
&lt;ref bean="fileDao"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;/beans&gt;

事务异常的捕捉及处理可以在切入点的包围进行，如：

@Transaction

public boolean saveFile(Files file, Files file2)  
{  
this.save(file);  
this.save(file2);  
return true;  
}

捕捉异常：

public boolean saveFile(Files file, Files file1)

{  
try{  
return this.getFileDao().saveFile(file, file1);  
}catch(Exception e)  
{  
log.error(e);

return false;  
}  
}