I am using a PostGreSQL database to store data. (I use hikariCP btw) I have many methods that collect connections from a Hikari Datasource:
public Connection getConnection() {
try {
return dataSource.getConnection();
}
catch (SQLException ex)
{
return null;
}
}
And use them to carry out various functions. Recently, I have run into an error that happens when the database receives a lot of connections:
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: FATAL: sorry, too many clients already
Caused by: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
I have already tried to edit my configuration, search google, and run tests, but they didn't fix it. Could someone here help me fix it, or provide information as to how? Here is my configuration in my database class constructor:
PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setDatabaseName("BGWW Bot");
ds.setServerNames(new String[] {"localhost"});
ds.setUser(dbUser);
ds.setPassword(dbPass);
// Configure the HikariDatesource; add PGDatasource
config.setJdbcUrl("jdbc:postgresql://localhost/BGWW Bot");
config.setMinimumIdle(5);
config.setMaximumPoolSize(100);
config.setMaxLifetime(30000);
config.setIdleTimeout(30000);
config.setConnectionTimeout(60000);
config.setLeakDetectionThreshold(10000);
config.addDataSourceProperty("max_connections", "100");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.setDataSource(ds);
Here is a method that gets a connection:
public String[] getMember(String uuid, TextChannel channel)
{
try (Connection conn = getConnection())
{
String getUserSTR = "SELECT * FROM guild_members WHERE uuid = ?";
PreparedStatement stmt = conn.prepareStatement(getUserSTR);
stmt.setString(1, uuid);
ResultSet rs = stmt.executeQuery();
while(rs.next())
{
if(rs.getString(1).equals(uuid))
{
String[] array = new String[]{rs.getString(1), rs.getString(2), rs.getString(3)};
conn.close();
return array;
}
}
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
return null;
}
I also found something interesting, in which I opened and then closed new connections rapidly, 150 times, but they didn't produce any error.
What could be causing this error? How can I fix it? Thanks!