IBATIS交易處理 - - 部落格頻道 - CSDN.NET

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   for   

IBATIS交易處理 - - 部落格頻道 - CSDN.NET

? ? ? ? iBATIS交易處理是和Dao緊密相聯的。?

? ? ? ? 在使用Dao時,如以下代碼,先插入新記錄,再進行更新:?

? ? ? ? UserDao.insertUser (user); // Starts transaction?

? ? ? ? user.setName("wh");?

? ? ? ? UserDao.updateUser (user); // Starts a new transaction

? ? ? ?因為沒有顯式地啟動事務,iBatis會認為這是兩次事務,分別從串連池中取兩次Connection。我們所寫的Dao子類(繼承自com.ibatis.dao.client.template.SqlMapDaoTemplate)的每一個Dao方法已經預設為一個事務(通過動態代理)。這樣的事務是隱含交易.

? ? ? ? iBatis是通過DaoManager類來統管Dao子類的事務,

? ? ? ? 眾Dao子類由DaoManager產生,如下:

? ? ? ? Reader reader =Resources.getResourceAsReader("dao.xml");?

? ? ? ? DaoManager ?daoManager =DaoManagerBuilder.buildDaoManager(reader);??

? ? ? ? UserDao userDao = (UserDao) daoManager.getDao(UserDao.class);

? ? ? ? UserDao是使用者自己定義的介面,獲得的其實是在dao.xml中指定的相對應的 SqlMapDao實作類別,從而實現了松藕合。在良好的分層設計中,

?

? ? ? ? 但是一般的事務需要放到業務層,因為一個業務需要具有原子性,事務放到Dao層是不能達到業務一致的效果的,那麼如果想要把事務放到業務層,就需要在業務層使用顯示事務進行聲明處理.

?

? ? ? ? 顯式地聲明交易處理語句,如下:

? ? ? ? try {?

? ? ? ? ? ? ? ? daoManager.startTransaction();?

? ? ? ? ? ? ? ? UserDao.insertUser (user);??

? ? ? ? ? ? ? ? user.setName("wh");?

? ? ? ? ? ? ? ? UserDao.updateUser(user);??

? ? ? ? ? ? ? ? otherDao.doSomething(other);?

? ? ? ? ? ? ? ? ?...?

? ? ? ? ? ? ? ? daoManager.commitTransaction();?

? ? ? ?} finally {?

? ? ? ? ? ? ? ? daoManager.endTransaction();?

? ? ? ?}

? ? ? ?這樣就保持了原子性,整體為一個事務,要麼全部執行成功,否則復原。

?

? ? ? ?現在唯一的問題就是,dao層的事務是否已經放棄,否則產生事務嵌套問題對效能會有影響.?

? ? ? ?當然,iBatis 完全可以這麼做:建一個聲明式介面:IService,再使用動態代理,將使用者自己的Serivce子類通過動態代理自動包上交易處理的代碼,預設每一個業務方法為一個事務。?

? ? ? ?大師的心如果能輕易揣測,就是不大師了:),估計大師認為這樣屬於過度設計,他認為把這種靈活性交給使用者是合適的,相當多的service 方法只調用一個Dao方法,例如CRUD操作。?

? ? ? ?再補充一下,iBatis中對事務的處理是可配置的,最常用的Type是"JDBC",也可以聲明為"JTA"或"EXTERNAL".

?

? ? ? ?項目裡常用Spring與Ibatis配合使用,這樣可以在Spring裡配置交易管理,可以省去業務層的顯示事務代碼.

? ? ? ? spring的設定檔的基本寫法為:

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
  3. ???xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"???
  4. ???xmlns:context="http://www.springframework.org/schema/context"???
  5. ???xsi:schemaLocation="????
  6. ??????????http://www.springframework.org/schema/beans?????
  7. ??????????http://www.springframework.org/schema/beans/spring-beans-2.0.xsd??
  8. ??????????http://www.springframework.org/schema/aop?????
  9. ??????????http://www.springframework.org/schema/aop/spring-aop-2.0.xsd????
  10. ??????????http://www.springframework.org/schema/tx?????
  11. ??????????http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">??
  12. ???????????
  13. ???????????
  14. <bean?id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"?>??
  15. ???<property?name="driverClassName"value="com.mysql.jdbc.Driver"?/>??
  16. ???<property?name="url"value="jdbc:mysql://10.11.0.145:3306/carrefour?characterEncoding=gb2312"/>??
  17. ???<property?name="username"?value="dev01"?/>??
  18. ???<property?name="password"?value="123456"/>??
  19. </bean>??
  20. <bean?id="sqlClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">??
  21. ???<property?name="dataSource">??
  22. ??????<ref?local="dataSource"?/>??
  23. ???</property>??
  24. ???<property?name="configLocation">??
  25. ??????<value>classpath:sqlmaps.xml</value>??
  26. ???</property>??
  27. </bean>??
  28. <!--配置交易管理員-->??
  29. <bean?id="transactionManager"?class="org.springframework.jdbc.datasource.DataSourceTransactionManager">??
  30. ????????<propertynamepropertyname="dataSource"?ref="dataSource"></property>??
  31. </bean>??
  32. <!--配置哪些方法,什麼情況下需要復原-->??
  33. <tx:advice?id="serviceAdvice"transaction-manager="transactionManager">???
  34. ????<tx:attributes>????
  35. ????????<!--當代理的service層中的方法拋出異常的時候才復原,必須加rollback-for參數-->??
  36. ????????<tx:methodnametx:methodname="insert*"?propagation="REQUIRED"rollback-for="Throwable"/>??
  37. ????????<tx:methodnametx:methodname="del*"?propagation="REQUIRED"rollback-for="Throwable"/>???
  38. ????????<tx:methodnametx:methodname="update*"?propagation="REQUIRED"rollback-for="Throwable"/>???
  39. ????????<!--除了上面標識的方法,其他方法全是唯讀方法-->??
  40. ????????<tx:methodnametx:methodname="*"?read-only="true"/>???
  41. ????</tx:attributes>???
  42. </tx:advice>???
  43. <!--?配置哪些類的方法需要進行交易管理?-->???
  44. <aop:config?proxy-target-class="true">???
  45. <aop:pointcut?id="servicePointcut"expression="execution(*?com.wh.service.*.*(..))"/>???
  46. <aop:advisor?pointcut-ref="servicePointcut"advice-ref="serviceAdvice"/>???
  47. </aop:config>??
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"    xmlns:context="http://www.springframework.org/schema/context"    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.apache.commons.dbcp.BasicDataSource" >   <property name="driverClassName"value="com.mysql.jdbc.Driver" />   <property name="url"value="jdbc:mysql://10.11.0.145:3306/carrefour?characterEncoding=gb2312"/>   <property name="username" value="dev01" />   <property name="password" value="123456"/></bean><bean id="sqlClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   <property name="dataSource">      <ref local="dataSource" />   </property>   <property name="configLocation">      <value>classpath:sqlmaps.xml</value>   </property></bean><!--配置交易管理員--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <propertyname="dataSource" ref="dataSource"></property></bean><!--配置哪些方法,什麼情況下需要復原--><tx:advice id="serviceAdvice"transaction-manager="transactionManager">     <tx:attributes>          <!--當代理的service層中的方法拋出異常的時候才復原,必須加rollback-for參數-->        <tx:methodname="insert*" propagation="REQUIRED"rollback-for="Throwable"/>        <tx:methodname="del*" propagation="REQUIRED"rollback-for="Throwable"/>         <tx:methodname="update*" propagation="REQUIRED"rollback-for="Throwable"/>         <!--除了上面標識的方法,其他方法全是唯讀方法-->        <tx:methodname="*" read-only="true"/>     </tx:attributes> </tx:advice> <!-- 配置哪些類的方法需要進行交易管理 --> <aop:config proxy-target-class="true"> <aop:pointcut id="servicePointcut"expression="execution(* com.wh.service.*.*(..))"/> <aop:advisor pointcut-ref="servicePointcut"advice-ref="serviceAdvice"/> </aop:config>

? ? ? ? ?這是在spring+Ibatis的情況下, ,通過aop控制需要事務的包和具體方法,將事務控制在service層,來達到事務在業務層提交和復原.保持業務的原子性.

來自為知筆記(Wiz)

IBATIS交易處理 - - 部落格頻道 - CSDN.NET

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.