gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:17:05 26 4
gpt4 key购买 nike

大家好,

我的连接池有这个问题。我已经遇到这个问题大约 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>

这是返回连接的数据源类:

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)数据库服务器。因此,如果遇到类似问题,请确保数据库服务器没有故障。

关于java - BasicDataSource 有时会抛出 "Protocol not supported, abandoning connection.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47629039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com