2011-05-02 64 views
1

實施DB會話連接大家好 我們正在開發在Struts2,Spring的JPA的應用程序,在這個應用中,我們使用sessions.xml和persistence.xml中管理Oracle數據庫會話。使用JPA持續在春季

在Spring的serviceimpl類構造函數中,通過調用getSession()方法,我們正在初始化serverSession變量。

在這裏,我附上了我們正在使用該項目的Coeds。

在默認地將Impl構造

serverSession=getSession();

和方法執行程序

try { 
     createConnection(serverSession); 
     /* call stored procudure */ 
     StoredProcedureCall call = new StoredProcedureCall(); 
     call.setProcedureName("ORACLE_USP"); 
     call.addNamedArgumentValue("ARG1", value); 
     call.addNamedOutputArgument("OUTPUTVAL", "P_OUTPUTVAL", Integer.class); 

     ValueReadQuery query = new ValueReadQuery(); 
     query.setCall(call); 
     actionplanforum_id = (Integer) serverSession.executeQuery(query); 
    } catch (DatabaseException e) { 
     LOGGER.error("DatabaseException" + e); 
    } finally { 
     releaseJTSCon(serverSession); 
    } 


    protected ServerSession getSession() throws Exception { 
     ServerSession sSession = (ServerSession) SessionManager.getManager().getSession("dbsession"); 
     if (!sSession.isConnected()) { 
       sSession.connect(); 
      } 
     return sSession; 
} 

    public void createConnection(ServerSession sSession) { 
     if (!sSession.isLoggedIn()) { 
      sSession.login(); 
     } 
} 

    protected void releaseJTSCon(ServerSession sSession) { 
     try {  
      sSession.releaseJTSConnection(); 
     }catch (DatabaseException e) { 
      LOGGER.error("Error in releasing DB connection resources"); 
     } 

這是不使用EntityManger一個正確的方法,使用這種方法時,當更多的流量來了,我有oracle中有很多db連接(大部分處於空閒階段)。

任何人可以實施正確的方法幫助。

回答

2

您使用的是JPA還是本地API?

要使用JPA,你只需要做到這一點,

StoredProcedureCall call = ... 
Query query = em.unwrap(JpaEntityManager).createQuery(call); // or use getDelegate() if JPA 1.0. 
List result = query.getResultList(); 

如果您打算使用本機API那麼你的代碼不正確。除去connect()調用和releaseJTSConnection()調用,其他都是API方法,API在Session,UnitOfWork和Server接口中定義。刪除login(),因爲SessionManager的會話返回應該已經連接。如果希望查詢是事務性的,您應該從ServerSession獲取並釋放一個ClientSession來執行查詢,並可能是一個UnitOfWork。

+0

我有jsp頁面,它使用3個Ajax調用struts2操作那些返回的對象列表顯示在jsp頁面(表格格式)。在測試結束時,爲200名用戶(使用jmeter)進行負載測試時,我有大約80 db的連接。這是爲什麼呢bahaving所以..我已經嘗試過直接連接的ServerSession(一個我用分組)和使用的ClientSession(詹姆斯的建議),但兩者都沒有MAJAR diffrence.Is這是一個公認的行爲。爲什麼不是0連接? – gnanz 2011-05-03 10:48:48