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

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

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

    Spring中事務的定義:

    一、Propagation :

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

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

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

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

        

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

    spring中的Isolation屬性:
    1、ISOLATION_DEFAULT :使用當前數據源的默認級別
    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標志表示對應的事務應該被最優化為只讀事務。這是一個最優化提示。在一些情況下,一些事務策略能夠起到顯著的最優化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時避免dirty checking(試圖“刷新”)。

    四、Timeout

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

    事務劃分策略

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

    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,readOnlyorg.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIRED </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,readOnlyorg.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>

    posted on 2006-10-17 09:31 國強 閱讀(5985) 評論(0)  編輯  收藏 所屬分類: java-spring


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


    網站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    java

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产L精品国产亚洲区久久| 又粗又长又爽又长黄免费视频 | 四虎影院永久免费观看| 国产a不卡片精品免费观看| 天堂亚洲国产中文在线| 中文字幕av无码不卡免费| 成人免费无码大片A毛片抽搐色欲| 国产亚洲美女精品久久久2020 | 精品无码一级毛片免费视频观看| 国产成人无码免费看视频软件 | 亚欧免费一级毛片| 日韩精品电影一区亚洲| 久久亚洲精品成人无码网站| 一区二区免费电影| 国产精品免费视频一区| 国产高清对白在线观看免费91| 亚洲区日韩区无码区| 亚洲w码欧洲s码免费| 国内外成人免费视频| 亚洲精品网站在线观看你懂的| 国产成人精品免费大全| 亚洲日韩v无码中文字幕| 亚洲一区二区三区高清在线观看| 97精品免费视频| 亚洲成AV人片在线观看无码| 国产成人精品免费大全| 亚洲免费视频网站| 国产成人久久AV免费| 国产偷v国产偷v亚洲高清| 一级女性全黄生活片免费看| 亚洲啪啪AV无码片| 91成年人免费视频| 亚洲AV无码国产精品色| 在线观看免费人成视频| 特级毛片aaaa免费观看| 亚洲熟妇无码久久精品| 日韩免费三级电影| 黄网站在线播放视频免费观看 | 免费精品99久久国产综合精品| 亚洲自偷自偷图片| 69堂人成无码免费视频果冻传媒|