2011-05-02 45 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