1. 在須要事務管理的地方加@Transactional 註解。@Transactional 註解能夠被應用於接口定義和接口方法、類定義和類的 public 方法上。java
2. @Transactional 註解只能應用到 public 可見度的方法上。 若是你在 protected、private 或者 package-visible 的方法上使用 @Transactional 註解,它也不會報錯, 可是這個被註解的方法將不會展現已配置的事務設置。sql
3. 注意僅僅 @Transactional 註解的出現不足於開啓事務行爲,它僅僅 是一種元數據。必須在配置文件中使用配置元素,才真正開啓了事務行爲。數據庫
4. 經過 元素的 "proxy-target-class" 屬性值來控制是基於接口的仍是基於類的代理被創建。 若是 "proxy-target-class" 屬值被設置爲 "true",那麼基於類的代理將起做用(這時須要CGLIB庫cglib.jar在CLASSPATH中)。若是 "proxy-target-class" 屬值被設置爲 "false" 或者這個屬性被省略,那麼標準的JDK基於接口的代理將起做用。分佈式
<!-- JTA事務(非分佈式事務), 事務配置的時候 ,不能指定dataSource屬性(分佈式事務,是有全局事務來管理數據庫連接的)--> <!-- 標準的JDK基於接口的代理將起做用 --> <!-- aop切面 --> <aop:aspectj-autoproxy proxy-target-class="false" /> <!-- 基於類的代理將起做用 ,同時 cglib.jar必須在CLASSPATH中 --> <!-- aop切面 --> <aop:aspectj-autoproxy proxy-target-class="true" />
注解@Transactional cglib與java動態代理最大區別是代理目標對象不用實現接口,那麼註解要是寫到接口方法上,要是使用cglib代理,這是註解事物就失效了,爲了保持兼容註解最好都寫到實現類方法上。this
5. Spring團隊建議在具體的類(或類的方法)上使用 @Transactional 註解,而不要使用在類所要實現的任何接口上。在接口上使用 @Transactional 註解,只能當你設置了基於接口的代理時它才生效。因爲註解是 不能繼承 的,這就意味着若是正在使用基於類的代理時,那麼事務的設置將不能被基於類的代理所識別,並且對象也將不會被事務代理所包裝。spa
6. @Transactional 的事務開啓 ,或者是基於接口的 或者是基於類的代理被建立。因此在同一個類中一個方法調用另外一個方法有事務的方法,事務是不會起做用的。代理
public interface PersonageTempService { //刪除指定id的Personage public void del(Integer Personageid) ; //刪除指定id的Personage,flag public void del(Integer Personageid,boolean flag) ; } public class PersonageTempServiceBean implements PersonageTempService { private JdbcTemplate jdbcTemplate; public void del(Integer Personageid){ try{ this.del(Personageid,true) System.out.println("del success"); }catch(Exception e){ System.out.println("del failed"); } } @Transactional //此時,事務根本就沒有開啓, 即數據庫會默認提交該操做,即記錄別刪除掉 public void del(Integer Personageid,boolean flag){ if(flag == ture){ jdbcTemplate.update("del from Personage where id=?", new Object[]{Personageid}, new int[]{java.sql.Types.INTEGER}); throw new RuntimeException("運行期例外"); } } } public class PersonageTempServiceBeanTest{ PersonageTempService ps = new PersonageTempServiceBean (); ps.del(5); } }
7. Spring使用聲明式事務處理,默認狀況下,若是被註解的數據庫操做方法中發生了unchecked異常,全部的數據庫操做將rollback;若是發生的異常是checked異常,默認狀況下數據庫操做仍是會提交的。code
public interface PersonageService { //刪除指定id的Personage public void del(Integer Personageid) ; //獲取Personage public Personage getPersonage(Integer Personageid); } //PersonageServiceBean 實現了PersonageService 接口,則基於接口的仍是基於類的代理 均可以實現事務 @Transactional public class PersonageServiceBean implements PersonageService { private JdbcTemplate jdbcTemplate; //發生了unchecked異常,事務回滾, @Transactional public void del(Integer Personageid){ jdbcTemplate.update("del from Personage where id=?", new Object[]{Personageid}, new int[]{java.sql.Types.INTEGER}); throw new RuntimeException("運行期例外"); } } public interface PersonageService { //刪除指定id的Personage public void delete(Integer Personageid) throws Exception; //獲取Personage public Personage getPersonage(Integer Personageid); } @Transactional public class PersonageServiceBean implements PersonageService { //發生了checked異常,事務不回滾,即數據庫記錄仍能被刪除, //checked的例外,須要咱們在外部用try/catch語法對調用該方法的地方進行包含 @Transactional public void delete(Integer Personageid) throws Exception{ jdbcTemplate.update("delete from Personage where id=?", new Object[]{Personageid}, 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) { } }
方法的可見度和 @Transactional 註解應該只被應用到可見度的方法上。 若是你在 protected、private 或者 package-visible 的方法上使用 @Transactional註解,它也不會報錯, 可是這個被註解的方法將不會展現已配置的事務設置。 @Transactional註解能夠被應用於接口定義和接口方法、類定義和類的方法上。 然而,請注意僅僅註解的出現不足於開啓事務行爲,它僅僅是一種元數據,可以被能夠識別@Transactional註解和上述 的配置適當的具備事務行爲的beans所使用。 Spring團隊的建議是你在具體的類(或類的方法)上使用@Transactional註解,而不要使用在類所要實現的任何接口上。 你固然能夠在接口上使用@Transactional註解,可是這將只能當你設置了基於接口的代理時它才生效。由於註解是不能繼承 的,這就意味着若是你正在使用基於類的代理時,那麼事務的設置將不能被基於類的代理所識別,並且對象也將不會被事務代理 所包裝(將被確認爲嚴重的)。所以,請接受Spring團隊的建議而且在具體的類上使用@Transactional註解。 注意: 當使用@Transactional風格的進行聲明式事務定義時,你能夠經過 <tx:annotation-driven/> 元素的 proxy-target-class 屬性值來控制是基於接口的仍是基於類的代理被建立。若是 "proxy-target-class" 屬值被設置爲 "true",那麼基於類的代理將起做用(這時須要CGLIB庫cglib.jar在CLASSPATH中)。 若是 "<code class=literal>proxy-target-class</code>" 屬值被設置爲 "<code class=literal>false</code>" 或者這個屬性被省略,那麼標準的JDK基於接口的代理將起做用。 在多數情形下,方法的事務設置將被優先執行。在下列狀況下,例如: <code class=classname>DefaultFooService</code> 類被註解爲只讀事務,可是,這個 類中的 <code class=methodname>updateFoo(Foo)</code> 方法的 <code class=interfacename>@Transactional</code> 註解的事務設置將優先於類級別註解的事務設置。