Spring整合Hibernate 二 - 聲明式的事務管理

Spring大戰Hibernate之聲明式的事務管理spring

Spring配置文件:數據庫

添加事務管理類的bean:express

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

該類由Spring提供,若是是hibernate4那就用"org.springframework.orm.hibernate4.HibernateTransactionManager"(Spring2不支持hibernate4)。session

把sessionFactory注入給該bean的sessionFactory屬性spa

配置事務的通知:hibernate

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="getUser" read-only="true" />
        <tx:method name="add*" propagation="REQUIRED"/>
        <tx:method name="save*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

transaction-manager="txManager":這個事務通知所用的事務管理bean是剛纔所定義的管理bean。code

爲名爲 getUse 的方法添加事務。該事務是隻讀的(read-only="true")。orm

名字以 add、save 開頭的方法添加事務。該事務傳播特性爲REQUIRED(propagation="REQUIRED")。xml

事務通知的屬性解釋:blog

一 propagation:事務的傳播特性(支持程度),默認爲REQUIRED
     1.REQUIRED:支持當前事務,若是當前沒有事務,就新建一個事務。這是最多見的選擇。
     2.SUPPORTS:支持當前事務,若是當前沒有事務,就以非事務方式執行。
     3.MANDATORY:支持當前事務,若是當前沒有事務,就拋出異常。
     4.REQUIRES_NEW:新建事務,若是當前存在事務,把當前事務掛起。
     5.NOT_SUPPORTED:以非事務方式執行操做,若是當前存在事務,就把當前事務掛起。
     6.NEVER:以非事務方式執行,若是當前存在事務,則拋出異常。
     7.NESTED:支持當前事務,若是當前事務存在,則執行一個嵌套事務,若是當前沒有事務,就新建一個事務。

二 isolation:事務的隔離級別
    1. ISOLATION_DEFAULT:使用數據庫默認的事務隔離級別。
    2. ISOLATION_READ_UNCOMMITTED:未提交讀。這是事務最低的隔離級別,它充許別外一個事務能夠看到這個事務未提交的數據。這種隔離級別會產生髒讀,不可重複讀和幻像讀。
    3. ISOLATION_READ_COMMITTED :提交讀。保證一個事務修改的數據提交後才能被另一個事務讀取。另一個事務不能讀取該事務未提交的數據。這種事務隔離級別能夠避免髒讀出現,可是可能會出現不可重複讀和幻像讀。
    4. ISOLATION_REPEATABLE_READ :重複讀。這種事務隔離級別能夠防止髒讀,不可重複讀。可是可能出現幻像讀。
    5. ISOLATION_SERIALIZABLE:存列化。這是花費最高代價可是最可靠的事務隔離級別。事務被處理爲順序執行。除了防止髒讀,不可重複讀外,還避免了幻讀。

三 timeout:超時的時間(秒)  -1:不限制

四 rollback-for:指定須要回滾的的異常類型,默認是針對unchecked exception回滾

添加事務的切入點

<aop:config>
    <aop:pointcut id="txPointcut" expression="execution(public * com.startspring.service..*.*(..))" />
    <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" />
</aop:config>

指定切面爲事務通知 txAdvice。指定切入點爲txPointcut。

---------------------------------------------------------------------------------------------------------------

要使用<tx:advice> <aop:config> 等標籤,須要先添加tx、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">

-----------------------------------------------------------------------------------------------------------------

這時候能夠把相應方法裏事務開起和提交的語句去掉,讓Spring來管理事務。如:

public void save(Student student) {
    Session session = sessionFactory.getCurrentSession();
    //session.beginTransaction();
    session.save(student);
    //session.getTransaction().commit();
}

把session.beginTransaction();和session.getTransaction().commit();去掉。若是運做不報錯則事務配置成功。

注意!!必須把hibernate的 hibernate.current_session_context_class 這項屬性去掉!不然事務是不起效的!!也就是hibernate配置文件的<property name="current_session_context_class">thread</property> 或者Spring配置文件的 <prop key="hibernate.current_session_context_class">thread</prop>

相關文章
相關標籤/搜索