在Spring中使用JPA持久性实现数据库会话连接

时间:2011-05-02 11:49:19

标签: oracle spring jpa persistence

大家好 我们正在使用Struts2-Spring-JPA开发一个应用程序。在这个应用程序中,我们使用sessions.xml和persistence.xml来管理oracle数据库会话。

在Spring serviceimpl类构造函数中,通过调用getSession()方法,我们正在初始化serverSession变量。

这里我附上了我们正在使用该项目的代码。

在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");  
        }  

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中打开了许多数据库连接(主要处于空闲阶段)。

任何人都可以帮助实施正确的方法。

1 个答案:

答案 0 :(得分: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。