數據加載中……
          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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 如东县| 莱芜市| 开江县| 绩溪县| 台山市| 永春县| 林周县| 新昌县| 无棣县| 朔州市| 丹阳市| 深圳市| 元阳县| 侯马市| 辽源市| 监利县| 海丰县| 灵川县| 靖安县| 蓬莱市| 九江市| 额济纳旗| 陕西省| 丹寨县| 寿宁县| 临城县| 屯门区| 株洲县| 松阳县| 承德县| 阳江市| 杭锦旗| 新化县| 廊坊市| 达州市| 景泰县| 崇义县| 沈丘县| 铜川市| 莫力| 平泉县|