PROPAGATION_REQUIRED,readOnlyPROPA" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Spring中事務的定義

系統(tǒng) 1548 0

轉(zhuǎn)載自:http://www.blogjava.net/280211429/articles/75529.html,推薦看原文頁面,原頁面的CSS比較好看,看文字也非常舒服

Spring中事務的定義:

一、Propagation :

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

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

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

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

二、Isolation Level(事務隔離等級):
1、Serializable:最嚴格的級別,事務串行執(zhí)行,資源消耗最大;
2、 REPEATABLE READ: 保證了一個事務不會修改已經(jīng)由另一個事務讀取但未提交(回滾)的數(shù)據(jù)。避免了“臟讀取”和“不可重復讀取”的情況,但是帶來了更多的性能損失。
3、 READ COMMITTED: 大多數(shù)主流數(shù)據(jù)庫的默認事務等級,保證了一個事務不會讀到另一個并行事務已修改但未提交的數(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
事務屬性中的readOnly標志表示對應的事務應該被最優(yōu)化為只讀事務。這是一個最優(yōu)化提示。在一些情況下,一些事務策略能夠起到顯著的最優(yōu)化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時避免dirty checking(試圖“刷新”)。

四、Timeout

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

事務劃分策略

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

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




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

以下為配置實例:

      <!-- 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事務
以下為配置實例:

        <!-- 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事務
如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name給BeanNameAutoProxyCreator,spring就會個給該bean提供事務代理,配置實例如下:

        <!-- 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>
    

Spring中事務的定義


更多文章、技術(shù)交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日本一区二区在线播放 | 亚洲va久久久噜噜噜久久男同 | 三级天堂 | 国产成人精品综合久久久软件 | 九九视频免费观看 | 九九福利 | 国产精品九九 | 国产自愉怕一区二区三区 | 四虎影视免费永久在线观看 | 日日干日日操日日射 | 日本一级毛片在线看 | 久久中文字幕在线观看 | 久久婷婷综合中文字幕 | 久久精品国产主播一区二区 | 色操网 | 午夜国产福利在线 | 欧美天天干 | 久久精品中文字幕第一页 | 久久国产视频网站 | 婷婷激情在线视频 | 国产一区二区三区不卡在线观看 | 亚洲一欧洲中文字幕在线 | 奇米影视色 | 免费国产高清精品一区在线 | 一区二区不卡不卡一卡 | 三a大片 | 久久精品66 | 色综合久久精品中文字幕首页 | 一级黄视频| 国产色婷婷亚洲 | 日韩欧美毛片免费观看视频 | 日本一级特黄毛片高清视频 | 欧美一区二区高清 | 免费的爱爱视频 | 免费99精品国产自在现线观看 | 久久99精品久久久久久秒播放器 | 国产在线干 | 午夜一区 | 久久亚洲综合中文字幕 | 免费高h影片在线观看 | 免费看国产一级特黄aa大片 |