<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 劉錚 閱讀(935) 評論(0)  編輯  收藏 所屬分類: Spring

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

    導航

    統計

    留言簿(1)

    文章分類(141)

    文章檔案(147)

    搜索

    最新評論

    主站蜘蛛池模板: 亚洲Av永久无码精品黑人 | 免费人成在线观看播放a| 精选影视免费在线 | 69视频在线观看免费| 国产一级淫片免费播放电影| 亚洲国产精品无码久久久秋霞2 | 亚洲成年人免费网站| 自拍偷自拍亚洲精品播放| 最近免费mv在线观看动漫| 最近免费中文字幕视频高清在线看| jlzzjlzz亚洲乱熟在线播放| 亚洲中文无码av永久| jzzjzz免费观看大片免费| 100000免费啪啪18免进| 亚洲欧洲中文日韩av乱码| 亚洲乱人伦精品图片| 国产va免费精品| 成人无遮挡裸免费视频在线观看| 亚洲香蕉网久久综合影视| 亚洲熟妇无码一区二区三区导航| a毛片免费在线观看| 精品免费国产一区二区| 亚洲视频中文字幕在线| 又大又硬又粗又黄的视频免费看 | 亚洲av手机在线观看| 亚洲国产精品张柏芝在线观看| 九九免费久久这里有精品23| 青草草色A免费观看在线| 亚洲色欲久久久综合网| 亚洲αⅴ无码乱码在线观看性色| 久久精品国产这里是免费| 免费在线观看视频a| www.亚洲成在线| 女人体1963午夜免费视频| 亚洲 无码 在线 专区| 亚洲乱码在线观看| 午夜精品免费在线观看| 亚洲人成色77777在线观看大 | 一级片在线免费看| 男女交性永久免费视频播放| 亚洲乱码在线视频|