Spring事務配置的五種方式和spring裡面事務的傳播屬性和事務隔離級別

阿木俠發表於2017-06-01

轉自:http://blog.csdn.net/it_man/article/details/5074371

Spring事務配置的五種方式

前段時間對Spring的事務配置做了比較深入的研究,在此之間對Spring的事務配置雖說也配置過,但是一直沒有一個清楚的認識。通過這次的學習發覺Spring的事務配置只要把思路理清,還是比較好掌握的。

    總結如下:

    Spring配置檔案中關於事務配置總是由三個組成部分,分別是DataSource、TransactionManager和代理機制這三部分,無論哪種配置方式,一般變化的只是代理機制這部分。

    DataSource、TransactionManager這兩部分只是會根據資料訪問方式有所變化,比如使用Hibernate進行資料訪問時,DataSource實際為SessionFactory,TransactionManager的實現為HibernateTransactionManager。

    具體如下圖:

 

    根據代理機制的不同,總結了五種Spring事務的配置方式,配置檔案如下:

    第一種方式:每個Bean都有一個代理

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

    
<bean id="sessionFactory"  
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        
<property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    
</bean>  

    
<!-- 定義事務管理器(宣告式的事務) -->  
    
<bean id="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>
    
    
<!-- 配置DAO -->
    
<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>
    
    
<bean id="userDao"  
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
           
<!-- 配置事務管理器 -->  
           
<property name="transactionManager" ref="transactionManager" />     
        
<property name="target" ref="userDaoTarget" />  
         
<property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
        
<!-- 配置事務屬性 -->  
        
<property name="transactionAttributes">  
            
<props>  
                
<prop key="*">PROPAGATION_REQUIRED</prop>
            
</props>  
        
</property>  
    
</bean>  
</beans>

    第二種方式:所有Bean共享一個代理基類

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

    
<bean id="sessionFactory"  
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        
<property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    
</bean>  

    
<!-- 定義事務管理器(宣告式的事務) -->  
    
<bean id="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>
    
    
<bean id="transactionBase"  
            class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
            lazy-init
="true" abstract="true">  
        
<!-- 配置事務管理器 -->  
        
<property name="transactionManager" ref="transactionManager" />  
        
<!-- 配置事務屬性 -->  
        
<property name="transactionAttributes">  
            
<props>  
                
<prop key="*">PROPAGATION_REQUIRED</prop>  
            
</props>  
        
</property>  
    
</bean>    
   
    
<!-- 配置DAO -->
    
<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>
    
    
<bean id="userDao" parent="transactionBase" >  
        
<property name="target" ref="userDaoTarget" />   
    
</bean>
</beans>

第三種方式:使用攔截器

<?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:context
="http://www.springframework.org/schema/context"
    xmlns:aop
="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>

    
<bean id="sessionFactory"  
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        
<property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    
</bean>  

    
<!-- 定義事務管理器(宣告式的事務) -->  
    
<bean id="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean> 
   
    
<bean id="transactionInterceptor"  
        class
="org.springframework.transaction.interceptor.TransactionInterceptor">  
        
<property name="transactionManager" ref="transactionManager" />  
        
<!-- 配置事務屬性 -->  
        
<property name="transactionAttributes">  
            
<props>  
                
<prop key="*">PROPAGATION_REQUIRED</prop>  
            
</props>  
        
</property>  
    
</bean>
      
    
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
        
<property name="beanNames">  
            
<list>  
                
<value>*Dao</value>
            </list>  
        
</property>  
        
<property name="interceptorNames">  
            
<list>  
                
<value>transactionInterceptor</value>  
            
</list>  
        
</property>  
    
</bean>  
  
    
<!-- 配置DAO -->
    
<bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>
</beans>

第四種方式:使用tx標籤配置的攔截器

<?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:context
="http://www.springframework.org/schema/context"
    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/context
           http://www.springframework.org/schema/context/spring-context-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"
>

    
<context:annotation-config />
    
<context:component-scan base-package="com.bluesky" />

    
<bean id="sessionFactory"  
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        
<property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    
</bean>  

    
<!-- 定義事務管理器(宣告式的事務) -->  
    
<bean id="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>

    
<tx:advice id="txAdvice" transaction-manager="transactionManager">
        
<tx:attributes>
            
<tx:method name="*" propagation="REQUIRED" />
        
</tx:attributes>
    
</tx:advice>
    
    
<aop:config>
        
<aop:pointcut id="interceptorPointCuts"
            expression
="execution(* com.bluesky.spring.dao.*.*(..))" />
        
<aop:advisor advice-ref="txAdvice"
            pointcut-ref
="interceptorPointCuts" />        
    
</aop:config>      
</beans>

第五種方式:全註解

<?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:context
="http://www.springframework.org/schema/context"
    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/context
           http://www.springframework.org/schema/context/spring-context-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"
>

    
<context:annotation-config />
    
<context:component-scan base-package="com.bluesky" />

    
<tx:annotation-driven transaction-manager="transactionManager"/>

    
<bean id="sessionFactory"  
            class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        
<property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    
</bean>  

    
<!-- 定義事務管理器(宣告式的事務) -->  
    
<bean id="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>
    
</beans>

此時在DAO上需加上@Transactional註解,如下:

package com.bluesky.spring.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import com.bluesky.spring.domain.User;

@Transactional
@Component(
"userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    
public List<User> listUsers() {
        
return this.getSession().createQuery("from User").list();
    }
    
    
}
spring裡面事務的傳播屬性和事務隔離級別

 

一、Propagation (事務的傳播屬性)

Propagation :  key屬性確定代理應該給哪個方法增加事務行為。這樣的屬性最重要的部份是傳播行為。有以下選項可供使用:  

PROPAGATION_REQUIRED--支援當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。

PROPAGATION_SUPPORTS--支援當前事務,如果當前沒有事務,就以非事務方式執行。

PROPAGATION_MANDATORY--支援當前事務,如果當前沒有事務,就丟擲異常。

PROPAGATION_REQUIRES_NEW--新建事務,如果當前存在事務,把當前事務掛起。

PROPAGATION_NOT_SUPPORTED--以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。

PROPAGATION_NEVER--以非事務方式執行,如果當前存在事務,則丟擲異常。

1: PROPAGATION_REQUIRED

加入當前正要執行的事務不在另外一個事務裡,那麼就起一個新的事務

比如說,ServiceB.methodB的事務級別定義為PROPAGATION_REQUIRED, 那麼由於執行ServiceA.methodA的時候,

ServiceA.methodA已經起了事務,這時呼叫ServiceB.methodB,ServiceB.methodB看到自己已經執行在ServiceA.methodA

的事務內部,就不再起新的事務。而假如ServiceA.methodA執行的時候發現自己沒有在事務中,他就會為自己分配一個事務。

這樣,在ServiceA.methodA或者在ServiceB.methodB內的任何地方出現異常,事務都會被回滾。即使ServiceB.methodB的事務已經被

提交,但是ServiceA.methodA在接下來fail要回滾,ServiceB.methodB也要回滾

2: PROPAGATION_SUPPORTS

如果當前在事務中,即以事務的形式執行,如果當前不再一個事務中,那麼就以非事務的形式執行

3: PROPAGATION_MANDATORY

必須在一個事務中執行。也就是說,他只能被一個父事務呼叫。否則,他就要丟擲異常

4: PROPAGATION_REQUIRES_NEW

這個就比較繞口了。 比如我們設計ServiceA.methodA的事務級別為PROPAGATION_REQUIRED,ServiceB.methodB的事務級別為PROPAGATION_REQUIRES_NEW,

那麼當執行到ServiceB.methodB的時候,ServiceA.methodA所在的事務就會掛起,ServiceB.methodB會起一個新的事務,等待ServiceB.methodB的事務完成以後,

他才繼續執行。他與PROPAGATION_REQUIRED 的事務區別在於事務的回滾程度了。因為ServiceB.methodB是新起一個事務,那麼就是存在

兩個不同的事務。如果ServiceB.methodB已經提交,那麼ServiceA.methodA失敗回滾,ServiceB.methodB是不會回滾的。如果ServiceB.methodB失敗回滾,

如果他丟擲的異常被ServiceA.methodA捕獲,ServiceA.methodA事務仍然可能提交。

5: PROPAGATION_NOT_SUPPORTED

當前不支援事務。比如ServiceA.methodA的事務級別是PROPAGATION_REQUIRED ,而ServiceB.methodB的事務級別是PROPAGATION_NOT_SUPPORTED ,

那麼當執行到ServiceB.methodB時,ServiceA.methodA的事務掛起,而他以非事務的狀態執行完,再繼續ServiceA.methodA的事務。

6: PROPAGATION_NEVER

不能在事務中執行。假設ServiceA.methodA的事務級別是PROPAGATION_REQUIRED, 而ServiceB.methodB的事務級別是PROPAGATION_NEVER ,

那麼ServiceB.methodB就要丟擲異常了。

7: PROPAGATION_NESTED

理解Nested的關鍵是savepoint。他與PROPAGATION_REQUIRES_NEW的區別是,PROPAGATION_REQUIRES_NEW另起一個事務,將會與他的父事務相互獨立,

而Nested的事務和他的父事務是相依的,他的提交是要等和他的父事務一塊提交的。也就是說,如果父事務最後回滾,他也要回滾的。

而Nested事務的好處是他有一個savepoint。

*****************************************

ServiceA {

/**

* 事務屬性配置為 PROPAGATION_REQUIRED

*/

void methodA() {

try {

//savepoint

ServiceB.methodB(); //PROPAGATION_NESTED 級別

} catch (SomeException) {

// 執行其他業務, 如 ServiceC.methodC();

}

}

}

********************************************

也就是說ServiceB.methodB失敗回滾,那麼ServiceA.methodA也會回滾到savepoint點上,ServiceA.methodA可以選擇另外一個分支,比如

ServiceC.methodC,繼續執行,來嘗試完成自己的事務。

但是這個事務並沒有在EJB標準中定義。

二、Isolation Level(事務隔離等級):

1、Serializable:最嚴格的級別,事務序列執行,資源消耗最大;

2、REPEATABLE READ:保證了一個事務不會修改已經由另一個事務讀取但未提交(回滾)的資料。避免了“髒讀取”和“不可重複讀取”的情況,但是帶來了更多的效能損失。

3、READ COMMITTED:大多數主流資料庫的預設事務等級,保證了一個事務不會讀到另一個並行事務已修改但未提交的資料,避免了“髒讀取”。該級別適用於大多數系統。

4、Read Uncommitted:保證了讀取過程中不會讀取到非法資料。隔離級別在於處理多事務的併發問題。

我們知道並行可以提高資料庫的吞吐量和效率,但是並不是所有的併發事務都可以併發執行,這需要檢視資料庫教材的可序列化條件判斷了。

這裡就不闡述。

我們首先說併發中可能發生的3中不討人喜歡的事情

1: Dirty reads--讀髒資料。也就是說,比如事務A的未提交(還依然快取)的資料被事務B讀走,如果事務A失敗回滾,會導致事務B所讀取的的資料是錯誤的。

2: non-repeatable reads--資料不可重複讀。比如事務A中兩處讀取資料-total-的值。在第一讀的時候,total是100,然後事務B就把total的資料改成 200,事務A再讀一次,結果就發現,total竟然就變成200了,造成事務A資料混亂。

3: phantom reads--幻象讀資料,這個和non-repeatable reads相似,也是同一個事務中多次讀不一致的問題。但是non-repeatable reads的不一致是因為他所要取的資料集被改變了(比如total的資料),但是phantom reads所要讀的資料的不一致卻不是他所要讀的資料集改變,而是他的條件資料集改變。比如Select account.id where account.name="ppgogo*",第一次讀去了6個符合條件的id,第二次讀取的時候,由於事務b把一個帳號的名字由"dd"改成"ppgogo1",結果取出來了7個資料。

 

 Dirty readsnon-repeatable readsphantom readsSerializable不會不會不會REPEATABLE READ不會不會會READ COMMITTED不會會會Read Uncommitted會會會

 

三、readOnly

事務屬性中的readOnly標誌表示對應的事務應該被最優化為只讀事務。

這是一個最優化提示。在一些情況下,一些事務策略能夠起到顯著的最優化效果,例如在使用Object/Relational對映工具(如:Hibernate或TopLink)時避免dirty checking(試圖“重新整理”)。

四、Timeout

在事務屬性中還有定義“timeout”值的選項,指定事務超時為幾秒。在JTA中,這將被簡單地傳遞到J2EE伺服器的事務協調程式,並據此得到相應的解釋

 

spring裡面事務的傳播屬性和事務隔離級別

 

一、Propagation (事務的傳播屬性)

Propagation :  key屬性確定代理應該給哪個方法增加事務行為。這樣的屬性最重要的部份是傳播行為。有以下選項可供使用:PROPAGATION_REQUIRED--支援當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。

PROPAGATION_SUPPORTS--支援當前事務,如果當前沒有事務,就以非事務方式執行。

PROPAGATION_MANDATORY--支援當前事務,如果當前沒有事務,就丟擲異常。

PROPAGATION_REQUIRES_NEW--新建事務,如果當前存在事務,把當前事務掛起。

PROPAGATION_NOT_SUPPORTED--以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。

PROPAGATION_NEVER--以非事務方式執行,如果當前存在事務,則丟擲異常。

1: PROPAGATION_REQUIRED

加入當前正要執行的事務不在另外一個事務裡,那麼就起一個新的事務

比如說,ServiceB.methodB的事務級別定義為PROPAGATION_REQUIRED, 那麼由於執行ServiceA.methodA的時候,

ServiceA.methodA已經起了事務,這時呼叫ServiceB.methodB,ServiceB.methodB看到自己已經執行在ServiceA.methodA

的事務內部,就不再起新的事務。而假如ServiceA.methodA執行的時候發現自己沒有在事務中,他就會為自己分配一個事務。

這樣,在ServiceA.methodA或者在ServiceB.methodB內的任何地方出現異常,事務都會被回滾。即使ServiceB.methodB的事務已經被

提交,但是ServiceA.methodA在接下來fail要回滾,ServiceB.methodB也要回滾

2: PROPAGATION_SUPPORTS

如果當前在事務中,即以事務的形式執行,如果當前不再一個事務中,那麼就以非事務的形式執行

3: PROPAGATION_MANDATORY

必須在一個事務中執行。也就是說,他只能被一個父事務呼叫。否則,他就要丟擲異常

4: PROPAGATION_REQUIRES_NEW

這個就比較繞口了。 比如我們設計ServiceA.methodA的事務級別為PROPAGATION_REQUIRED,ServiceB.methodB的事務級別為PROPAGATION_REQUIRES_NEW,

那麼當執行到ServiceB.methodB的時候,ServiceA.methodA所在的事務就會掛起,ServiceB.methodB會起一個新的事務,等待ServiceB.methodB的事務完成以後,

他才繼續執行。他與PROPAGATION_REQUIRED 的事務區別在於事務的回滾程度了。因為ServiceB.methodB是新起一個事務,那麼就是存在

兩個不同的事務。如果ServiceB.methodB已經提交,那麼ServiceA.methodA失敗回滾,ServiceB.methodB是不會回滾的。如果ServiceB.methodB失敗回滾,

如果他丟擲的異常被ServiceA.methodA捕獲,ServiceA.methodA事務仍然可能提交。

5: PROPAGATION_NOT_SUPPORTED

當前不支援事務。比如ServiceA.methodA的事務級別是PROPAGATION_REQUIRED ,而ServiceB.methodB的事務級別是PROPAGATION_NOT_SUPPORTED ,

那麼當執行到ServiceB.methodB時,ServiceA.methodA的事務掛起,而他以非事務的狀態執行完,再繼續ServiceA.methodA的事務。

6: PROPAGATION_NEVER

不能在事務中執行。假設ServiceA.methodA的事務級別是PROPAGATION_REQUIRED, 而ServiceB.methodB的事務級別是PROPAGATION_NEVER ,

那麼ServiceB.methodB就要丟擲異常了。

7: PROPAGATION_NESTED

理解Nested的關鍵是savepoint。他與PROPAGATION_REQUIRES_NEW的區別是,PROPAGATION_REQUIRES_NEW另起一個事務,將會與他的父事務相互獨立,

而Nested的事務和他的父事務是相依的,他的提交是要等和他的父事務一塊提交的。也就是說,如果父事務最後回滾,他也要回滾的。

而Nested事務的好處是他有一個savepoint。

*****************************************

ServiceA {

/**

* 事務屬性配置為 PROPAGATION_REQUIRED

*/

void methodA() {

try {

//savepoint

ServiceB.methodB(); //PROPAGATION_NESTED 級別

} catch (SomeException) {

// 執行其他業務, 如 ServiceC.methodC();

}

}

}

********************************************

也就是說ServiceB.methodB失敗回滾,那麼ServiceA.methodA也會回滾到savepoint點上,ServiceA.methodA可以選擇另外一個分支,比如

ServiceC.methodC,繼續執行,來嘗試完成自己的事務。

但是這個事務並沒有在EJB標準中定義。

二、Isolation Level(事務隔離等級):

1、Serializable:最嚴格的級別,事務序列執行,資源消耗最大;

2、REPEATABLE READ:保證了一個事務不會修改已經由另一個事務讀取但未提交(回滾)的資料。避免了“髒讀取”和“不可重複讀取”的情況,但是帶來了更多的效能損失。

3、READ COMMITTED:大多數主流資料庫的預設事務等級,保證了一個事務不會讀到另一個並行事務已修改但未提交的資料,避免了“髒讀取”。該級別適用於大多數系統。

4、Read Uncommitted:保證了讀取過程中不會讀取到非法資料。隔離級別在於處理多事務的併發問題。

我們知道並行可以提高資料庫的吞吐量和效率,但是並不是所有的併發事務都可以併發執行,這需要檢視資料庫教材的可序列化條件判斷了。

這裡就不闡述。

我們首先說併發中可能發生的3中不討人喜歡的事情

1: Dirty reads--讀髒資料。也就是說,比如事務A的未提交(還依然快取)的資料被事務B讀走,如果事務A失敗回滾,會導致事務B所讀取的的資料是錯誤的。

2: non-repeatable reads--資料不可重複讀。比如事務A中兩處讀取資料-total-的值。在第一讀的時候,total是100,然後事務B就把total的資料改成 200,事務A再讀一次,結果就發現,total竟然就變成200了,造成事務A資料混亂。

3: phantom reads--幻象讀資料,這個和non-repeatable reads相似,也是同一個事務中多次讀不一致的問題。但是non-repeatable reads的不一致是因為他所要取的資料集被改變了(比如total的資料),但是phantom reads所要讀的資料的不一致卻不是他所要讀的資料集改變,而是他的條件資料集改變。比如Select account.id where account.name="ppgogo*",第一次讀去了6個符合條件的id,第二次讀取的時候,由於事務b把一個帳號的名字由"dd"改成"ppgogo1",結果取出來了7個資料。

 

事務屬性中的readOnly標誌表示對應的事務應該被最優化為只讀事務。


讀髒資料(Dirty reads) 不可重複讀(non-repeatable reads) 幻象讀資料(phantom reads) Serializable 不會 不會 不會 REPEATABLE READ 不會 不會 會 READ COMMITTED 不會 會 會 Read Uncommitted 會 會 會

三、readOnly

這是一個最優化提示。在一些情況下,一些事務策略能夠起到顯著的最優化效果,例如在使用Object/Relational對映工具(如:Hibernate或TopLink)時避免dirty checking(試圖“重新整理”)。

四、Timeout

在事務屬性中還有定義“timeout”值的選項,指定事務超時為幾秒。在JTA中,這將被簡單地傳遞到J2EE伺服器的事務協調程式,並據此得到相應的解釋

 

 

20110112

資料庫提供了四種事務隔離級別, 不同的隔離級別採用不同的鎖類開來實現. 

在四種隔離級別中, Serializable的級別最高, Read Uncommited級別最低. 

大多數資料庫的預設隔離級別為: Read Commited,如Sql Server , Oracle. 

少數資料庫預設的隔離級別為Repeatable Read, 如MySQL InnoDB儲存引擎 

即使是最低的級別,也不會出現 第一類 丟失 更新問題 .  

1. 髒讀(事務沒提交,提前讀取) :髒讀就是指當一個事務正在訪問資料,並且對資料進行了修改,而這種修改還沒有提交到資料庫中,這時,另外一個事務也訪問這個資料,然後使用了這個資料。

2. 不可重複讀(兩次讀的不一致) :是指在一個事務內,多次讀同一資料。在這個事務還沒有結束時,另外一個事務也訪問該同一資料。那麼,在第一個事務中的兩次讀資料之間,由於第二個事務的修改,那麼第一個事務兩次讀到的的資料可能是不一樣的。這樣就發生了在一個事務內兩次讀到的資料是不一樣的,因此稱為是不可重複讀。例如,一個編輯人員兩次讀取同一文件,但在兩次讀取之間,作者重寫了該文件。當編輯人員第二次讀取文件時,文件已更改。原始讀取不可重複。如果只有在作者全部完成編寫後編輯人員才可以讀取文件,則可以避免該問題。 
3. 幻讀 : 是指當事務不是獨立執行時發生的一種現象,例如第一個事務對一個表中的資料進行了修改,這種修改涉及到表中的全部資料行。同時,第二個事務也修改這個表中的資料,這種修改是向表中插入一行新資料。那麼,以後就會發生操作第一個事務的使用者發現表中還有沒有修改的資料行,就好象發生了幻覺一樣。例如,一個編輯人員更改作者提交的文件,但當生產部門將其更改內容合併到該文件的主複本時,發現作者已將未編輯的新材料新增到該文件中。如果在編輯人員和生產部門完成對原始文件的處理之前,任何人都不能將新材料新增到文件中,則可以避免該問題。 
4. 第一類更新丟失(回滾丟失) : 
  當2個事務更新相同的資料來源,如果第一個事務被提交,而另外一個事務卻被撤銷,那麼會連同第一個事務所做的跟新也被撤銷。也就是說第一個事務做的跟新丟失了。 
5. 第二類更新丟失(覆蓋丟失) : 
  第二類更新丟失實在實際應用中經常遇到的併發問題,他和不可重複讀本質上是同一類併發問題,通常他被看做不可重複讀的特例:當2個或這個多個事務查詢同樣的記錄然後各自基於最初的查詢結果更新該行時,會造成第二類丟失更新。因為每個事務都不知道不知道其他事務的存在,最後一個事務對記錄做的修改將覆蓋其他事務對該記錄做的已提交的跟新... 
補充 : 基於後設資料的 Spring 宣告性事務 : 

Isolation 屬性一共支援五種事務設定,具體介紹如下: 

l          DEFAULT 使用資料庫設定的隔離級別 ( 預設 ) ,由 DBA 預設的設定來決定隔離級別 . 

l          READ_UNCOMMITTED 會出現髒讀、不可重複讀、幻讀 ( 隔離級別最低,併發效能高 ) 

l          READ_COMMITTED  會出現不可重複讀、幻讀問題(鎖定正在讀取的行) 

l          REPEATABLE_READ 會出幻讀(鎖定所讀取的所有行) 

l          SERIALIZABLE 保證所有的情況不會發生(鎖表) 

不可重複讀的重點是修改 : 
同樣的條件 ,   你讀取過的資料 ,   再次讀取出來發現值不一樣了 
幻讀的重點在於新增或者刪除 
同樣的條件 ,   第 1 次和第 2 次讀出來的記錄數不一樣 

相關文章