我的连接池存在这个问题。我有这个问题大约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$3.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>
这是返回连接的DataSource类:
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方法,它没有正确同步,但我开始怀疑,因为。我试图通过较低的逐出时间和更高的时间来调整设置。但我没有想法。
答案 0 :(得分:0)
此问题的解决方案是使用本地数据库而不是我有权访问的公共数据库服务器。因此,如果遇到类似问题,请确保数据库服务器没有出现故障。