首页 > 解决方案 > 在 Oracle UCP 池中重新建立连接

问题描述

我正在使用 Oracle UCP JDBC,以下方法用于从连接池获取连接。

private static PoolDataSource poolDataSource;

....

static synchronized Connection createConnection() throws SQLException {
    if (poolDataSource == null) {
        poolDataSource = PoolDataSourceFactory.getPoolDataSource();
        poolDataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
        poolDataSource.setURL(url);
        poolDataSource.setUser(user);
        poolDataSource.setPassword(password);
        poolDataSource.setInitialPoolSize(1);
        poolDataSource.setMinPoolSize(1);
        poolDataSource.setMaxPoolSize(10);
    }
    Connection connection = poolDataSource.getConnection();
    return connection;
}

我知道有 Connection.isValid() 方法来检查从池中获得的连接是否有效。但是如果 isValid() 返回 false,我该怎么办?如何强制连接池重新建立连接?

另外,请注意,在我们的测试环境中,我们没有使用 RAC(Real Application Clusters),但在生产环境中,我们确实有 RAC。我已经阅读了关于 RAC 的内容,我们需要在代码中进行一些配置才能使 RAC 工作。

是否可以为 RAC 和非 RAC 环境使用相同的代码,以便重新建立池中的无效连接?

提前致谢。

标签: oraclejdbcconnection-poolingucp

解决方案


如果您的数据库已启动并正在运行,则 isValid() 将返回 true,表示应用程序可以连接到数据库。但是,在生产系统中,会有很多节点,如果其中一个节点宕机,那么 UCP 将从其他节点获取连接。让我知道这是否可以澄清您的问题。


推荐阅读