PostgresJDBCドライバーを介してJava1.7とPostgresを使用しています。データベース接続は、Webサービスから使用されます。テスト中に、次のエラーが発生しました。
FATAL: connection limit exceeded for non-superusers
接続を静的にし、1回だけ作成することで、エラーを解決しました。私の質問は、静的接続は安全ですか?これはこれを行う正しい方法ですか?
私は次のようなConnectionFactoryを介した接続を使用しています:
public class ConnectionFactory
{
String driverClassName = "org.postgresql.Driver";
String connectionUrl = "jdbc:postgresql://localhost:5432/dbName";
String dbUser = "user";
String dbPwd = "password";
private static ConnectionFactory connectionFactory = null;
private static Connection conn = null;
private ConnectionFactory()
{
try
{
Class.forName(driverClassName);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException
{
if (conn == null)
{
conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
}
return conn;
}
public static ConnectionFactory getInstance()
{
if (connectionFactory == null)
{
connectionFactory = new ConnectionFactory();
}
return connectionFactory;
}
}