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)

          搜索

          最新評論

          主站蜘蛛池模板: 辽阳市| 桓台县| 石泉县| 黄大仙区| 祁门县| 镶黄旗| 昌乐县| 建阳市| 砀山县| 高州市| 义乌市| 焦作市| 龙游县| 冷水江市| 汉源县| 万载县| 将乐县| 台南市| 大竹县| 怀柔区| 那坡县| 司法| 商丘市| 吉木萨尔县| 谢通门县| 沁源县| 井冈山市| 惠州市| 遂宁市| 内黄县| 恩施市| 巴中市| 那曲县| 军事| 新巴尔虎左旗| 社旗县| 泉州市| 绥中县| 邢台县| 噶尔县| 蒙山县|