隨筆-13  評論-9  文章-9  trackbacks-0
          統觀spring事務,圍繞著兩個核心PlatformTransactionManager和TransactionStatus

          spring提供了幾個關于事務處理的類:?
          TransactionDefinition?//事務屬性定義
          TranscationStatus?//代表了當前的事務,可以提交,回滾。
          PlatformTransactionManager這個是spring提供的用于管理事務的基礎接口,其下有一個實現的抽象類AbstractPlatformTransactionManager,我們使用的事務管理類例如DataSourceTransactionManager等都是這個類的子類。

          一般事務定義步驟:
          TransactionDefinition td = new TransactionDefinition();
          TransactionStatus ts = transactionManager.getTransaction(td);
          try
          { //do sth
          ? transactionManager.commit(ts);
          }
          catch(Exception e){transactionManager.rollback(ts);}
          ?

          spring提供的事務管理可以分為兩類:編程式的和聲明式的。編程式的,比較靈活,但是代碼量大,存在重復的代碼比較多;聲明式的比編程式的更靈活。

          編程式主要使用transactionTemplate。省略了部分的提交,回滾,一系列的事務對象定義,需注入事務管理對象.
          void add()
          {
          ??? transactionTemplate.execute( new TransactionCallback(){
          ??????? pulic Object doInTransaction(TransactionStatus ts)
          ?????? { //do sth}
          ??? }
          }

          聲明式:
          使用TransactionProxyFactoryBean:
          <bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
          ??<property name="transactionManager"><ref bean="transactionManager"/></property>
          ??<property name="target"><ref local="userManagerTarget"/></property>
          ??<property name="transactionAttributes">
          ???<props>
          ????<prop key="insert*">PROPAGATION_REQUIRED</prop>
          ????<prop key="update*">PROPAGATION_REQUIRED</prop>
          ????<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          ???</props>
          ??</property>
          ?</bean>

          圍繞Poxy的動態代理 能夠自動的提交和回滾事務
          org.springframework.transaction.interceptor.TransactionProxyFactoryBean
          • PROPAGATION_REQUIRED--支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。
          • PROPAGATION_SUPPORTS--支持當前事務,如果當前沒有事務,就以非事務方式執行。
          • PROPAGATION_MANDATORY--支持當前事務,如果當前沒有事務,就拋出異常。
          • PROPAGATION_REQUIRES_NEW--新建事務,如果當前存在事務,把當前事務掛起。
          • PROPAGATION_NOT_SUPPORTED--以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
          • PROPAGATION_NEVER--以非事務方式執行,如果當前存在事務,則拋出異常。
          • PROPAGATION_NESTED--如果當前存在事務,則在嵌套事務內執行。如果當前沒有事務,則進行與PROPAGATION_REQUIRED類似的操作。
          posted on 2006-06-23 15:35 Dragonofson 閱讀(14332) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 锦州市| 吉木乃县| 绵竹市| 友谊县| 盘山县| 奇台县| 东山县| 凯里市| 隆尧县| 合肥市| 金门县| 蒙自县| 大渡口区| 中山市| 巫溪县| 怀宁县| 凌海市| 凉山| 琼结县| 中山市| 鄄城县| 时尚| 武山县| 那坡县| 潍坊市| 武邑县| 布拖县| 奉贤区| 恩平市| 建平县| 盐亭县| 霞浦县| 罗江县| 基隆市| 辽源市| 庆元县| 梅河口市| 肥城市| 茌平县| 鹰潭市| 佳木斯市|