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

时间:2015-07-30 14:14:28

标签: java oracle jdbc ojdbc

由于现在在ojdbc中不推荐使用ConnectionCaching,我想使用Oracle Universal Connection Pool

这里的问题是我需要从中获取oracle.jdbc.OracleConnection。我尝试过铸造和展开,但我没有工作。我怎么得到一个?

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

3 个答案:

答案 0 :(得分:2)

从api文档中,如果您获得UniversalPooledConnection的实例,则可以从getPhysicalConnection获取基础连接:

  

getPhysicalConnection

     

java.lang.Object getPhysicalConnection()

     

获取物理连接   这个UniversalPooledConnection装饰。

     

返回:物理   连接。永远不会为空。

答案 1 :(得分:0)

谢谢@ 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);

答案 2 :(得分:0)

您可以使用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());        
}