數據加載中……
          Spring提供的Hibernate申明式事務管理有兩種辦法
           

          Spring提供的Hibernate申明式事務管理有兩種辦法

          a) 配合使用org.springframework.transaction.interceptor.TransactionInterceptor和org.springframework.orm.hibernate.HibernateTransactionManager,下面是spring reference的例子


          代碼:

           

           

          <beans>
                  
                  
          <bean id="myTransactionManager"
                      class
          ="org.springframework.orm.hibernate.HibernateTransactionManager">
                      
          <property name="sessionFactory">
                      
          <ref bean="mySessionFactory"/>
                      
          </property>
                  
          </bean>
                  
          <bean id="myTransactionInterceptor"
                          class
          ="org.springframework.transaction.interceptor.TransactionInterceptor">
                      
          <property name="transactionManager">
                          
          <ref bean="myTransactionManager"/>
                      
          </property>
                      
          <property name="transactionAttributeSource">
                          
          <value>
                              product.ProductService.increasePrice*=PROPAGATION_REQUIRED
                              product.ProductService.someOtherBusinessMethod=PROPAGATION_MANDATORY
                          
          </value>
                      
          </property>
                  
          </bean>
                  
          <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
                      
          <property name="productDao">
                          
          <ref bean="myProductDao"/>
                      
          </property>
                  
          </bean>
                  
          <bean id="myProductService" class="org.springframework.aop.framework.ProxyFactoryBean">
                      
          <property name="proxyInterfaces">
                          
          <value>product.ProductService</value>
                      
          </property>
                      
          <property name="target">
                          
          <ref local="myProductServiceTarget<"/>
                      
          </property>
                      
          <property name="interceptorNames">
                          
          <list>
                          
          <value>myTransactionInterceptor</value>
                          
          </list>
                      
          </property>
                  
          </bean>
              
          </beans>




           


          HibernateInterceptor和事務無關,它的用途在javadocs中描述如下:
          引用:
          This interceptor binds a new Hibernate Session to the thread before a method
          call, closing and removing it afterwards in case of any method outcome.
          If there already was a pre-bound Session (e.g. from HibernateTransactionManager,
          or from a surrounding Hibernate-intercepted method), the interceptor simply
          takes part in it.


          b)使用TransactionProxyFactoryBean,下面是Spring Reference中的例子
          代碼:

             

          <beans>
                  
                  
          <bean id="myTransactionManager"
                      class
          ="org.springframework.orm.hibernate.HibernateTransactionManager">
                      
          <property name="sessionFactory">
                          
          <ref bean="mySessionFactory"/>
                      
          </property>
                  
          </bean>
                  
          <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
                      
          <property name="productDao">
                          
          <ref bean="myProductDao"/>
                      
          </property>
                  
          </bean>
                  
          <bean id="myProductService"
                      class
          ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                      
          <property name="transactionManager">
                          
          <ref bean="myTransactionManager"/>
                      
          </property>
                      
          <property name="target">
                          
          <ref bean="myProductServiceTarget"/>
                      
          </property>
                      
          <property name="transactionAttributes">
                          
          <props>
                              
          <prop key="increasePrice*">PROPAGATION_REQUIRED</prop>
                              
          <prop key="someOtherBusinessMethod">PROPAGATION_MANDATORY</prop>
                          
          </props>
                      
          </property>
                  
          </bean>
              
          </beans>
              


          在沒有其他AOP interceptor情況下,使用TransactionProxyFactoryBean是比較方便的。
          事務劃分一般是的業務層,而不是在DAO一層。

          2.代理工廠返回的是接口AddressDao的應用,通過接口最終調用target的方法。
          3.TransactionDefinition定義了所有的事務屬性

          posted on 2007-08-15 15:52 當扎瓦 閱讀(134) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 沂源县| 阿城市| 永川市| 金坛市| 二连浩特市| 保康县| 泰宁县| 通许县| 库车县| 大庆市| 六安市| 咸宁市| 肇庆市| 五指山市| 楚雄市| 荆州市| 汤阴县| 龙游县| 黑山县| 台东市| 安塞县| 许昌县| 扶余县| 荃湾区| 湛江市| 吉安市| 萍乡市| 绥江县| 华安县| 吴川市| 图木舒克市| 钟山县| 迁西县| 武鸣县| 赤峰市| 济阳县| 利川市| 义马市| 桃源县| 日喀则市| 巩留县|