spring事務管理
zhuanzaizi:z_xiaofei168
6.1、spring的事務管理器
Spring框架并沒有直接管理用戶的應用系統中的事務,它只是提供許多供用戶選擇的事務管理器,然后將事務管理的責任委托給與此事務管理器對應的持久化技術的事務實現。
事務管理實現 |
使用時機 |
org.springframework.jdbc.datasource. DataSourceTransactionManager |
在單一的JDBC DataSource中管理事務 |
org.springframework.orm.hibernate3. HibernateTransactionManager |
當持久化機制是Hibernate時,用它來管理職務 |
org.springframework.orm. jpa.JpaTransactionManager |
當JPA用作持久化時,用它來管理職務 |
org.springframework.transaction. jta.JtaTransactionManager |
使用一個JTA實現來管理事務。在一個事務跨越多個資源時必須使用 |
配置文件中的配置如下:
<bean id=”transactionManager” class=” org.springframework.jdbc.datasource. DataSourceTransactionManager”>
<property name=”dataSource” ref=” dataSource”/>
</bean>
6.2、事務屬性介紹
1>.傳播行為
傳播行為 |
說明 |
PROPAGATION_REQUIRED |
必須在一個事務中執行。如果當前有一個事務正在進行,該方法將會在那個事務中執行。否則要開始一個新事務。Spring事務傳播行為的默認值。 |
PROPAGATION_SUPPORTS |
支持現有的事務。如果當前沒有事務在進行,就以非事務的方式執行 |
PROPAGATION_MANDATORY |
方法必須在一個現有事務中進行,否則會拋出異常。 |
PROPAGATION_REQUIRES_NEW |
必須在它自己的新啟事務里進行。如果現有事務在進行就先暫停它 |
PROPAGATION_NOT_SUPPORTED |
不應在事務中進行。如果現有事務在進行就先暫停它 |
PROPAGATION_NEVER |
不應在事務中進行。如果現有事務在進行就拋出異常 |
PROPAGATION_NESTED |
如果現有事務正在進行,則該方法運行在一個嵌套式事務中。否則PROPAGATION_REQUIRED執行 |
2>.隔離級別
隔離級別 |
說明 |
ISOLATION_DEFAULT |
使用底層數據庫默認的隔離級別spring事務隔離級別的默認值 |
ISOLATION_READ_UNCOMMITED |
充許另一個事務可以讀到這個事務未提交的數據可能導致臟讀、不可重復讀和幻讀。 |
ISOLATION_READ_COMMITED |
保證一個事務修改的數據提交后才能被另一個事務讀取可能導致不可重復讀和幻讀。 |
ISOLATION_REPEATABLE_READ |
要求對相同字段的多次讀取的結果必須相同,除非事務本身更新了數據可能導致幻讀。 |
ISOLATION_SERIALIZABLE |
事務被處理為順序執行可以防止臟讀、不可重復讀、幻讀。 |
3>.只讀提示
如果事務只對后端數據進行讀操作,則后端數據庫可以采用一些優化措施來提高執行效率。但必須在事務中才有效。也就是說要搭配傳播行為PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED 來設置。
4>.事務超時間隔
還可以設置事務的超時間隔,讓事務在特定秒數后自動回滾,不必等它自己結束。由于計時是從事事務開始時算起的,所以它也得搭配傳播行為為 PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED 來設置。
5>.回滾規則
當事務運行過程中拋出異常時,事務可以被聲明為回滾或者不回滾。默認情況下只在出現RuntimeExceptio才會回滾,而在出現受檢異常時不回滾。
當然,也可以改變這種回滾規則,可以聲明一個事務在出現特定的受檢異常時能回滾。也可以聲明一個事務在出現特定的非受檢異常時不回滾。
6.3、聲明式事務管理
1>.基于xml配置方式
第1步:定義事務通知
第2部:把事務通知綁定到切入點
- <?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.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql:///spring" />
- <property name="username" value="root" />
- <property name="password" value="123" />
- </bean>
- <!-- JDBC事務管理器 -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!-- DataSource事務管理器需要數據源實例 -->
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!-- 第1步:定義事務通知(主要是針對指定事務管理器對應的事務實現配置事務參數) -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <!-- 對選定的方法配置詳細的事務屬性 -->
- <tx:method name="find*" read-only="true" />
- <tx:method name="*"/>
- </tx:attributes>
- </tx:advice>
- <!-- 第2步:AOP配置 -->
- <aop:config>
- <!-- 聲明事務切入點(配置哪些類的哪些方法參與事務) -->
- <aop:pointcut id="AllServiceMethod"
- expression="execution(* com.zxf.service.*.*(..))" />
- <!-- 通知器(把事務通知綁定到切入點) -->
- <aop:advisor pointcut-ref="AllServiceMethod" advice-ref="txAdvice" />
- </aop:config>
- <!-- 以下是Spring容器管理的Bean -->
- <bean id="accountDao" class="com.zxf.dao.AccountDaoJDBCImpl">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <bean id="accountService" class="com.zxf.service.AccountService">
- <property name="accountDao" ref="accountDao"/>
- </bean>
- <!-- Hibernate事務管理器
- <bean id="txManager2"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref ="sessionFactory"/>
- </bean>
- -->
- <!-- JPA事務管理器
- <bean id="txManager3"
- class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref ="entityManagerFactory"/>
- </bean>
- -->
- </beans>
2>.基于注解方式
第1步:在spring配置文件中啟用對AspectJ注解的支持
- <?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.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql:///spring_04" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- </bean>
- <!-- JDBC事務管理器 -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!-- DataSource事務管理器需要數據源實例 -->
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!-- 啟用對事務注解的支持 -->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <!-- 以下是Spring容器管理的Bean -->
- <bean id="accountDao" class="com.zxf.dao.AccountDaoJDBCImpl">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <bean id="accountServiceByTxAnnotation"
- class="com.zxf.service.AccountServiceByTxAnnotation">
- <property name="accountDao" ref="accountDao"/>
- </bean>
- </beans>
第2步:用@Transactional注解指定接口、類或方法的事務屬性
- package com.zxf.service;
- import java.util.List;
- import org.springframework.transaction.annotation.Transactional;
- import com.zxf.dao.AccountDao;
- import com.zxf.domain.Account;
- /** Account業務邏輯類--基于注解方式的聲明式事務管理配置 */
- @Transactional //指定需要聲明式事務,事務屬性使用默認值
- public class AccountServiceByTxAnnotation {
- private AccountDao accountDao;
- public void setAccountDao(AccountDao accountDao){
- this.accountDao = accountDao;
- }
- }