Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
coney opened this issue Sep 24, 2022 · 1 comment
Milestone

Comments

@coney
Copy link

coney commented Sep 24, 2022

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

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

@wenshao
Copy link
Member

wenshao commented Oct 6, 2022

https://github.com/alibaba/druid/releases/tag/1.2.13
问题已修复,请用新版本

@wenshao wenshao closed this as completed Oct 6, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants