zoukankan      html  css  js  c++  java
  • Oracle UCP ValidateConnectionOnBorrow

    环境

    macOS Catalina 10.15.7
    docker desktop 3.5.1(66090)
    oracle 11g
    IntelliJ IDEA 2019.1.3
    JDK 1.8.0_202

    准备工作

    • 安装 docker
    • 下载 ucp.jar 和 ojdbc6.jar 两个 jar 包

    一、使用 Docker 安装 Oracle11g

    参考资料:
    Mac安装oracle(使用Docker)

    拉取镜像

    docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
    

    启动容器

    docker run -dp 9090:8080 -p 1521:1521 --name oracle_11g registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g
    

    使用 navicat 测试连通性

    服务名:helowin
    用户名:system
    密码:helowin

    二、配置 Oracle idle_time 用于测试

    参考资料:
    Oracle 概要文件IDLE_TIME限制用户最大空闲连接时间

    在 navicat 中连接 oracle11g 实例,并新建查询

    启用 RESOURCE_LIMIT 限制用户资源

      idle_time 等配置需要 resource_limit 的支持,所以需要先开启 resource_limit,否则配置了 idle_time 不生效。

    可使用如下 sql 查询当前 RESOURCE_LIMIT 状态,默认为 false:

    select name,value from gv$parameter where name='resource_limit';
    

    修改 RESOURCE_LIMIT 状态为 true:

    alter system set resource_limit=true;
    

    配置 idle_time

    通过 "alter profile <profile_name> limit idle_time

    alter profile default limit idle_time 1;
    

    可通过如下 sql 检查更改结果:

    select PROFILE,RESOURCE_NAME,LIMIT from dba_profiles where RESOURCE_NAME='IDLE_TIME';
    

    编码测试

    在 IDEA 中创建工程,工程引入 ucp.jar 和 ojdbc6.jar

    不配置 validateConnectionOnBorrow

    import oracle.ucp.jdbc.PoolDataSource;
    import oracle.ucp.jdbc.PoolDataSourceFactory;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class Test {
        public static void main(String[] args) throws SQLException, InterruptedException {
            // 1.创建pool类型的数据源
            PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource();
            // 2.配置数据源
            dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
            dataSource.setURL("jdbc:oracle:thin:system/helowin@//localhost:1521/helowin");
            dataSource.setUser("system");
            dataSource.setPassword("helowin");
            dataSource.setMaxPoolSize(1);
            //dataSource.setValidateConnectionOnBorrow(true);
            Connection connection = null;
            for (int i = 1;; i ++) {
                try {
                    // 3.通过数据源获取数据库连接(从连接池中获取)
                    connection = dataSource.getConnection();
                    PreparedStatement statement = connection.prepareStatement("select * from SYSTEM.HELP");
                    ResultSet result = statement.executeQuery();
                    if (result.next()) {
                        System.out.println(i + ": success");
                    }
                    // 4.关闭连接(归还到连接池)
                    connection.close();
                    Thread.sleep(1000 * 60 * 3);
                } catch (Exception e) {
                    connection.close();
                    System.out.println(i + " : " + e);
                    Thread.sleep(1000 * 60 * 3);
                }
            }
        }
    }
    

    运行结果:

    配置 validateConnectionOnBorrow 为 true

    把代码中对应的注释删除

    dataSource.setValidateConnectionOnBorrow(true);
    

    运行结果:

  • 相关阅读:
    MTG Hole
    串行SPI口Flash全部引脚
    名词解释:100BASEX
    锂离子电池开路电压与电池剩余电量的对应关系
    BT.656接口数据帧的结构
    Oracle SCN是什么
    oracle联机文档
    ORACLE SERVER 组成
    struts文件上传时异常问题
    C++ NULL的使用
  • 原文地址:https://www.cnblogs.com/life-of-coding/p/14999898.html
Copyright © 2011-2022 走看看