日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Spring中的TopLinkServerSession

SessionFactory 抽象層
TopLink本身并沒有提供SessionFactory抽象層邏輯,多線程的數(shù)據(jù)訪問是建立在中央 ServerSession 上的。對(duì)于單線程訪問, 這個(gè)中央 ServerSession 會(huì)為它一個(gè) ClientSession 的實(shí)例供其使用。為了提供靈活便捷的創(chuàng)建選項(xiàng), Spring為TopLink定義了一個(gè) SessionFactory 接口,從而使你可以任意地在不同的 Session 創(chuàng)建策略之間進(jìn)行切換。

作為一個(gè)一站式的商店,Spring提供了一個(gè) LocalSessionFactoryBean 類,允許你以bean風(fēng)格的配置方式來定義一個(gè)TopLink SessionFactory。 需要進(jìn)行配置的地方主要是TopLink session配置文件,通常來說還需配置一個(gè)受到Spring管理的JDBC DataSource。

  1.  
  2.  
  3.      
  4.      
  5.      
  6.      
  7. bean> 
  8.  
  9.      
  10.      
  11. bean> 
  12.      
  13. beans> 
  14.  
  15.  
  16.     Session name> 
  17.     toplink-mappings.xml project-xml> 
  18.      
  19.        
  20.      session-type> 
  21.     true enable-logging> 
  22.      
  23. session> 
  24. toplink-configuration>  

通常情況下,LocalSessionFactoryBean 在底層將持有一個(gè)多線程的TopLink ServerSession 并創(chuàng)建合適的客戶端 Session: 它或者是一個(gè)普通的 Session(典型情況) —— 一個(gè)受管理的 ClientSession;或者是一個(gè)具備事務(wù)功能的 Session (后者主要在Spring內(nèi)部對(duì)TopLink的支持中被使用)。還有一種情況,LocalSessionFactoryBean 可能會(huì)持有一個(gè)單線程的TopLink的 DatabaseSession,這是非常特殊的情況了。

TopLinkTemplate and TopLinkDaoSupport
每個(gè)基于TopLink的DAO將通過IoC被注入一個(gè) SessionFactory,你可以通過Setter方式注入,也可以用構(gòu)造函數(shù)方式注入。這樣的DAO可以直接操作原生的TopLink API,通過 SessionFactory 來獲取一個(gè) Session, 但是通常情況下,你更愿意使用Spring的 TopLinkTemplate:

  1.  
  2.  id="myProductDao" class="product.ProductDaoImpl"> 
  3.      name="sessionFactory" ref="mySessionFactory"/> 
  4. bean> 
  5. beans> 
  6. public class TopLinkProductDao implements ProductDao {  
  7.     private TopLinkTemplate tlTemplate;  
  8.     public void setSessionFactory(SessionFactory sessionFactory) {  
  9.         this.tlTemplate = new TopLinkTemplate(sessionFactory);  
  10.     }  
  11.     public Collection loadProductsByCategory(final String category)                                            throws DataAccessException {  
  12.       return (Collection) this.tlTemplate.execute(new TopLinkCallback() {  
  13.        public Object doInTopLink(Session session) throws TopLinkException {  
  14.         ReadAllQuery findOwnersQuery = new ReadAllQuery(Product.class);  
  15.         findOwnersQuery.addArgument("Category");  
  16.         ExpressionBuilder builder = this.findOwnersQuery.getExpressionBuilder();  
  17.          findOwnersQuery.setSelectionCriteria(  
  18.               builder.get("category").like(builder.getParameter("Category")));  
  19.                 Vector args = new Vector();  
  20.                 args.add(category);  
  21.                 List result = session.executeQuery(findOwnersQuery, args);  
  22.                 // do some further stuff with the result list  
  23.                 return result;  
  24.             }  
  25.         }
  26.     }  
  27. }  

一個(gè)回調(diào)的實(shí)現(xiàn)能夠有效地在任何TopLink數(shù)據(jù)訪問中使用。TopLinkTemplate 會(huì)確保當(dāng)前的 Session 對(duì)象的正確打開和關(guān)閉,并自動(dòng)參與到事務(wù)管理中去。 Template實(shí)例不僅是線程安全的,同時(shí)它也是可重用的。因而他們可以作為外部對(duì)象的實(shí)例變量而被持有。對(duì)于那些簡(jiǎn)單的諸如 executeQuery、readAll、readById 和 merge 操作的調(diào)用,TopLinkTemplate提供可選擇的快捷函數(shù)來替換這種回調(diào)的實(shí)現(xiàn)。 不僅如此,Spring還提供了一個(gè)簡(jiǎn)便的 TopLinkDaoSupport 基類,這個(gè)類提供了 setSessionFactory(..) 方法來接受一個(gè) SessionFactory 對(duì)象,同時(shí)提供了 getSessionFactory() 和 getTopLinkTemplate() 方法給子類使用。綜合了這些,對(duì)于那些典型的業(yè)務(wù)需求,就有了一個(gè)非常簡(jiǎn)單的DAO實(shí)現(xiàn)。

  1. public class ProductDaoImpl extends TopLinkDaoSupport implements ProductDao {  
  2.     public Collection loadProductsByCategory(String category) throws DataAccessException {  
  3.         ReadAllQuery findOwnersQuery = new ReadAllQuery(Product.class);  
  4.         findOwnersQuery.addArgument("Category");  
  5.         ExpressionBuilder builder = this.findOwnersQuery.getExpressionBuilder();  
  6.         findOwnersQuery.setSelectionCriteria(  
  7.             builder.get("category").like(builder.getParameter("Category")));  
  8.         return getTopLinkTemplate().executeQuery(findOwnersQuery, new Object[] {category});  
  9.     }  
  10. }  

邊注:TopLink查詢對(duì)象是線程安全的,并且能夠在DAO層被緩存。在一開始被創(chuàng)建時(shí)以實(shí)例變量的方式被保持。

作為不使用Spring的 TopLinkTemplate 來實(shí)現(xiàn)DAO的替代解決方案, 你依然可以通過原生TopLink API對(duì)那些基于Spring的DAO進(jìn)行編程,此時(shí)你必須明確地打開和關(guān) 閉一個(gè) Session。正如在相應(yīng)的Hibernate章節(jié)描述的一樣,這種做法的主要優(yōu)點(diǎn)在于你的數(shù)據(jù)訪問代碼可以在整個(gè)過程中拋出checked exceptions。 TopLinkDaoSupport 為這種情況提供了多種函數(shù)支持,包括獲取和釋放 一個(gè)具備事務(wù)的 Session 并做相關(guān)的異常轉(zhuǎn)化。

基于原生的TopLink API的DAO實(shí)現(xiàn)
我們可以直接操作TopLink API來實(shí)現(xiàn)DAO,直接使用一個(gè)注入的 Session 而無需對(duì)Spring產(chǎn)生的任何依賴。它通常基于一個(gè)由 LocalSessionFactoryBean 定義的 SessionFactory,并通過Spring的 TransactionAwareSessionAdapter 暴露成為一個(gè) Session 類型的引用。

TopLink的 Session 接口中定義的 getActiveSession() 方法將返回當(dāng)前具備事務(wù)管理功能的 Session 對(duì)象。如果當(dāng)前沒有處于活躍狀態(tài)的事務(wù), 這個(gè)函數(shù)將返回一個(gè)共享的TopLink ServerSession,也就是說,這種情況應(yīng)該只是一個(gè)直接使用的只讀訪問。另外還有一個(gè) getActiveUnitOfWork() 方法, 返回TopLink的與當(dāng)前事務(wù)綁定的 UnitOfWork (如果沒有當(dāng)前事務(wù)則返回 null)。

一個(gè)相應(yīng)的DAO實(shí)現(xiàn)類看上去就像下面那樣:

  1. public class ProductDaoImpl implements ProductDao {  
  2.     private Session session;  
  3.     public void setSession(Session session) {  
  4.         this.session = session;  
  5.     }  
  6.     public Collection loadProductsByCategory(String category) {  
  7.         ReadAllQuery findOwnersQuery = new ReadAllQuery(Product.class);  
  8.         findOwnersQuery.addArgument("Category");  
  9.         ExpressionBuilder builder = this.findOwnersQuery.getExpressionBuilder();  
  10.         findOwnersQuery.setSelectionCriteria(  
  11.             builder.get("category").like(builder.getParameter("Category")));  
  12.         Vector args = new Vector();  
  13.         args.add(category);  
  14.         return session.getActiveSession().executeQuery(findOwnersQuery, args);  
  15.     }  
  16. }  

上面我們所列出的DAO完全遵循IoC:它如同使用Spring的 TopLinkTemplate 進(jìn)行編碼那樣,非常適合在application context中進(jìn)行配置。Spring的 TransactionAwareSessionAdapter 將暴露一個(gè) Session 類型的bean的引用,并傳入到DAO中去:

  1.  
  2.  id="mySessionAdapter" 
  3.       class="org.springframework.orm.toplink.support.                                                            TransactionAwareSessionAdapter"> 
  4.      name="sessionFactory" ref="mySessionFactory"/> 
  5. bean> 
  6.  id="myProductDao" class="product.ProductDaoImpl"> 
  7.      name="session" ref="mySessionAdapter"/> 
  8. bean> 
  9. beans>  

這種DAO風(fēng)格的主要好處在于它僅僅依賴于TopLink自身的API,而無需引入任何的Spring 的類。從無入侵性的角度來看,這一點(diǎn)非常吸引人。同時(shí),對(duì)于TopLink的開發(fā)人員來說也更自然。

然而,這樣的DAO訪問方式會(huì)拋出 TopLinkException (這是一個(gè)無需聲明或捕獲的unchecked exception),這意味著,DAO的調(diào)用者只能以普通的錯(cuò)誤來處理這些異常,除非完全依賴TopLink自身的異常體系。因而,除非你將DAO的調(diào)用者綁定到具體的實(shí)現(xiàn)策略上去,否則你將無法捕獲特定的異常原因(諸如樂觀鎖異常)。這種折中平衡或許可以被接受,如果你的應(yīng)用完全基于TopLink或者無需進(jìn)行特殊的異常處理。

這樣的DAO風(fēng)格有一個(gè)不利因素在于TopLink的標(biāo)準(zhǔn)的 getActiveSession() 函數(shù)僅僅在JTA事務(wù)中有效。而對(duì)于其他的事務(wù)管理策略尤其時(shí)本地的TopLink事務(wù),它將 無法 工作。

幸運(yùn)的是,Spring的 TransactionAwareSessionAdapter 為TopLink ServerSession 暴露了一個(gè)相應(yīng)的代理類。 這個(gè)代理類能夠在任何的事務(wù)策略之上支持TopLink的 Session.getActiveSession() 和 Session.getActiveUnitOfWork() 函數(shù),返回當(dāng)前收到Spring管理 (即便由 TopLinkTransactionManager 管理)的具備事務(wù)管理功能的 Session 實(shí)例。當(dāng)然,這個(gè)函數(shù)的標(biāo)準(zhǔn)行為依然有效:返回與當(dāng)前的JTA事務(wù)綁定的 Session 對(duì)象。 (無論這個(gè)JTA事務(wù)是由Spring的 JtaTransactionManager、 EJB CMT或者普通的JTA所驅(qū)動(dòng)的事務(wù))。

總體來說,DAO可以基于TopLink的原生API實(shí)現(xiàn),同時(shí),它依舊需要能夠參與到Spring的事務(wù)管理中。這對(duì)于那些已經(jīng)對(duì)TopLink非常熟悉的人來說很有吸引力,因?yàn)檫@種方式更加自然。不過,這種DAO將拋出 TopLinkException,因而,如果有必要的話需要明確地去做由 TopLinkException 到Spring的 DataAccessException 的轉(zhuǎn)化。

事務(wù)管理
將事務(wù)管理納入到Service操作的執(zhí)行中,你可以使用Spring通用的聲明式的事務(wù)管理功能,參加下面的例子:

  1. xml version="1.0" encoding="UTF-8"?> 
  2.  
  3.         xmlns="http://www.springframework.org/schema/beans" 
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5.         xmlns:aop="http://www.springframework.org/schema/aop" 
  6.         xmlns:tx="http://www.springframework.org/schema/tx" 
  7.         xsi:schemaLocation="  
  8.    http://www.springframework.org/schema/beans                                  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.    http://www.springframework.org/schema/tx                                              http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
  10.    http://www.springframework.org/schema/aop                                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
  11.  id="myTxManager" class="org.springframework.orm.toplink.                                       TopLinkTransactionManager"> 
  12.      name="sessionFactory" ref="mySessionFactory"/> 
  13. bean> 
  14.  id="myProductService" class="product.ProductServiceImpl"> 
  15.      name="productDao" ref="myProductDao"/> 
  16. bean> 
  17.  
  18.      id="productServiceMethods" expression="execution                                          (* product.ProductService.*(..))"/> 
  19.      advice-ref="txAdvice" pointcut-ref="productServiceMethods"/> 
  20. aop:config> 
  21.  id="txAdvice" transaction-manager="myTxManager"> 
  22.      
  23.        name="increasePrice*" propagation="REQUIRED"/> 
  24.        name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/> 
  25.        name="*" propagation="SUPPORTS" read-only="true"/> 
  26.      tx:attributes> 
  27. tx:advice> 
  28. beans>  

注意,TopLink要求你必須在一個(gè)活躍的 工作單元(UnitOfWork) 中修改一個(gè)持久化對(duì)象(你通常不能修改由普通的TopLink的 Session 查詢返回的對(duì)象,因?yàn)檫@些對(duì)象通常是一些從二級(jí)緩存中讀出的只讀對(duì)象)。與Hibernate相比,在TopLink中并沒有一種類似脫離事務(wù)刷出(non-transactional flush)的概念?;谶@種原因,TopLink需要被建立在特定的環(huán)境中,尤其是它需要為JTA同步做明確的創(chuàng)建,由此來 自行檢測(cè)一個(gè)JTA事務(wù)以及暴露一個(gè)相應(yīng)的活躍的 Session 和 UnitOfWork。這一點(diǎn)對(duì)于本地事務(wù)不是必要的,由于它已經(jīng)被 Spring的 TopLinkTransactionManager 處理,但是對(duì)于 需要參與到JTA事務(wù)中的情況,是必須的(無論是由Spring的 JtaTransactionManager、EJB CMT或者普通的JTA所驅(qū)動(dòng)的事務(wù))。

在你的基于TopLink的DAO代碼中,你可以使用 Session.getActiveUnitOfWork() 方法來訪問當(dāng)前的 UnitOfWork 并通過它來執(zhí)行寫操作。這將只在一個(gè)活躍的事務(wù)中有效(在一個(gè)收到Spring管理的事務(wù)或者JTA事務(wù)中)。對(duì)于特殊的需求,你同樣可以獲取單獨(dú)的 UnitOfWork 實(shí)例,它將不參與到當(dāng)前的事務(wù)中去,不過這種情況非常少。

TopLinkTransactionManager 能夠?qū)⒁粋€(gè)TopLink事務(wù)暴露給 訪問相同的JDBC DataSource 的JDBC訪問代碼。 前提條件是,TopLink在底層是以JDBC方式工作的并且能夠暴露底層的JDBC Connection。這種情況下,用于暴露事務(wù)的 DataSource 必須被明確指定, 它是無法被自動(dòng)檢測(cè)到的。


網(wǎng)頁(yè)題目:Spring中的TopLinkServerSession
文章來源:http://www.5511xx.com/article/djspgej.html