开发者社区 问答 正文

PG驱动参数中loginTimeout和socketTimeout传入了Integer类型导致失效或

Affected Version Druid 1.2.12

Description DruidAbstractDataSource#createPhysicalConnection 添加了 Integer类型的loginTimeout and socketTimeout

// com.alibaba.druid.pool.DruidAbstractDataSource#createPhysicalConnection()

// connectTimeout is primitive integer protected volatile int connectTimeout; public PhysicalConnectionInfo createPhysicalConnection() throws SQLException { ... if (this.connectTimeout > 0) { if (this.isMySql) { physicalConnectProperties.put("connectTimeout", this.connectTimeout); } else if (this.isOracle) { physicalConnectProperties.put("oracle.net.CONNECT_TIMEOUT", this.connectTimeout); } else if (this.driver != null && "org.postgresql.Driver".equals(this.driver.getClass().getName())) { // add loginTimeout & socketTimeout,, the value is integer instead of string physicalConnectProperties.put("loginTimeout", this.connectTimeout); physicalConnectProperties.put("socketTimeout", this.connectTimeout); } }

但PosgresSQL Driver仅过滤及读取String类型的KV参数, 理论上这两个值是不生效的

// org.postgresql.Driver#connect from pgdriver:42.4.1 Properties props = new Properties(defaults); if (info != null) { // notice the line below Set e = info.stringPropertyNames(); for (String propName : e) { String propValue = info.getProperty(propName); if (propValue == null) { throw new PSQLException( GT.tr("Properties for the driver contains a non-string value for the key ") + propName, PSQLState.UNEXPECTED_ERROR); } props.setProperty(propName, propValue); } }

同时旧版本的PG驱动上这两个值还会导致NPE, 因为老PG驱动上获取properties时未使用info.stringPropertyNames()而是获取了全部的propertyNames, 同时getProperty如果遇到非String类型返回NULL, 导致下面的props.setProperty(propName, info.getProperty(propName))抛NPE and it cause the NPE in the old version of PG:

   Properties props = new Properties(defaults);
    Enumeration e = info.propertyNames();

    while(e.hasMoreElements()) {
        String propName = (String)e.nextElement();
        // the line below cause NPE , getProperty traits non-string value as null
        props.setProperty(propName, info.getProperty(propName));
    }

新驱动不生效, 老驱动抛异常, 请帮忙看看

原提问者GitHub用户coney

展开
收起
山海行 2023-07-05 17:59:54 198 分享 版权
2 条回答
写回答
取消 提交回答
  • 北京阿里云ACE会长

    根据您提供的信息,可以看出 loginTimeout 和 socketTimeout 参数在 Druid 连接池中被设置为 Integer 类型,并且被添加到 PostgreSQL 数据库连接的 physicalConnectProperties 中。

    然而,根据 PostgreSQL JDBC 驱动的官方文档,loginTimeout 和 socketTimeout 参数都是字符串类型的参数,应该使用字符串类型的值进行设置。如果将 Integer 类型的值传递给这些参数,可能会导致这些参数无法生效。

    2023-07-30 21:10:15
    赞同 展开评论
  • 问题已修复,请用新版本

    https://github.com/alibaba/druid/releases/tag/1.2.14

    原回答者GitHub用户wenshao

    2023-07-06 10:38:52
    赞同 展开评论