Search code examples
javapostgresqlconnection-poolingapache-commons-dbcp

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


Hello all,

I have this problem with our connection pooling. I have had this problem for about 2 weeks which keeps popping up, and the problem is that it is not a consistence problem. Sometimes it work sometimes it throws exception.

The exception stack trace is the following:

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$3.run(QueuedThreadPool.java:555)
        at java.base/java.lang.Thread.run(Thread.java:844)

I am using the following maven versions:

<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>

This is the DataSource class which returns the connection:

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

Could it be the settings which are used on ds or is it something else? In the beginning I thought it was the getConnection method which where not synchronized correctly, but I am starting to doubt that since. I have tried to tweak the settings both with a lower evict times and higher. But I am out of ideas.


Solution

  • The solution to this problem was to use a local database instead of a public database server which I was given access to. So make sure the database server is not faulted if you experience a similar problem.