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

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

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

    Spring 中的事務管理學習心得

    Spring 中的事務管理學習心得


    可以使用<bean id="autoproxy"class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
       …
    </bean>

    進行自動代理,但是這里要注意的是自動代理針對的是advisor,advisor是由advice和pointcut(interceptor也是advice的一種)所組成的,所以單獨的interceptor不能使用,也就是說TransactionInterceptor不能使用。
    所以應該建立advisor,在事務管理中也就是TransactionAttributeSourceAdvisor。

    TransactionAttributeSourceAdvisor中需要配置TransactionInterceptor。
    配置示例如下:
    <bean id="transactionAdvisor"
    class="org.springframework.transaction.interceptor.
    TransactionAttributeSourceAdvisor">
    <property name="transactionInterceptor">
    <ref bean="transactionInterceptor"/>
    </property>
    </bean>

    TransactionInterceptor的配置如下:
    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.
    TransactionInterceptor">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributeSource">
    <ref bean="transactionAttributeSource"/>
    </property>
    </bean>

    其中transactionManager的配置根據所使用的存儲方法不同而不同:

    使用jdbc的配置如下:
    <bean id="transactionManager" class="org.springframework.jdbc.
    datasource.DataSourceTransactionManager">
    <property name="dataSource">
    <ref bean="dataSource"/>
    </property>
    </bean>

     使用hibernate的配置如下:
    <bean id="transactionManager" class="org.springframework.
    orm.hibernate.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    而transactionAttributeSource是一個接口,一般使用NameMatchTransactionAttributeSource實現此接口
    <bean id="transactionAttributeSource"
    class="org.springframework.transaction.interceptor.
    NameMatchTransactionAttributeSource">
    <property name="properties">
    <props>
    <prop key="get*">PROPAGATION_SUPPORTS</prop>
    </props>
    </property>
    </bean>
    注意其中的properties屬性是一個name/attribute 的map其中name是針對需要進行的事務管理的方法attribute是事務管理的屬性由TransactionAttribute決定
    而TransactionAttribute擴展的事務定義基本類TransactionDefinition,在這個基本類上面加上了boolean rollbackOn(Throwable ex)方法
    所以<prop key="get*">PROPAGATION_SUPPORTS</prop>的attribute還可以添加:
    如<prop key="get*">PROPAGATION_SUPPORTS,ISOLATION_SERIALIZABLE ,readonly</prop>



    以上是從宏觀到微觀的分析,
    以下是從微觀到宏觀的分析:

    1。設置事務管理屬性
    <bean id="transactionAttributeSource"
    class="org.springframework.transaction.interceptor.
    NameMatchTransactionAttributeSource">
    <property name="properties">
    <props>
    <prop key="get*v>PROPAGATION_SUPPORTS</prop>
    </props>
    </property>
    </bean>  
    2。設置事務管理器,根據所使用的存儲方法不同而不同:以hibernate為例

    <bean id="transactionManager" class="org.springframework.
    orm.hibernate.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    3。設置interceptor
    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.
    TransactionInterceptor">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributeSource">
    <ref bean="transactionAttributeSource"/>
    </property>
    </bean>

    4。最后設置advisor:
    <bean id="transactionAdvisor"
    class="org.springframework.transaction.interceptor.
    TransactionAttributeSourceAdvisor">
    <property name="transactionInterceptor">
    <ref bean="transactionInterceptor"/>
    </property>
    </bean>
    所以就可以通過自動代理進行代理了,
    注意這里的advisor還是有pointcut的,pointcut在事務管理中設置了,例如:
    <props>
    <prop key="get*">PROPAGATION_SUPPORTS</prop>
    </props>
    還有由于Spring本身的框架的原因,如果在Log4j中的調試級別為debug的話,那么設置advisor時就不能使用property方法,而要使用constructor-arg方法





    使用MethodMapTransactionAttributeSource可以對特定的類的方法進行事務管理
    設置property------setMethodMap
    Set a name/attribute map, consisting of "FQCN.method" method names (e.g. "com.mycompany.mycode.MyClass.myMethod") and TransactionAttribute instances (or Strings to be converted to TransactionAttribute instances).

    posted on 2007-09-10 17:18 劉錚 閱讀(936) 評論(0)  編輯  收藏 所屬分類: Spring

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    留言簿(1)

    文章分類(141)

    文章檔案(147)

    搜索

    最新評論

    主站蜘蛛池模板: 精品久久久久久久免费人妻 | 亚洲第一福利网站| 免费国产黄网站在线看| 午夜时刻免费入口| 免费国产成人高清视频网站| 亚洲欧洲另类春色校园网站| 4hu四虎最新免费地址| 久久丫精品国产亚洲av不卡 | 免费一级全黄少妇性色生活片| 亚洲成在人线电影天堂色| 色欲A∨无码蜜臀AV免费播| 久久亚洲精品中文字幕三区| 你懂得的在线观看免费视频| 国产福利电影一区二区三区,亚洲国模精品一区 | 亚洲国产第一站精品蜜芽| 亚洲国产高清美女在线观看| 182tv免费视视频线路一二三| 青青草国产免费久久久91| 亚洲日韩中文字幕在线播放| 丰满人妻一区二区三区免费视频| 一区二区三区免费视频观看| 亚洲美女又黄又爽在线观看| 69影院毛片免费观看视频在线| 免费人成视网站在线观看不卡| 亚洲综合久久久久久中文字幕| a高清免费毛片久久| 四虎影院在线免费播放| 色网站在线免费观看| 成年女人色毛片免费看| 日韩在线视频播放免费视频完整版| 女人被弄到高潮的免费视频| 午夜免费国产体验区免费的| 亚洲第一视频网站| 免费观看一级毛片| a毛片视频免费观看影院| 亚洲jjzzjjzz在线观看| 一本久久综合亚洲鲁鲁五月天 | 亚洲无人区一区二区三区| 亚洲精品在线免费观看视频| 亚洲AV成人无码网站| 国产精一品亚洲二区在线播放|