SSH、IBatis等框架相關知識

Spring的事務

    事務使用ACID特性來衡量事務的質量。這些特性包括原子性、一致性

隔離性和持久性。

    a.原子性:事務必須是原子的。不可分割的,在事務結束的時候,事務中的所有任務必須全部成功完成或者全部任務失敗,事務回滾到事務開始之前的狀態

    b.一致性:數據庫中的所有數據必須和現實保持一致。

    c.隔離性:事務與事務之間的屏障。每個事務必須與其他事務的執行結果隔離開,直到該事務執行完畢。

    d.持久性:如果事務成功執行,無論系統發生什麼情況,事務的持久性必須保證事務的執行結果是永存的。

    在事務處理中有違反ACID特性的三個問題:髒讀、不可重複讀和幻讀。

    髒讀:當一個事務讀取了一個事務尚未提交的更新,就叫作髒讀取。

    不可重複讀:在一個事務中執行多次同樣的查詢操作,但每次查詢的結果都不相同,叫不可重複讀

    幻讀:一個事務的更新結果影響到了另一個事務的問題。

    Spring的事務管理器有5個,也即DataSourceTransactionManager(JDBC事務管理器),HibernateTransactionManager(Hibernate事務管理器),JdoTransactionManager(JDO事務管理器),JtaTransactionManager(Jta事務管理器)以及Apache的OJB事務管理器。

    Spring支持聲明式事務,並建議這樣做。因爲Spring中的事務是基於AOP實現的,而Spring的AOP是以方法爲單位的,所以Spring的事務屬性就是對事務應用到方法的策略描述,這些屬性分別爲:傳播行爲、隔離級別、只讀和超時屬性。

    事務的傳播行爲是事務應用於方法的邊界,它定義了事務的建立、暫停等行爲。

    事務的隔離級別:爲了解決事務之間的3個缺陷,必須在事務之間建立隔離關係來保障事務的完整性。

    事務的只讀屬性:在數據庫的操作中,查詢是使用最頻繁的操作,每次執行查詢操作時都要從數據庫中重新讀取數據,有時多次讀取的數據都是相同的,這樣的操作浪費了系統資源和影響了系統速度。如果將食物聲明爲只讀,那麼數據庫可以根據事務的特性優化事務的讀取操作。事務的只讀屬性需要配合事務的PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW和PROPAGATION_NESTED傳播行爲共同設置。

    事務的超時屬性:設置了事務的超時時間。

    Spring的聲明式事務不涉及組建依賴關係,它通過AOP實現事務管理。在使用Spring的聲明式事務時不需要編寫任何代碼。使用事務代理工廠來管理事務。事務代理工廠TransactionProxyFactoryBean包含了事務攔截器、目標代理和事務的屬性設置。

    Spring使用註解式進行事務管理,配置示例

    步驟一、在Spring配置文件中引入<tx:>命名空間

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:aop="http://www.springframework.org/schema/aop"  
 xmlns:tx="http://www.springframework.org/schema/tx"  
 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-3.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/aop  
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    步驟二、具有@Transactional註解的bean自動配置爲聲明式事務

<!-- 事務管理 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     	<property name="dataSource" ref="dataSource"></property>
     </bean>
     <!-- 配置事務增強器 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<!-- 配置詳細事務處理語義 -->
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="saveOrUpdate*" propagation="REQUIRED"/>
			<tx:method name="get*" propagation="SUPPORTS"/>
			<tx:method name="find*" propagation="SUPPORTS"/>
			<tx:method name="load*" propagation="SUPPORTS"/>
			<tx:method name="select*" propagation="SUPPORTS"/>
			<!-- 其他採用默認方式 -->
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>
	<!-- SpringAOP事務管理 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.jf.service..*impl.*(..))" id="transactionPointCut"/>
		<!-- 指定txAdvice切入點應用txAcvice食物增強處理 -->
		<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointCut"/>
	</aop:config>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章