oracle - 在 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 环境使用相同的代码,以便重新建立池中的无效连接?
提前致谢。
解决方案
如果您的数据库已启动并正在运行,则 isValid() 将返回 true,表示应用程序可以连接到数据库。但是,在生产系统中,会有很多节点,如果其中一个节点宕机,那么 UCP 将从其他节点获取连接。让我知道这是否可以澄清您的问题。
推荐阅读
- django - Django 弹性搜索:AttributeError:类型对象“PostDocument”没有属性“Django”
- c# - 以不同的顺序对集合中的对象执行几种方法
- php - 阻止直接访问文件的问题
- python - Python Django Queryset 仅从日期获取月份和年份
- python - 如何读取一些文件数据并将其写入另一个文件?
- jquery - JQuery删除限制在外部容器中的重复项
- elixir - 开始登录后用户不存在
- python - 如何清除 IDLE shell 中所有文本的屏幕?
- azure - 如何使用 ADF 表达式生成器提取两个字符之间的字符串?
- assembly - 在用户输入中查找第一个和最后一个大写字母