數(shù)據(jù)加載中……
          Spring提供的Hibernate申明式事務(wù)管理有兩種辦法
           

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

          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和事務(wù)無(wú)關(guān),它的用途在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>
              


          在沒(méi)有其他AOP interceptor情況下,使用TransactionProxyFactoryBean是比較方便的。
          事務(wù)劃分一般是的業(yè)務(wù)層,而不是在DAO一層。

          2.代理工廠返回的是接口AddressDao的應(yīng)用,通過(guò)接口最終調(diào)用target的方法。
          3.TransactionDefinition定義了所有的事務(wù)屬性

          posted on 2007-08-15 14:22 當(dāng)扎瓦 閱讀(519) 評(píng)論(0)  編輯  收藏 所屬分類: Spring


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 盘锦市| 金湖县| 洛隆县| 饶阳县| 利津县| 大宁县| 临夏县| 皋兰县| 台前县| 兰州市| 常熟市| 潍坊市| 根河市| 昌黎县| 聊城市| 濮阳县| 平度市| 滨州市| 宿迁市| 渑池县| 建昌县| 称多县| 元谋县| 饶平县| 白城市| 澜沧| 贞丰县| 灵山县| 迁西县| 方城县| 左权县| 桑植县| 崇礼县| 毕节市| 西贡区| 大连市| 青神县| 英吉沙县| 庐江县| 东安县| 双流县|