title: Java Postgres connection limit exceeded
body: <p>I am using Java 1.7 and Postgres via the Postgres JDBC drivers. The database connection will be used from a Web Service. In testing, I got the following error:</p>
  
  <pre><code>FATAL: connection limit exceeded for non-superusers
  </code></pre>
  
  <p>I solved the error by making my connection static, and, only creating once. My question is, is a static connection safe? Is this the right way to do this?</p>
  
  <p>I am using the connection via a ConnectionFactory that looks something like this:</p>
  
  <pre><code>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;
     }
  }
  </code></pre>