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

    运行结果:

  • 相关阅读:
    [LeetCode] Power of Three 判断3的次方数
    [LeetCode] 322. Coin Change 硬币找零
    [LeetCode] 321. Create Maximum Number 创建最大数
    ITK 3.20.1 VS2010 Configuration 配置
    VTK 5.10.1 VS2010 Configuration 配置
    FLTK 1.3.3 MinGW 4.9.1 Configuration 配置
    FLTK 1.1.10 VS2010 Configuration 配置
    Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别
    [LeetCode] Bulb Switcher 灯泡开关
    [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
  • 原文地址:https://www.cnblogs.com/life-of-coding/p/14999898.html
Copyright © 2011-2022 走看看