Spring的交易處理

來源:互聯網
上載者:User
Spring有兩種交易處理方式,編程式交易處理(programmatic transaction management)與聲明式交易處理(declarative transaction management)。

        編程式交易處理比傳統的JDBC事務有所改進,但是還是要實現具體方法。雖然有三種實現Spring編程式交易處理的方法,但其中都必須寫入特定的代碼,這樣做一是代碼量增加,DAO裡面每個方法都加上那些必須要寫上的代碼那也是很煩人的,二是耦合度也提高了,哪天哪天不用Spring用XX也說不定呀....-_-!。

        聲明式事務處事,在DAO中你的對資料庫操作的代碼可以隨意的寫上,不用加任何其它代碼,事務的處理交給Spring的TransactionProxyFactoryBean來代為處理。

 xxDAO:

  1. .......   
  2. //對資料庫的操作      
  3. public void update(String name,String password){      
  4.           //業務處理方法   
  5.           //update1();   
  6.           //update2();      
  7. }     
  8. ......   
 

Spring設定檔

  1. <bean id="dataSource"  
  2.     class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  3.     <property name="driverClassName">  
  4.         <value>com.microsoft.jdbc.sqlserver.SQLServerDrivervalue>  
  5.     property>  
  6.     <property name="url">  
  7.         <value>jdbc:microsoft:sqlserver://localhost:1433/xxvalue>  
  8.     property>  
  9.     <property name="name">  
  10.         <value>usenamevalue>  
  11.     property>  
  12.     <!-- 設定密碼 -->  
  13.     <property name="msg">  
  14.         <value>passwordvalue>  
  15.     property>  
  16. bean>  
  17. <bean id="transactionManager"  
  18.     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.     <property name="dataSource">  
  20.         <ref bean="dataSource" />  
  21.     property>  
  22. bean>  
  23. <bean id="xxDAOProxy"  
  24.     class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  25.     <property name="transactionManager">  
  26.         <ref bean="transactionManager" />  
  27.     property>  
  28.     <property name="target">  
  29.         <ref bean="xxDAO" />  
  30.     property>  
  31.     <property name="transactionAttributes">  
  32.    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.