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

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 永仁县| 连城县| 托克逊县| 天长市| 文水县| 丽江市| 四子王旗| 湟中县| 台东县| 南康市| 新安县| 班玛县| 甘泉县| 商都县| 开鲁县| 托克托县| 法库县| 镇沅| 策勒县| 开江县| 开远市| 苏州市| 鄂托克旗| 岐山县| 梧州市| 内乡县| 西乌珠穆沁旗| 海丰县| 大宁县| 定安县| 土默特左旗| 临清市| 贵德县| 卢湾区| 宁阳县| 大新县| 石渠县| 镇宁| 平谷区| 平潭县| 石城县|