某天日志中连续都是如下的异常:

2017-09-06 21:39:52.936 ERROR [DruidDataSource.java:1974] create connection holder error

org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block

at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2476) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.core.v3.QueryExecutorImpl.proce***esults(QueryExecutorImpl.java:2189) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:301) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:287) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:264) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgConnection.execSQLQuery(PgConnection.java:416) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgConnection.execSQLQuery(PgConnection.java:410) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgConnection.getTransactionIsolation(PgConnection.java:793) ~[postgresql-42.1.1.jar:42.1.1]

at com.alibaba.druid.filter.FilterChainImpl.connection_getTransactionIsolation(FilterChainImpl.java:362) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.filter.FilterAdapter.connection_getTransactionIsolation(FilterAdapter.java:871) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.filter.FilterChainImpl.connection_getTransactionIsolation(FilterChainImpl.java:359) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.getTransactionIsolation(ConnectionProxyImpl.java:260) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidConnectionHolder.<init>(DruidConnectionHolder.java:145) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidConnectionHolder.<init>(DruidConnectionHolder.java:84) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidDataSource.put(DruidDataSource.java:1964) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2221) [druid-1.1.0.jar:1.1.0]

Caused by: org.postgresql.util.PSQLException: ERROR: canceling statement due to user request

at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2476) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.core.v3.QueryExecutorImpl.proce***esults(QueryExecutorImpl.java:2189) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:301) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:287) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:264) ~[postgresql-42.1.1.jar:42.1.1]

at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:231) ~[postgresql-42.1.1.jar:42.1.1]

at com.alibaba.druid.pool.vendor.PGValidConnectionChecker.isValidConnection(PGValidConnectionChecker.java:65) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidAbstractDataSource.validateConnection(DruidAbstractDataSource.java:1293) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1539) ~[druid-1.1.0.jar:1.1.0]

at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2178) [druid-1.1.0.jar:1.1.0]

最后查找资料,查看druid源代码,终于找到了原因,在数据库建立连接时,会检查连接状况:

(!= ) {
    result = ;
    Exception error = ;
    {
        result = .isValidConnection(conn, , );
    } (Exception ex) {
        error = ex;
    }
    
    (!result) {
        SQLException sqlError = error != ? SQLException(, error) : SQLException();
        sqlError;
    }
    ;
}

由于我们使用的是postgres数据库,在PGValidConnectionChecker代码中

int queryTimeout = validationQueryTimeout < 0 ? defaultQueryTimeout : validationQueryTimeout;

Statement stmt = ;
ResultSet rs = ;
{
    stmt = conn.createStatement();
    stmt.setQueryTimeout(queryTimeout);
    rs = stmt.executeQuery(validateQuery);
    ;

此处validationQueryTime默认值-1,defaultQueryTimeout则为1,这就造成执行sql语句需要在1秒内返回,偏偏那时候我们数据库正处于非常复杂的计算中,没有及时响应,就抛出了

如下异常Caused by: org.postgresql.util.PSQLException: ERROR: canceling statement due to user request,然后更有趣的事情发生了

这段代码是数据源建立连接的时候,测试连接是否通顺运行的,抛出异常后,

由于此处result默认为true,抛出异常也没有更改result为false,然后该连接会被正常使用,由于本身该连接使用过程中已经出现过异常,再次使用就会抛出

ERROR [DruidDataSource.java:1974] create connection holder error

org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block

由于druid的连接池未满,所以不停的创建连接,接连不断抛出如上异常