I'm using dbcp's BasicDataSource
in my JSF Java application. Since the basic convention is to close the connection after using it, I do so in catch - finally in my code. However, the application grinds to a halt with the error,
java.sql.SQLException: Connection is null.
at org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.checkOpen(DelegatingConnection.java:611)
at org.apache.tomcat.dbcp.dbcp2.DelegatingConnection.createStatement(DelegatingConnection.java:258)
So I decide to not close my connections; my code almost runs okay, but then often stops with this error:
Caused by: org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections
Below is my connection configuration:
public class StageDB {
public StageDB() {}
public static Connection getConnection() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(JDBC_DRIVER);
ds.setUsername(USER);
ds.setPassword(PASS);
ds.setUrl(DB_URL);
ds.setTimeBetweenEvictionRunsMillis(20*1000);
ds.setMinIdle(0);
ds.setMaxIdle(10);
ds.setMaxOpenPreparedStatements(100);
conn = ds.getConnection();
return conn;
}
}
I should mention I've tried playing around with these settings, and also using defaults, but with the same results. What could I be doing wrong?