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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
根据您提供的信息,可以看出 loginTimeout 和 socketTimeout 参数在 Druid 连接池中被设置为 Integer 类型,并且被添加到 PostgreSQL 数据库连接的 physicalConnectProperties 中。
然而,根据 PostgreSQL JDBC 驱动的官方文档,loginTimeout 和 socketTimeout 参数都是字符串类型的参数,应该使用字符串类型的值进行设置。如果将 Integer 类型的值传递给这些参数,可能会导致这些参数无法生效。
问题已修复,请用新版本
https://github.com/alibaba/druid/releases/tag/1.2.14
原回答者GitHub用户wenshao