一, JDBC事務管理
Spring提供編程式的事務管理(Programmatic transaction manage- ment)與聲明式的事務管理(Declarative transaction management),爲不同的事務實現提供了一致的編程模型,這節以JDBC事務爲例,介紹Spring的事務管理。
二, Spring對事務的支持
事務是一組原子(Atomic)操作的工作單元,以數據庫存取的實例來說,就是一組SQL指令,這一組SQL指令必須全部執行成功,若因爲某個原因未全部執行成功(例如其中一行SQL有錯誤),則先前所有執行過的SQL指令都會被撤消。
舉個簡單的例子,一個客戶從A銀行轉賬至B銀行,要作的動作爲從A銀行的賬戶扣款、在B銀行的賬戶加上轉賬的金額,兩個動作必須成功,如果有一個動作失敗,則此次轉賬失敗。
事務還必須保持所參與資源的一致性(Consistent),例如在銀行賬戶的例子中,兩個賬戶的轉賬金額,B賬戶取款的金額不能大於A賬戶的存款金額。每個事務彼此之間必須是隔離的(Isolated),例如在A賬戶中可能有兩筆事務,同時進行存款與提款的動作,兩個事務基本上不需意識到彼此的存在。事務還必須是可持續的(Durable),在某一筆事務之後,這筆事務必須是被記錄下來的。
在這裏將介紹JDBC如何使用事務管理。首先來看看事務的原子性實現,在JDBC中,可以操作 Connection的setAutoCommit() 方法,給定false參數,在下達一連串的SQL語句後,自行執行Connection的commit()來送出變更,如果中間發生錯誤,則執行 rollback() 來撤消所有的執行,例如:
try {
.....
connection.setAutoCommit(false);
.....
// 一連串SQL操作
connection.commit();
} catch(SQLException) {
// 發生錯誤,撤消所有變更
connection.rollback();
}
在Spring中對JDBC的事務管理加以封裝,Spring事務管理的抽象關鍵在於org.springframework.transaction.PlatformTransactionManager接口的實現:
...
public interface PlatformTransactionManager {
TransactionStatus getTransaction(TransactionDefinition
definition) throws TransactionException;
void commit(TransactionStatus status)
throws TransactionException;
void rollback(TransactionStatus status)
throws TransactionException;
}
PlatformTransactionManager接口有許多具體的事務實現類,例如 DataSourceTransactionManager、HibernateTransactionManager、JdoTransaction- Manager、JtaTransactionManager等,通過依賴於PlatformTransactionManager接口及各種的技術實現,Spring在事務管理上可以讓開發人員使用一致的編程模型,即使所使用的是不同的事務管理技術。
TransactionException是Unchecked Exception。事務的失敗通常都是致命的錯誤,Spring不強迫您一定要處理,而是讓您自行選擇是否要捕捉異常。
getTransaction() 方法根據一個TransactionDefinition對象來回傳一個TransactionStatus對象,TransactionDefinition接口的實例定義了事務的隔離程度(Isolation level)、傳播行爲(Propagation behavior)、超時(Timeout)、只讀(Read-only)等,TransactionStatus代表着一個新的事務發起或已經存在的事務,您可以通過它來控制事務的執行或調查的狀態:
...
public interface TransactionStatus {
boolean isNewTransaction();
void setRollbackOnly();
boolean isRollbackOnly();
}
Spring提供編程式的事務管理(Programmatic transaction management)與聲明式的事務管理(Declarative transaction management):
l 編程式的事務管理
編程式的事務管理可以清楚地控制事務的邊界,也就是讓您自行實現事務開始時間、撤消操作的時機、結束時間等,可以實現細粒度的事務控制。
l 聲明式的事務管理
然而多數的情況下,事務並不需要細粒度的控制,而是採用聲明式的事務管理,好處是Spring事務管理的相關API可以不用介入程序之中,從對象的角度來看,它並不知道自己正被納入事務管理之中,在不需要事務管理的時候,只要在設置文件上修改一下設置,即可移去事務管理服務。
5.3.2 JDBC編程事務管理
Spring提供兩種方式實現編程式的事務管理,一是直接使用PlatformTransaction- Manager實現,二是使用org.springframework.transaction.support.Transaction- Template。
先來看看如何使用PlatformTransactionManager,在這裏使用它的實現類 DataSourceTransactionManager,可以改寫一下之前5.2.1節中的JdbcTemplateDemo項目,讓它具有事務管理功能,修改一下UserDAO類的insert() 方法來作示範:
ProgrammaticTransactionDemo UserDAO.java
package onlyfun.caterpillar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.
datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.
support.DefaultTransactionDefinition;
public class UserDAO implements IUserDAO {
private DataSourceTransactionManager transactionManager;
private DefaultTransactionDefinition def;
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
transactionManager =
new DataSourceTransactionManager(dataSource);
// 建立事務的定義
def = new DefaultTransactionDefinition();
def.setPropagationBehavior(
TransactionDefinition.PROPAGATION_REQUIRED);
}
public void insert(User user) {
String name = user.getName();
int age = user.getAge().intValue();
TransactionStatus status =
transactionManager.getTransaction(def);
try {
jdbcTemplate.update("INSERT INTO user (name,age) "
+ "VALUES('" + name + "'," + age + ")");
// 下面的SQL有錯誤,用以測試事務
jdbcTemplate.update("INSER INTO user (name,age) "
+ "VALUES('" + name + "'," + age + ")");
}
catch(DataAccessException e) {
transactionManager.rollback(status);
throw e;
}
transactionManager.commit(status);
}
public User find(Integer id) {
List rows = jdbcTemplate.queryForList(
"SELECT * FROM user WHERE id=" + id.intValue());
Iterator it = rows.iterator();
if(it.hasNext()) {
Map userMap = (Map) it.next();
Integer i = new Integer(
userMap.get("id").toString());
String name = userMap.get("name").toString();
Integer age = new Integer(
userMap.get("age").toString());
User user = new User();
user.setId(i);
user.setName(name);
user.setAge(age);
return user;
}
return null;
}
}
在insert()方法中使用了 DataSourceTransactionManager來進行事務管理,如果發生了異常,則catch區塊中會進行事務的Rollback,在 insert() 方法中故意寫入錯誤的SQL(注意INSERT方法少寫了一個T),因此實際上數據並不會被儲存至數據庫中。
良葛格的話匣子<<<
要使用MySQL數據庫進行事務處理,必須建立支持事務的表格類型,例如InnoDB的表格類型,這裏用來建立表格的SQL如下所示:
CREATE TABLE user (
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default '',
age INT
) TYPE = InnoDB;
另一個實現編程式事務管理的方法是使用TransactionTemplate,它需要一個TransactionManager實例,如下所示:
...
TransactionTemplate transactionTemplate =
new TransactionTemplate(transactionManager);
...
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return jdbcTemplate.update("INSERT INTO user (name,age) "
+ "VALUES('" + name + "'," + age + ")");
}
});
如果發生了異常,則會進行Rollback,否則提交事務,如果沒有回傳值,則也可以使用TransactionCallbackWithoutResult:
...
transactionTemplate.execute(
new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(
TransactionStatus status) {
. ...
}
});
5.3.3 JDBC聲明事務管理
Spring聲明式的事務管理依賴它的AOP框架來完成。使用聲明事務管理的好處是,事務管理不能侵入您所開發的組件,具體來說,DAO對象不會意識到正在事務管理之中,事實上也應當如此,因爲事務管理是屬於系統層面的服務,而不是業務邏輯的一部分,如果想要改變事務管理策略的話,也只需要在定義文件中重新配置。
舉個例子來說,可以將5.2.1節中的JdbcTemplateDemo項目修改一下,在不修改 UserDAO類的情況下,可以爲它加入事務管理的服務,一個簡單的方法是使用TransactionProxyFactoryBean,指定要介入的事務管理對象及其方法,這需要在定義文件中修改,如下所示:
DeclarativeTransactionDemo beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.
→ datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="caterpillar"/>
<property name="password" value="123456"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.
→ datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDAO"
class="onlyfun.caterpillar.UserDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDAOProxy"
class="org.springframework.transaction.
→ interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>onlyfun.caterpillar.IUserDAO</value>
</list>
</property>
<property name="target" ref="userDAO"/>
<property name="transactionManager"
ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
TransactionProxyFactoryBean需要一個TransactionManager,由於這裏使用的是JDBC,所以使用DataSourceTransactionManager,TransactionProxyFactoryBean 是個代理對象,"target" 屬性指定要代理的對象,事務管理會自動介入指定的方法前後,這裏使用 "transactionAttributes" 屬性指定,"insert*" 表示指定方法名稱以insert開頭的都要納入事務管理,您也可以指定方法全名,如果在方法執行過程中發生錯誤,則所有先前的操作自動撤回,否則正常提交。
在"insert*" 等方法上指定了 "PROPAGATION_REQUIRED",表示在目前的事務中執行操作,如果事務不存在就建立一個新的,相關的常數意義都可以在API文件的 TransactionDefinition接口中找到。您可以加上多個事務定義,中間使用逗號 "," 區隔,例如可以加上只讀,或者是指定某個異常發生時撤回操作:
PROPAGATION_REQUIRED,readOnly,-MyCheckedException
MyCheckedException前面加上 "-" 時,表示發生指定異常時撤消操作,如果前面加上 "+",表示發生異常時立即提交。
由於"userDAO"被"userDAOProxy"代理了,所以要做的是取得"userDAOProxy",而不是"userDAO",例如:
DeclarativeTransactionDemo SpringDAODemo.java
package onlyfun.caterpillar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.ClassPathXmlApplicationContext;
public class SpringDAODemo {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"beans-config.xml");
User user = new User();
user.setName("caterpillar");
user.setAge(new Integer(30));
IUserDAO userDAO =
(IUserDAO) context.getBean("userDAOProxy");
userDAO.insert(user);
user = userDAO.find(new Integer(1));
System.out.println("name: " + user.getName());
}
}
您也可以設置不同的TransactionInterceptor來得到更多的管理細節,例如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.
→ datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="caterpillar"/>
<property name="password" value="123456"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.
→ datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDAO"
class="onlyfun.caterpillar.UserDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionInterceptor"
class="org.springframework.transaction.
→ interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource"
value="onlyfun.caterpillar.UserDAO.insert*=
→ PROPAGATION_REQUIRED "/>
</bean>
<bean id="userDAOProxy"
class="org.springframework.aop.
→ framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>onlyfun.caterpillar.IUserDAO</value>
</list>
</property>
<property name="target" ref="userDAO"/>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>
即使後來不再需要事務管理,也可以直接在Bean定義文件中修改配置,而不用修改程序重新進行編譯等動作。
良葛格的話匣子<<<
聲明事務管理是利用Spring AOP來達成的,所以執行以上的程序時,請記得您的Classpath設置中必須包括spring-aop.jar。
5.3.4 事務的屬性介紹
Spring使用AOP來完成聲明式的事務管理,因而聲明式事務是以方法爲邊界的,Spring的事務屬性(Transaction attribute)自然就在於描述事務應用至方法上的策略,在Spring中事務屬性分作以下的幾個參數:
l 傳播行爲(Propagation behavior)
傳播行爲定義了事務應用於方法上之邊界(Boundaries),它告知何時該開始一個新的事務,或何時事務該被暫停,或方法是否要在事務中進行。
Spring定義了幾個傳播行爲,可以在TransactionDefinition的API文件說明上找到相對應的常數與說明,以下列出幾個:
表5.1 事務傳播行爲說明
傳播行爲 |
說明 |
PROPAGATION_MANDATORY |
方法必須在一個現存的事務中進行,否則丟出異常 |
PROPAGATION_NESTED |
在一個嵌入的事務中進行,如果不是,則同PROPAGATION_REQUIRED |
PROPAGATION_NEVER |
指出不應在事務中進行,如果有就丟出異常 |
PROPAGATION_NOT_SUPPORTED |
指出不應在事務中進行,如果有就暫停現存的事務 |
PROPAGATION_REQUIRED |
支持現在的事務,如果沒有就建立一個新的事務 |
PROPAGATION_REQUIRES_NEW |
建立一個新的事務,如果現存一個事務就暫停它 |
PROPAGATION_SUPPORTS |
支持現在的事務,如果沒有就以非事務的方式執行 |
舉個例子來說,如果傳播行爲被聲明爲PROPAGATION_REQUIRED,則事務的邊界在開始第一個事務的方法呼叫及結束時,如果先前沒有事務被開始,則事務邊界即爲目前方法的執行前後。又如果傳播行爲被聲明爲PROPAGATION_REQUIRES_NEW,則事務的邊界即爲該方法執行的前後。
l 隔離層級(Isolation level)
在一個應用程序中,可能有多個事務同時在進行,這些事務應當彼此之間互相不知道另一個事務的存在,好比現在整個應用程序就只有一個事務存在,由於事務彼此之間獨立,若讀取的是同一個數據的話,就容易發生問題,例如:
n Dirty read
某個事務已更新一份數據,另一個事務在此時讀取了同一份數據,由於某些原因,前一個Roll back了操作,則後一個事務所讀取的數據就會是不正確的。
n Non-repeatable read
在一個事務的兩次查詢之中數據不一致,這可能是因爲兩次查詢過程中間插入了一個事務更新的原有的數據。
n Phantom read
在一個事務的兩次查詢中數據筆數不一致,例如有一個事務查詢了幾列(Row)數據,而另一個事務卻在此時插入了新的幾列數據,先前的事務在接下來的查詢中,就會發現有幾列數據是它先前所沒有的。
爲了避免以上問題的方法之一,需要在某個事務進行過程中鎖定正在更新或查詢的數據字段,直到目前的事務完成,然而完全鎖定字段時,若另一個事務來查詢同一份數據就必須等待,直到前一個事務完成並解除鎖定爲止,因而會造成應用程序在查詢或更新數據時效率上的問題,而事實上根據需求的不同,並不用在事務進行時完全地鎖定數據,隔離層級可以讓您根據實際的需求,對數據的鎖定進行設置。
Spring提供了幾種隔離層級設置,同類型的設置可以在TransactionDefinition的API文件說明上找到相對應的常數與說明,以下列出幾個:
表5.2 事務隔離層級說明
隔離層級 |
說明 |
ISOLATION_DEFAULT |
使用底層數據庫預設的隔離層級 |
ISOLATION_READ_COMMITTED |
允許事務讀取其他並行的事務已經送出(Commit)的數據字段,可以防止Dirty read問題 |
ISOLATION_READ_UNCOMMITTED |
允許事務讀取其他並行的事務還沒送出的數據,會發生Dirty、Nonrepeatable、Phantom read等問題 |
續表
隔離層級 |
說明 |
ISOLATION_REPEATABLE_READ |
要求多次讀取的數據必須相同,除非事務本身更新數據,可防止Dirty、Nonrepeatable read問題 |
ISOLATION_SERIALIZABLE |
完整的隔離層級,可防止Dirty、Nonrepeatable、Phantom read等問題,會鎖定對應的數據表格,因而有效率問題 |
l 只讀提示(Read-only hints)
如果事務只進行讀取的動作,則可以利用底層數據庫在只讀操作時發生的一些最佳化動作,由於這個動作利用到數據庫在只讀的事務操作最佳化,因而必須在事務中才有效,也就是說要搭配傳播行爲 PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED來設置。
l 事務超時期間(The transaction timeout period)
有的事務操作可能延續很長一段的時間,事務本身可能關聯到數據表格的鎖定,因而長時間的事務操作會有效率上的問題,對於過長的事務操作,您要考慮Roll back事務並要求重新操作,而不是無限時的等待事務完成。
您可以設置事務超時期間,計時是從事務開始時,所以這個設置必須搭配傳播行爲PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED來設置。
5.3.5 TransactionAttributeSource、TransactionAttribute
在TransactionProxyFactoryBean上有setTransactionAttributeSource()與setTransaction Attributes()方法,它們是用來設置事務屬性的策略實例。
org.springframework.transaction.interceptor.TransactionAttributeSource 接口上有一個getTransactionAttribute() 方法,您可以根據傳遞給它的Method實例與Class實例,決定該回傳一個什麼內容的 org.springframework.transaction. interceptor.TransactionAttribute實例,一個最簡單的TransactionAttributeSource實現是 org.springframework.transaction.interceptor.MatchAlwaysTransaction- AttributeSource,對於每一個方法執行都會應用事務,它回傳的TransactionAttribute實例的默認傳播行爲是 PROPAGATION_REQUIRED,隔離層級爲ISOLATION_DEFAULE。
一個應用的例子如下所示:
...
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.
→ MatchAlwaysTransactionAttributeSource"/>
<bean id="userDAOProxy"
class="org.springframework.transaction.
→ interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>onlyfun.caterpillar.IUserDAO</value>
</list>
</property>
<property name="target" ref="userDAO"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource"
ref="transactionAttributeSource"/>
</bean>
...
您可以使用org.springframework.transaction.interceptor.DefaultTransaction- Attribute,並設置自己的事務策略,之後設置給TransactionAttributeSource,例如:
...
<bean id="myTransactionAttribute"
class="org.springframework.transaction.
→ interceptor.DefaultTransactionAttribute">
<property name="propagationBehaviorName"
value="PROPAGATION_REQUIRES_NEW"/>
<property name="isolationLevelName"
value="ISOLATION_REPEATABLE_READ"/>
</bean>
<bean id="transactionAttributeSource"
class="org.springframework.transaction.
→ interceptor.MatchAlwaysTransactionAttributeSource">
<property name="transactionAttribute"
ref="myTransactionAttribute"/>
</bean>
<bean id="userDAOProxy"
class="org.springframework.transaction.
→ interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>onlyfun.caterpillar.IUserDAO</value>
</list>
</property>
<property name="target" ref="userDAO"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource"
ref="transactionAttributeSource"/>
</bean>
...
可以使用org.springframework.transaction.interceptor.NameMatchTransaction- AttributeSource來指定某些方法要應用事務,以及要應用的事務策略,例如:
...
<bean id="transactionAttributeSource"
class="org.springframework.transaction.
→ interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="insert*">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
<bean id="userDAOProxy"
class="org.springframework.transaction.
→ interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>onlyfun.caterpillar.IUserDAO</value>
</list>
</property>
<property name="target" ref="userDAO"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributeSource"
ref="transactionAttributeSource"/>
</bean>
...
在 NameMatchTransactionAttributeSource的 "properties"屬性上,可以指定方法名稱與事務策略,方法名稱的指定可以指定全名,也可以使用Wildcard來指定,例如上面的指定中,只要方法名稱以insert爲開頭的都會應用相對應的事務策略。
在指定事務策略時,指定的格式如下:
傳播行爲,隔離層級,只讀,+異常, -異常
除了傳播行爲一定要設置之外,其他都可選擇性的設置,中間以逗號區隔,例如:
PROPAGATION_REQUIRED,readOnly,-MyCheckedException
MyCheckedException前面加上 "-" 時,表示發生指定異常時撤消操作,如果前面加上 "+",表示發生異常時立即提交。
在比較簡單的設置中,可以僅設置TransactionProxyFactoryBean,並在它的 "transactionAttributes" 屬性上直接設置要應用事務的方法及事務策略,例如:
...
<bean id="userDAOProxy"
class="org.springframework.transaction.
→ interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>onlyfun.caterpillar.IUserDAO</value>
</list>
</property>
<property name="target" ref="userDAO"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
...
甚至也可以直接指定TransactionInterceptor,以獲得更多的控制,例如:
...
<bean id="transactionInterceptor"
class="org.springframework.transaction.
→ interceptor.TransactionInterceptor">
<property name="transactionManager">
ref="transactionManager"/>
<property name="transactionAttributeSource"
value="onlyfun.caterpillar.UserDAO.insert*=
→ PROPAGATION_REQUIRED"/>
</bean>
<bean id="userDAOProxy"
class="org.springframework.aop.
→ framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>onlyfun.caterpillar.IUserDAO</value>
</list>
</property>
<property name="target" ref="userDAO"/>
<property name="interceptorNames" value="transactionInterceptor"/>
</bean>
...
選擇哪一種設置方式是需求的問題,您可以嘗試在DeclarativeTransactionDemo項目的Bean定義文件上設置以上所介紹的方式,基於篇幅的限制,以上僅列出部分的設置內容。
5.3.6 Spring 2.0聲明式事務管理:基於XML Schmea
在Spring 2.0中要設置聲明式事務管理,可以依賴於Spring 2.0的<aop>與<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: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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
…
</beans>
事務是系統層面的服務,也就是一個Aspect,其實具體來說就是一個Advice,您可以使用<tx:advice>標籤來提供這個Advice,它需要設置一個TransactionManager,並在當中使用<tx:attributes>來設置事務相關屬性。
可以將先前的DeclarativeTransactionDemo項目改寫,修改其beans-config.xml爲使用<aop>與<tx>標籤的方式:
DeclarativeTransactionDemo2 beans-config.xml
<?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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.
→ datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="caterpillar"/>
<property name="password" value="123456"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.
→ datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDAO"
class="onlyfun.caterpillar.UserDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userDAOPointcut"
expression="execution(* onlyfun.caterpillar.IUserDAO.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="userDAOPointcut"/>
</aop:config>
</beans>
注意到<tx:method>中的屬性設置,對於傳播行爲、隔離層級、只讀、超時、異常時撤回或提交,都有對應的"propagation"、"isolation"、"timeout"、"read-only"、"rollback-for"、"no- rollback-for"屬性可以設置,若不設置,"propagation"屬性默認是"REQUIRE","isolation"屬性默認是"DEFAULT"、"timeout"屬性默認是"-1"(單位是秒)、"read-only"屬性默認是"false"。
與先前介紹Spring 2.0基於XML Schema的AOP設置相同,由於不再於設置文件中設置代理對象,所以直接取得"userDAO"實例進行操作即可。
5.3.7 Spring 2.0聲明式事務管理:基於Annotation
聲明式事務管理在Spring 2.0中,也支持使用Annotation的標示方式,方法是使用@Transactional來標示,例如可以將 DeclarativeTransactionDemo項目的UserDAO改寫,在上頭直接標示@Transactional,並設置相關屬性:
DeclarativeTransactionDemo3 UserDAO.java
package onlyfun.caterpillar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
public class UserDAO implements IUserDAO {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Transactional(propagation = Propagation.REQUIRED)
public void insert(User user) {
String name = user.getName();
int age = user.getAge().intValue();
jdbcTemplate.update("INSERT INTO user (name,age) "
+ "VALUES('" + name + "'," + age + ")");
}
@Transactional(readOnly=true)
public User find(Integer id) {
List rows = jdbcTemplate.queryForList(
"SELECT * FROM user WHERE id=" + id.intValue());
Iterator it = rows.iterator();
if(it.hasNext()) {
Map userMap = (Map) it.next();
Integer i = new Integer(userMap.get("id").toString());
String name = userMap.get("name").toString();
Integer age =
new Integer(userMap.get("age").toString());
User user = new User();
user.setId(i);
user.setName(name);
user.setAge(age);
return user;
}
return null;
}
}
在使用@Transactional時,相關的屬性設置爲"propagation"、"isolation"、"readOnly"、"timeout"、"rollbackFor"、"noRollbackFor"等,而在beans-config.xml中,則要使用<tx:annotation-driven>標籤,並指定TransactionManager,例如:
DeclarativeTransactionDemo3 beans-config.xml
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.
→ datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/demo"/>
<property name="username" value="caterpillar"/>
<property name="password" value="123456"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.
→ datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="userDAO"
class="onlyfun.caterpillar.UserDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
同樣的,由於不再於設置文件中設置代理對象,所以直接取得"userDAO"實例進行操作即可。