Spring結合Hibernate聲明式事務配置

spring提供的事務管理可以分爲兩類:編程式的和聲明式的。編程式的,比較靈活,但是代碼量大,存在重複的代碼比較多;聲明式的比編程式的更靈活。很多時候都會用到他的聲明式事務,簡單的在配置文件中進行一些規則配置,利用Spring的AOP功能就能輕鬆搞定事務問題;這裏面涉及到一個事務的傳播屬性問題【Propagation】,他在TransactionDefinition接口中定義,共有7種選項可用:

PROPAGATION_REQUIRED:支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
PROPAGATION_SUPPORTS:支持當前事務,如果當前沒有事務,就以非事務方式執行。
PROPAGATION_MANDATORY:支持當前事務,如果當前沒有事務,就拋出異常。
PROPAGATION_REQUIRES_NEW:新建事務,如果當前存在事務,把當前事務掛起。
PROPAGATION_NOT_SUPPORTED:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
PROPAGATION_NEVER:以非事務方式執行,如果當前存在事務,則拋出異常。
PROPAGATION_NESTED:支持當前事務,新增Savepoint點,與當前事務同步提交或回滾。

以下爲一例:

<?xml version="1.0" encoding="UTF-8"?>
<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">

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

<!-- DAO接口的實現類 -->
<bean id="fileDaoTarget" class=".......dao.FileDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- Serv接口的實現類 -->
<bean id="fileService" class="......serv.FileService">
<property name="fileDao">
<ref bean="fileDao"/>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<bean id="fileDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>

<!-- 代理接口 -->
<value>.......dao.IFileDAO</value>
</list>
</property>
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="fileDaoTarget"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>

<!-- 事務屬性配置 -->
<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>

使用AOP:

<?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: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"
>

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

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="daoOperation" expression="execution(* dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation"/>
</aop:config>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<bean id="fileDao" class="..dao.FileDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<bean id="fileService" class="..FileService">
<property name="fileDao">
<ref bean="fileDao"/>
</property>
</bean>

</beans>

事務異常的捕捉及處理可以在切入點的包圍進行,如:

@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;
}
}

發佈了96 篇原創文章 · 獲贊 1 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章