編程式事務控制相關對象
PlatformTransactionManager
PlatformTransactionManager 接口是 spring 的事務管理器,它里面提供了我們常用的操作事務的方法。注意:
PlatformTransactionManager 是接口類型,不同的 Dao 層技術則有不同的實現類
例如:Dao 層技術是jdbc 或 mybatis 時:org.springframework.jdbc.datasource.DataSourceTransactionManager
Dao 層技術是hibernate時:org.springframework.orm.hibernate5.HibernateTransactionManager
TransactionDefinition
TransactionDefinition 是事務的定義信息對象,里面有如下方法:
事務隔離級別
設置隔離級別,可以解決事務并發產生的問題,如臟讀、不可重復讀和虛讀。
-
ISOLATION_DEFAULT
-
ISOLATION_READ_UNCOMMITTED
-
ISOLATION_READ_COMMITTED
-
ISOLATION_REPEATABLE_READ
-
ISOLATION_SERIALIZABLE
事務傳播行為
-
REQUIRED:如果當前沒有事務,就新建一個事務,如果已經存在一個事務中,加入到這個事務中。一般的選擇(默認值)
-
SUPPORTS:支持當前事務,如果當前沒有事務,就以非事務方式執行(沒有事務)
-
MANDATORY:使用當前的事務,如果當前沒有事務,就拋出異常
-
REQUERS_NEW:新建事務,如果當前在事務中,把當前事務掛起。
-
NOT_SUPPORTED:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起
-
NEVER:以非事務方式運行,如果當前存在事務,拋出異常
-
NESTED:如果當前存在事務,則在嵌套事務內執行。如果當前沒有事務,則執行 REQUIRED 類似的操作
-
超時時間:默認值是-1,沒有超時限制。如果有,以秒為單位進行設置
-
是否只讀:建議查詢時設置為只讀
TransactionStatus
TransactionStatus 接口提供的是事務具體的運行狀態
基于 XML 的聲明式事務控制
什么是聲明式事務控制
Spring 的聲明式事務顧名思義就是采用聲明的方式來處理事務。這里所說的聲明,就是指在配置文件中聲明,用在 Spring 配置文件中聲明式的處理事務來代替代碼式的處理事務。
聲明式事務處理的作用
-
事務管理不侵入開發的組件。具體來說,業務邏輯對象就不會意識到正在事務管理之中,事實上也應該如此,因為事務管理是屬于系統層面的服務,而不是業務邏輯的一部分,如果想要改變事務管理策劃的話,也只需要在定義文件中重新配置即可
-
在不需要事務管理的時候,只要在設定文件上修改一下,即可移去事務管理服務,無需改變代碼重新編譯,這樣維護起來極其方便
注意:Spring 聲明式事務控制底層就是AOP。
聲明式事務控制的實現
①引入tx命名空間
<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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
②配置事務增強
③配置事務 AOP 織入
<bean id="txm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="datasource"/></bean><tx:advice id="interceptor" transaction-manager="txm"><tx:attributes><tx:method name="save" isolation="DEFAULT" propagation="REQUIRED" timeout="-1" read-only="false"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="pc" expression="execution( void com.ImplService.*(..))"/><aop:advisor advice-ref="interceptor" pointcut-ref="pc"/></aop:config>
④測試事務控制轉賬業務代碼
@Service("service")
@Scope("singleton")
public class ImplService implements com.Service {@Autowired@Qualifier("impl")ImplDao dao;public void save() {dao.out();dao.in();}}
@Repository("impl")
@Scope("singleton")
public class ImplDao implements Dao {@AutowiredJdbcTemplate jdbcTemplate;public void save() {System.out.println("saving");}public void in(){jdbcTemplate.update("update account set money=money+500 where name='tony'");}public void out(){jdbcTemplate.update("update account set money=money-500 where name='john'");}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringJunitTest {@AutowiredService service;@Testpublic void test(){service.save();}}
切點方法的事務參數的配置
<!--事務增強配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*"/></tx:attributes>
</tx:advice>
其中,tx:method 代表切點方法的事務參數的配置,例如:
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1" read-only="false"/>
-
name:切點方法名稱
-
isolation:事務的隔離級別
-
propogation:事務的傳播行為
-
timeout:超時時間
-
read-only:是否只讀
基于注解的聲明式事務控制
@Service("service")
@Scope("singleton")
public class ImplService implements com.Service {@Autowired@Qualifier("impl")ImplDao dao;@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = -1,readOnly = false)public void save() {dao.out();/* int i=1/0;*/dao.in();}}
@Repository("impl")
@Scope("singleton")
public class ImplDao implements Dao {@AutowiredJdbcTemplate jdbcTemplate;public void save() {System.out.println("saving");}public void in(){jdbcTemplate.update("update account set money=money+500 where name='tony'");}public void out(){jdbcTemplate.update("update account set money=money-500 where name='john'");}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringJunitTest {@AutowiredService service;@Testpublic void test(){service.save();}}
<?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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
"><context:property-placeholder location="jdbc.properties"/><context:component-scan base-package="com"/><tx:annotation-driven transaction-manager="txm"/><bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="datasource"/></bean><bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"/><property name="jdbcUrl" value="${jdbc.url}"/><property name="user" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><bean id="txm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="datasource"/></bean></beans>
注解配置聲明式事務控制解析
①使用 @Transactional 在需要進行事務控制的類或是方法上修飾,注解可用的屬性同 xml 配置方式,例如隔離級別、傳播行為等。
②注解使用在類上,那么該類下的所有方法都使用同一套注解參數配置。
③使用在方法上,不同的方法可以采用不同的事務參數配置。
④Xml配置文件中要開啟事務的注解驅動<tx:annotation-driven />
知識要點
注解聲明式事務控制的配置要點
-
平臺事務管理器配置(xml方式)
-
事務通知的配置(@Transactional注解配置)
-
事務注解驅動的配置 tx:annotation-driven/