Oracle JDBC UCP和Java

我想知道是否有人可以对这个话题有所了解,因为我已经绞尽脑汁好几天了,并且不太明白为什么这不起作用。 我有三个main类, RetrieveDBVersionGetOracleConnection我一直在用oracle JDBC,UCP和Java 1.7进行一些测试。 根据Oracle文档,如果我使用连接池,一旦关闭连接,连接将返回到池,使其无效并将其设置为null 请参阅此处 。 所以我决定给它一个旋转,看看它是否会像文档说的那样表现。 在我的主应用程序中,我有一个简单的循环,通过调用RetrieveDBVersion 200次连接。 RetrieveDBVersion只是执行查询并返回驱动程序版本。 我的循环工作正常,直到我达到68的幻数,然后我收到一个错误

 java.sql.SQLException: Exception occurred while getting connection: oracle.ucp.UniversalConnectionPoolException: Cannot get Connection from Datasource: java.sql.SQLException: Listener refused the connection with the following error: ORA-12516, TNS:listener could not find available handler with matching protocol stack 

这些是3种方法的细节。 这些方法不在服务器环境中。 他们只是调用本地oracle express数据库,我在桌面上运行它们。 为什么我会一直收到这个错误? 如果我将连接返回池中?

  1. 主要

     import com.jam.DB.JDBCVersion; import static java.lang.System.out; public class MainApp { public static void main(String[] args) { String myMainJDBCVar; try{ for(int i=1; i<200; i++ ) { myMainJDBCVar= JDBCVersion.RetrieveDBVersion(); out.println(myMainJDBCVar + " " + i); } out.println("this is Done!"); } catch (Exception e) { System.out.println(e.getMessage()); } } } 
  2. RetrieveDBVersion

      import java.sql.*; import oracle.ucp.jdbc.ValidConnection; public class JDBCVersion { public static String DBVersion; public static String RetrieveDBVersion()throws SQLException { Connection conn = JDBCConnection.GetOracleConnection("test"); try { DatabaseMetaData meta = conn.getMetaData(); //get driver info System.out.println("JDBC driver version is " + meta.getDriverMajorVersion()); DBVersion = meta.getDriverVersion(); } catch (SQLException e) { e.printStackTrace(); DBVersion = e.getMessage(); } finally { System.out.println("hit the finally clause"); ((ValidConnection) conn).setInvalid(); conn.close(); conn=null; } return DBVersion; } 
  3. GetOracleConnection

      import oracle.ucp.jdbc.PoolDataSource; import oracle.ucp.jdbc.PoolDataSourceFactory; import java.sql.*; public class JDBCConnection { public static Connection GetOracleConnection(String Enviroment) throws SQLException{ PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); Connection conn = null; //ora.defaultConnection(); try { pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); pds.setURL("jdbc:oracle:thin:@//localhost:1521/xe"); pds.setUser("system"); //pds.setInitialPoolSize(5); pds.setPassword("xxx"); pds.setMaxStatements(10); conn = pds.getConnection(); return conn; } catch(Exception e){ e.printStackTrace(); } return conn; } 

所以经过仔细考虑后,从Oracle论坛获得一些额外的帮助。 我终于理解为什么上面引用的代码给出了我收到的错误消息。 请参阅此处以获得响应因为每次循环时我都在设置数据源,所以我实际上创建了多个池。 执行此操作的方法是创建一个池,然后从该池中拉出连接。 替换GetOracleConnection新代码我为数据源创建了一个单例类,在代码中我只是从数据源中检索连接,就像这样

Connection conn = Database.getInstance().GetPoolSource().getConnection();

 package com.jam.DB; import oracle.ucp.jdbc.PoolDataSource; import oracle.ucp.jdbc.PoolDataSourceFactory; public class Database { private static Database dbIsntance; private static PoolDataSource pds; private Database() { // private constructor // } public static Database getInstance() { if (dbIsntance == null) { dbIsntance = new Database(); } return dbIsntance; } public PoolDataSource GetPoolSource() { if (pds == null) { pds = PoolDataSourceFactory.getPoolDataSource(); try { pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); pds.setURL("jdbc:oracle:thin:@//localhost:1521/xe"); pds.setUser("system"); pds.setPassword("xxxx"); pds.setMaxStatements(15); return pds; } catch (Exception e) { } return pds; } return pds; } }