1. 在需要事務管理的地方加@Transactional 註解。
@Transactional 註解可以被應用於接口定義和接口方法、類定義和類的 public 方法上。
2. @Transactional 註解只能應用到 public 可見度的方法上。
如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 註解,它也不會報錯, 但是這個被註解的方法將不會展示已配置的事務設置。
3. 注意僅僅 @Transactional 註解的出現不足於開啓事務行爲,它僅僅 是一種元數據。必須在配置文件中使用配置元素,才真正開啓了事務行爲。
詳見最下面的配置實例
4. 通過 元素的 "proxy-target-class" 屬性值來控制是基於接口的還是基於類的代理被創建。
如果 "proxy-target-class" 屬值被設置爲 "true",那麼基於類的代理將起作用(這時需要CGLIB庫cglib.jar在CLASSPATH中)。
如果 "proxy-target-class" 屬值被設置爲 "false" 或者這個屬性被省略,那麼標準的JDK基於接口的代理將起作用。
如<!--標準的JDK基於接口的代理將起作用-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
<!--基於類的代理將起作用 ,同時 cglib.jar必須在CLASSPATH中-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
非JTA事務(即非分佈式事務), 事務配置的時候 ,事務管理器(transactionManager)需要指定dataSource屬性(非分佈式事務,事務是在數據庫創建的鏈接上開啓。)
JTA事務(分佈式事務), 事務配置的時候 ,事務管理器(transactionManager)不能指定dataSource屬性(分佈式事務,是有全局事務來管理數據庫鏈接的)
註解@Transactional cglib與java動態代理最大區別是代理目標對象不用實現接口,那麼註解要是寫到接口方法上,要是使用cglib代理,這是註解事物就失效了,爲了保持兼容註解最好都寫到實現類方法上。
5. Spring團隊建議在具體的類(或類的方法)上使用 @Transactional 註解,而不要使用在類所要實現的任何接口上。
在接口上使用 @Transactional 註解,只能當你設置了基於接口的代理時它才生效。因爲註解是 不能繼承 的,這就意味着如果正在使用基於類的代理時,那麼事務的設置將不能被基於類的代理所識別,而且對象也將不會被事務代理所包裝。
6. @Transactional 的事務開啓 ,或者是基於接口的 或者是基於類的代理被創建。
所以在同一個類中一個方法調用另一個方法有事務的方法,事務是不會起作用的。
public interface PersonService {
//刪除指定id的person
public void delete(Integer personid) ;
//刪除指定id的person,flag
public void delete(Integer personid,boolean flag) ;
}
public class PersonServiceBean implements PersonService {
private JdbcTemplate jdbcTemplate;
public void delete(Integer personid){
try{
this.delete(personid,true)
System.out.println("delete success");
}catch(Exception e){
System.out.println("delete failed");
}
}
@Transactional
//此時,事務根本就沒有開啓, 即數據庫會默認提交該操作,即記錄別刪除掉 public void delete(Integer personid,boolean flag){
if(flag == ture){
jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
throw new RuntimeException("運行期例外");
}
}
}
public class PersonServiceBeanTest{
PersonService ps = new PersonServiceBean ();
ps.delete(5);
}
7. Spring使用聲明式事務處理,默認情況下,如果被註解的數據庫操作方法中發生了unchecked異常,所有的數據庫操作將rollback;如果發生的異常是checked異常,默認情況下數據庫操作還是會提交的。
public interface PersonService {
//刪除指定id的person
public void delete(Integer personid) ;
//獲取person
public Person getPerson(Integer personid);
}
//PersonServiceBean 實現了PersonService 接口,則基於接口的還是基於類的代理 都可以實現事務
@Transactional public class PersonServiceBean implements PersonService {
private JdbcTemplate jdbcTemplate;
//發生了unchecked異常,事務回滾, @Transactional
public void delete(Integer personid){
jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
throw new RuntimeException("運行期例外");
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
public interface PersonService {
//刪除指定id的person
public void delete(Integer personid) throws Exception;
//獲取person
public Person getPerson(Integer personid);
}
@Transactional
public class PersonServiceBean implements PersonService {
//發生了checked異常,事務不回滾,即數據庫記錄仍能被刪除,
//checked的例外,需要我們在外部用try/catch語法對調用該方法的地方進行包含 @Transactional
public void delete(Integer personid) throws Exception{
jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
throw new Exception("運行期例外");
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
但是,對於checked這種例外,默認情況下它是不會進行事務回滾的,但是如果我們需要它進行事務回滾,這時候可以在delete方法上通過@Transaction這個註解來修改它的行爲。
@Transactional
public class PersonServiceBean implements PersonService {
@Transactional(rollbackFor=Exception.class)
//rollbackFor這屬性指定了,既使你出現了checked這種例外,那麼它也會對事務進行回滾
public void delete(Integer personid) throws Exception{
jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
new int[]{java.sql.Types.INTEGER});
throw new Exception("運行期例外");
}
}
在PersonServiceBean這個業務bean裏面,有一些事務是不需要事務管理的,好比說獲取數據的getPersons方法,getPerson方法。因爲@Transactional 放在了類的上面。
此時,可以採用propagation這個事務屬性@Transactional(propagation=Propagation.NOT_SUPPORTED),
propagation這個屬性指定了事務傳播行爲,我們可以指定它不支持事務,當我們這麼寫了之後,Spring容器在getPersons方法執行前就不會開啓事務.
@Transactional
public class PersonServiceBean implements PersonService {
@Transactional(propagation=Propagation.NOT_SUPPORTED)
//則此方法 就不會開啓事務了
public Person getPerson(Integer personid)
{
}
}
混合事務配置實例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans" 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-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
<property name="driverClass" value="${db.driver}"></property>
<property name="jdbcUrl" value="${db.url}"></property>
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="acquireIncrement" value="2"></property>
<property name="preferredTestQuery" value="select 1 from dual"></property>
<!-- <property name="idleConnectionTestPeriod" value="60"></property> -->
<property name="initialPoolSize" value="${db.minPoolSize}"></property>
<property name="minPoolSize" value="${db.minPoolSize}"></property>
<property name="maxPoolSize" value="${db.maxPoolSize}"></property>
<property name="maxIdleTime" value="${db.maxIdleTime}"></property>
</bean>
<!-- Transaction manager :
非JTA事務(即非分佈式事務), 事務配置的時候 ,事務管理器(transactionManager)需要指定dataSource屬性(非分佈式事務,事務是在數據庫創建的鏈接上開啓。)
JTA事務(分佈式事務), 事務配置的時候 ,事務管理器(transactionManager)不能指定dataSource屬性(分佈式事務,是有全局事務來管理數據庫鏈接的)
-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 使用註解annotation定義事務:
如果 "proxy-target-class" 屬值被設置爲 "true",那麼基於類的代理將起作用(這時需要CGLIB庫cglib.jar在CLASSPATH中)。
如果 "proxy-target-class" 屬值被設置爲 "false" 或者這個屬性被省略,那麼標準的JDK基於接口的代理將起作用。
-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- 使用AOP transaction定義事務 -->
<tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="query*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="do*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="execute*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="*" read-only="true" propagation="REQUIRED" isolation="DEFAULT" />
</tx:attributes>
</tx:advice>
<tx:advice id="logTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" rollback-for="java.lang.Exception" isolation="DEFAULT" />
<tx:method name="query*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" propagation="REQUIRES_NEW"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!-- "order"參數,這個參數是用來控制aop通知的優先級,值越小,優先級越高 ,混合事務配置時要制定該參數。
execution:使用“execution(方法表達式)”匹配方法執行
within:使用“within(類型表達式)”匹配指定類型內的方法執行
-->
<aop:config>
<aop:pointcut id="defaultOperation"
expression="execution(* *..*ServiceImpl.*(..))" />
<aop:pointcut id="logServiceOperation"
expression="@within(com.homent.service.LogService+" />
<aop:advisor advice-ref="defaultTxAdvice" order="3" pointcut-ref="defaultOperation" />
<aop:advisor advice-ref="logTxAdvice" order="2" pointcut-ref="logServiceOperation" />
</aop:config>
</beans>