<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 4, comments - 5, trackbacks - 0, articles - 10

    Spring事務(wù)

    Posted on 2005-11-23 10:36 勇敢的心 閱讀(857) 評論(3)  編輯  收藏 所屬分類: Spring
    Spring中事務(wù)的定義:
    一、Propagation :

    對于特定的方法或方法命名模式,代理的具體事務(wù)行為由事務(wù)屬性驅(qū)動,如下面的例子所示:
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="store*">PROPAGATION_REQUIRED</prop>

      key屬性確定代理應(yīng)該給哪個方法增加事務(wù)行為。這樣的屬性最重要的部份是傳播行為。有以下選項可供使用:

    • PROPAGATION_REQUIRED--支持當前事務(wù),如果當前沒有事務(wù),就新建一個事務(wù)。這是最常見的選擇。
    • PROPAGATION_SUPPORTS--支持當前事務(wù),如果當前沒有事務(wù),就以非事務(wù)方式執(zhí)行。
    • PROPAGATION_MANDATORY--支持當前事務(wù),如果當前沒有事務(wù),就拋出異常。
    • PROPAGATION_REQUIRES_NEW--新建事務(wù),如果當前存在事務(wù),把當前事務(wù)掛起。
    • PROPAGATION_NOT_SUPPORTED--以非事務(wù)方式執(zhí)行操作,如果當前存在事務(wù),就把當前事務(wù)掛起。
    • PROPAGATION_NEVER--以非事務(wù)方式執(zhí)行,如果當前存在事務(wù),則拋出異常。

          前六個策略類似于EJB CMT:常量名相同,因此,對EJB開發(fā)人員來說,應(yīng)該立刻就感到熟悉。第七個(PROPAGATION_NESTED)是Spring所提供的一個特殊變量。它要求事務(wù)管理器或者使用JDBC 3.0 Savepoint API提供嵌套事務(wù)行為(如Spring的DataSourceTransactionManager),或者通過JTA支持嵌套事務(wù)。

        

    二、Isolation Level(事務(wù)隔離等級):
    1、Serializable:最嚴格的級別,事務(wù)串行執(zhí)行,資源消耗最大;
    2、REPEATABLE READ:保證了一個事務(wù)不會修改已經(jīng)由另一個事務(wù)讀取但未提交(回滾)的數(shù)據(jù)。避免了“臟讀取”和“不可重復(fù)讀取”的情況,但是帶來了更多的性能損失。
    3、READ COMMITTED:大多數(shù)主流數(shù)據(jù)庫的默認事務(wù)等級,保證了一個事務(wù)不會讀到另一個并行事務(wù)已修改但未提交的數(shù)據(jù),避免了“臟讀取”。該級別適用于大多數(shù)系統(tǒng)。
    4、Read Uncommitted:保證了讀取過程中不會讀取到非法數(shù)據(jù)。

    spring中的Isolation屬性:
    1、ISOLATION_DEFAULT :使用當前數(shù)據(jù)源的默認級別
    2、ISOLATION_READ_UNCOMMITTED :Dirty reads, non-repeatable reads, and phantom reads can occur.
    3、ISOLATION_READ_COMMITTED :Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
    4、ISOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
    5、ISOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.

    三、readOnly
    事務(wù)屬性中的readOnly標志表示對應(yīng)的事務(wù)應(yīng)該被最優(yōu)化為只讀事務(wù)。這是一個最優(yōu)化提示。在一些情況下,一些事務(wù)策略能夠起到顯著的最優(yōu)化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時避免dirty checking(試圖“刷新”)。

    四、Timeout

          在事務(wù)屬性中還有定義“timeout”值的選項,指定事務(wù)超時為幾秒。在JTA中,這將被簡單地傳遞到J2EE服務(wù)器的事務(wù)協(xié)調(diào)程序,并據(jù)此得到相應(yīng)的解釋。

    事務(wù)劃分策略

    1、推薦在業(yè)務(wù)層使用事務(wù),這樣可以允許業(yè)務(wù)層捕獲導(dǎo)致rollback的異常,并拋出恰當?shù)臉I(yè)務(wù)層異常;不在dao層使用事務(wù)是因為這會限制了dao重用其他事務(wù)需求,并且dao層沒有實現(xiàn)業(yè)務(wù)邏輯,并且原子性也是業(yè)務(wù)層的概念。

    spring聲明性事務(wù)的劃分:
    1、有四個地方需要配置:The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.




    2、使用ProxyFactoryBean/Transaction Interceptor(transactionInterceptor)配置spring事務(wù)

    以下為配置實例:

    <!-- The DBCP DataSource -->
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
            destroy-method="close">
        <property name="driverClassName">
          <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url"><value>${jdbc.url}</value></property>
        <property name="username"><value>${jdbc.username}</value></property>
        <property name="password"><value>${jdbc.password}</value></property>
      </bean>
       
      <!-- The DAO class -->
      <bean id="dao" 
    class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property> 
      </bean>
       
      <!-- The transactionmanager to use for regular non JTA datasource -->
      <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property> 
      </bean>
       
      <!-- TransactionInterceptor -->
      <bean id="transactionInterceptor" 
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
          <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributeSource">
          <value>
    org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,re
    adOnly
    org.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIR
    ED
          </value>
        </property>
      </bean>  
       
      <!-- Transactional proxy for the primary business object -->
      <bean id="boxOffice" 
            class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target">
          <ref local="boxOfficeTarget"/>
        </property>
        <property name="proxyInterfaces">
          <value>org.springframework.prospring.ticket.service.BoxOffice</value>
        </property>
        <property name="interceptorNames">
          <value>transactionInterceptor</value>
        </property>
      </bean>  
       
      <!-- Business Object -->
      <bean id="boxOfficeTarget" 
        class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
        <property name="boxOfficeDao">
          <ref local="dao"/>
        </property> 
      </bean>

    3、使用TransactionProxyFactoryBean配置spring事務(wù)
    以下為配置實例:

      <!-- The DBCP DataSource -->
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
            destroy-method="close">
        <property name="driverClassName">
          <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url"><value>${jdbc.url}</value></property>
        <property name="username"><value>${jdbc.username}</value></property>
        <property name="password"><value>${jdbc.password}</value></property>
      </bean>
       
      <!-- The DAO class -->
      <bean id="dao"
    class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property> 
      </bean>
       
      <!-- The transactionmanager to use for regular non JTA datasource -->
      <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property> 
      </bean>
       
      <!-- Transactional proxy and the primary business object -->
      <bean id="boxOffice" 
       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="target">
          <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
            <property name="boxOfficeDao">
              <ref local="dao"/>
            </property> 
          </bean>
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
            <prop key="allocate*">PROPAGATION_REQUIRED</prop>
          </props>
        </property>
      </bean>  

    4、使用BeanNameAutoProxyCreator配置spring事務(wù)
    如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name給BeanNameAutoProxyCreator,spring就會個給該bean提供事務(wù)代理,配置實例如下:

      <!-- The DBCP DataSource -->
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
          destroy-method="close">
        <property name="driverClassName">
          <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url"><value>${jdbc.url}</value></property>
        <property name="username"><value>${jdbc.username}</value></property>
        <property name="password"><value>${jdbc.password}</value></property>
      </bean>
       
      <!-- The DAO class -->
      <bean id="dao"
    class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property> 
      </bean>
       
      <!-- The transactionmanager to use for regular non JTA datasource -->
      <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
          <ref local="dataSource"/>
        </property> 
      </bean> 
       
      <!-- TransactionInterceptor -->
      <bean id="transactionInterceptor" 
            class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
          <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributeSource">
          <value>
    org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS
    ,readOnly
    org.springframework.prospring.ticket.service.BoxOffice.allocate*=
    PROPAGATION_REQUIRED
          </value>
        </property>
      </bean>  
       
      <!-- BeanNameAutoProxyCreator -->
    <bean id="autoProxyCreator" 
       class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="interceptorNames">
        <value>transactionInterceptor</value>
      </property>
      <property name="beanNames">
        <list>
          <idref local="boxOffice"/>
        </list>
      </property>
    </bean>  
       
    <!-- Business Object -->
    <bean id="boxOffice"
       class="org.springframework.prospring.ticket.service.BoxOfficeImpl">
      <property name="boxOfficeDao">
        <ref local="dao"/>
      </property> 
    </bean>

    Feedback

    # re: Spring事務(wù)  回復(fù)  更多評論   

    2006-11-10 17:41 by loocky
    這篇文章很不錯

    # re: Spring事務(wù)  回復(fù)  更多評論   

    2007-04-05 11:35 by eric77
    不錯,不錯. very nice!

    # re: Spring事務(wù)  回復(fù)  更多評論   

    2013-11-05 11:18 by yobin
    ~~

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久精品国产亚洲av麻豆图片| 伊人久久亚洲综合| 亚洲视频在线不卡| 光棍天堂免费手机观看在线观看| 亚洲成a人在线看天堂无码| 老牛精品亚洲成av人片| 国产成人免费手机在线观看视频| 亚洲男同gay片| 国产成人在线免费观看| 亚洲精品女同中文字幕| 国产精品深夜福利免费观看 | 国产国拍亚洲精品mv在线观看 | 亚洲另类视频在线观看| 99国产精品永久免费视频| 亚洲一区二区三区免费在线观看| 91视频国产免费| 亚洲欧美日韩中文字幕一区二区三区 | 一级做a爱片特黄在线观看免费看| 亚洲 综合 国产 欧洲 丝袜| 人成电影网在线观看免费| 亚洲爽爽一区二区三区| 拍拍拍无挡免费视频网站| 久久久久亚洲AV成人无码| 8x8x华人永久免费视频| 精品亚洲国产成人| 日韩精品成人亚洲专区| 你是我的城池营垒免费看| 亚洲视频一区二区在线观看| 成年美女黄网站色大免费视频| 亚洲AV无码一区二区乱子仑| 亚洲国产成人久久一区久久| 日韩精品免费视频| 亚洲日韩一区二区三区| 国产a v无码专区亚洲av| 无码囯产精品一区二区免费 | 亚洲日本一区二区一本一道| 免费看搞黄视频网站| 国产精品亚洲专区在线观看| heyzo亚洲精品日韩| 91久久青青草原线免费| 国产精品成人亚洲|