BasicDataSource 有时会抛出 "Protocol not supported, abandoning connection."

BasicDataSource sometimes throws "Protocol not supported, abandoning connection."

大家好,

我的连接池有这个问题。我已经遇到这个问题大约 2 周了,它不断出现,问题是它不是一致性问题。有时它工作有时它抛出异常。

异常堆栈跟踪如下:

dec. 04, 2017 8:34:29 AM org.postgresql.core.v3.ConnectionFactoryImpl openConnectionImpl
SEVERE: Protocol not supported, abandoning connection.
dec. 04, 2017 8:34:29 AM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: A connection could not be made using the requested protocol null.
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:57)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:194)
        at org.postgresql.Driver.makeConnection(Driver.java:450)
        at org.postgresql.Driver.connect(Driver.java:252)
        at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
        at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:256)
        at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888)
        at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:432)
        at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)
        at org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:134)
        at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1533)
        at implementation.DataSource.getConnection(DataSource.java:54)
        at io.swagger.api.impl.ProjectApiServiceImpl.deleteProject(ProjectApiServiceImpl.java:58)
        at io.swagger.api.ProjectApi.deleteProject(ProjectApi.java:60)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        ......
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:555)
        at java.base/java.lang.Thread.run(Thread.java:844)

我正在使用以下 Maven 版本:

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-dbcp2</artifactId>
  <version>2.1.1</version>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-pool2</artifactId>
  <version>2.4.3</version>
</dependency>
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>42.1.4</version>
</dependency>

这是数据源 class,returns 连接:

package implementation;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
/**
 *
 * @author Lagoni
 */
public class DataSource {

    private static DataSource datasource;
    private BasicDataSource ds;
    private final Object lock = new Object();

    private DataSource() throws IOException, SQLException, PropertyVetoException {
        ds = new BasicDataSource();
        ds.setDriverClassName("org.postgresql.Driver");
        ds.setUsername("username");
        ds.setPassword("password");
        ds.setUrl("jdbc:postgresql://host:" + 1234 + "/database");

        ds.setMaxWaitMillis(1000 * 60); //wait max 1 min to get new connection
        ds.setMaxTotal(5);
        ds.setMaxIdle(5);
        ds.setTestWhileIdle(true);
        ds.setTestOnReturn(true);
        ds.setTimeBetweenEvictionRunsMillis(10000); // 10 sec wait to run evictor process
        ds.setSoftMinEvictableIdleTimeMillis(10000); // 10 sec wait to run evictor process
        ds.setMinEvictableIdleTimeMillis(10000); // 10 seconds to wait before idle connection is evicted
        ds.setMaxConnLifetimeMillis(1000*60*10); // 10 minutes is max life time
    }
    public static DataSource getInstance() {
        return datasource;
    }

    public static void cacheInstance() throws IOException, SQLException, PropertyVetoException{
        datasource = new DataSource();
    }

    /**
     * Weird exception is sometimes thrown.
     * @return
     * @throws SQLException 
     */
    public Connection getConnection() throws SQLException {
        synchronized(lock){
            return ds.getConnection();
        }
    }
}

可能是ds上使用的设置还是其他?一开始我以为是 getConnection 方法没有正确同步,但从那以后我开始怀疑了。我试图用较低的逐出时间和更高的时间来调整设置。但是我没主意了。

这个问题的解决方案是使用本地数据库,而不是我有权访问的 public 数据库服务器。因此,如果遇到类似问题,请确保数据库服务器没有故障。