數據加載中……
          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 14:22 當扎瓦 閱讀(520) 評論(0)  編輯  收藏 所屬分類: Spring

          主站蜘蛛池模板: 孟津县| 乌苏市| 汪清县| 临安市| 东丰县| 临邑县| 北京市| 萝北县| 惠安县| 漳浦县| 迭部县| 永德县| 宝坻区| 勐海县| 色达县| 长寿区| 宜昌市| 江西省| 万荣县| 苏尼特右旗| 通河县| 大厂| 广东省| 江源县| 万源市| 七台河市| 赤水市| 北安市| 宁明县| 大名县| 厦门市| 阿坝县| 板桥市| 湖州市| 许昌市| 湖南省| 晴隆县| 古蔺县| 雅江县| 灵川县| 新兴县|