如何从 oracle.ucp.jdbc.PoolDataSource 中获取 oracle.jdbc.OracleConnection

How to get a oracle.jdbc.OracleConnection from an oracle.ucp.jdbc.PoolDataSource

由于 ConnectionCaching 现在已在 ojdbc 中弃用,我想使用 Oracle Universal Connection Pool

这里的问题是我需要从中获取 oracle.jdbc.OracleConnection。我试过铸造和展开,但我不工作。我如何获得一个?

我需要 OracleConnection.createARRAY() 方法来调用存储过程。

从 api 文档中,如果您正在获取 UniversalPooledConnection 的实例,则可以从 getPhysicalConnection:

获取基础连接

getPhysicalConnection

java.lang.Object getPhysicalConnection()

Gets the physical connection that this UniversalPooledConnection decorates.

Returns: The physical connection. Never null.

感谢@6ton。我是这样做的:

/* missing all error handling and resource management*/
universalConnectionPoolManager = UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
poolDataSource = PoolDataSourceFactory.getPoolDataSource();
    // setting up dataSource .... 
poolDataSource.setConnectionPoolName(NAME);
universalConnectionPoolManager.createConnectionPool((UniversalConnectionPoolAdapter) poolDataSource);
universalConnectionPoolManager.startConnectionPool(NAME);
universalConnectionPool = universalConnectionPoolManager.getConnectionPool(NAME);
universalPooledConnection = universalConnectionPool.borrowConnection(universalConnectionPool.getConnectionRetrievalInfo());
physicalConnection = universalPooledConnection.getPhysicalConnection();
oracleConn = (OracleConnection) physicalConnection;
    // ... query stuff
universalPooledConnection.heartbeat();
universalConnectionPool.returnConnection(universalPooledConnection);
universalConnectionPoolManager.destroyConnectionPool(NAME);

您可以使用如下所示的unwrap 方法获取OracleConnection。

PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();    
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setURL("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=localhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservice)))");        
pds.setUser("hr");        
pds.setPassword("hr");
pds.setConnectionPoolName("JDBC_UCP_POOL");
pds.setInitialPoolSize(5);
pds.setMinPoolSize(5);
pds.setMaxPoolSize(10);
try (Connection conn = pds.getConnection()) {          
   OracleConnection oc = conn.unwrap(OracleConnection.class);          
} catch (SQLException e) {          
  System.out.println("UCPSample - SQLException occurred : " + e.getMessage());        
}