title: What is wrong with this class for getting connection from database pool
body: <p>I wrote a class to get connections from a database pool (<strong>HikariCP</strong>). I searched for error and it means that connection is closed. But, I am not able to figure out the problem. I am certain I did something wrong in the code, may be not closing the connection properly or whatever because after some time I start to get errors as below:</p>
  
  <pre><code>11:27:58.843 [http-apr-8080-exec-10] WARN  c.z.h.u.ConcurrentBag -
  **Attempt to remove an object from the bag that does not exist: com.mysql.jdbc.JDBC4Connection@5f3145e5, created Oct 21, 11:27:12.734, last release 0ms ago, NOT_IN_USE 11:27:58.861 [http-apr-8080-exec-10] ERROR c.p.d.DAOConnectionFactory - Error while closing connection.** java.sql.SQLException: Connection is closed   at com.zaxxer.hikari.proxy.ClosedConnection$1.invoke(ClosedConnection.java:51) ~[HikariCP-2.4.1.jar:na]     at com.sun.proxy.$Proxy4.clearWarnings(Unknown Source) ~[na:na]     at com.zaxxer.hikari.proxy.ConnectionProxy.close(ConnectionProxy.java:197) ~[HikariCP-2.4.1.jar:na]     at com.presence.dao.DAOConnectionFactory.closeConnection(DAOConnectionFactory.java:73)
  **~[DAOConnectionFactory.class:na]   11:28:01.133 [http-apr-8080-exec-9] WARN  c.z.h.p.ConnectionProxy - HikariPool-0 - Connection com.mysql.jdbc.JDBC4Connection@46fb3f92, created Oct 21, 11:27:12.781, last release 28ms ago, IN_USE marked as broken because of SQLSTATE(08003), ErrorCode(0) com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.   at** sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_45]   at 
  </code></pre>
  
  <p>Below is the class:</p>
  
  <pre><code>public class DAOConnectionFactory {
  
      private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DAOConnectionFactory.class);
      private static final String databaseJNDI = "jdbc/presencedb";
      private static DataSource dataSource = null;
      private static Context context = null;
      private static Connection conn = null;
  
      private DAOConnectionFactory() {
          System.out.println("Initialized");
      }
  
      private static DataSource getDataSource() {
          if (dataSource != null) {
              return dataSource;
          }
          try {
              context = new InitialContext();
  
              if (context == null) {
                  context = new InitialContext();
              }
  
              dataSource = (DataSource) context.lookup("java:comp/env/" + databaseJNDI);
          } catch (NamingException e) {
              logger.error("Error while creating datasource.", e);
          }
          return dataSource;
      }
  
      protected static Connection getConnection() {
          try {
              conn = getDataSource().getConnection();
          } catch (SQLException e) {
              logger.error("Error while getting database connection.", e);
          }
          return conn;
      }
  
      protected static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
          if (resultSet != null) {
              try {
                  resultSet.close();
              } catch (SQLException e) {
                  logger.error("Error while closing resultset.", e);
              }
          }
          if (statement != null) {
              try {
                  statement.close();
              } catch (SQLException e) {
                  logger.error("Error while closing statement.", e);
              }
          }
          if (connection != null) {
              try {
                  connection.close();
              } catch (SQLException e) {
                  logger.error("Error while closing connection.", e);
              }
          }
      }
  }
  </code></pre>
  
  <p>hikaricp properties:</p>
  
  <pre><code>cachePrepStmts="true" 
  defaultAutoCommit="true" 
  description="Presence database." 
  driverClassName="com.mysql.jdbc.Driver" 
  factory="com.zaxxer.hikari.HikariJNDIFactory" 
  global="jdbc/presencedb" 
  jdbcUrl="jdbc:mysql://192.168.254.167/presence" 
  maxWaitMillis="10000" maximumPoolSize="30" 
  name="jdbc/presencedb" 
  password="" 
  prepStmtCacheSize="50" 
  prepStmtCacheSqlLimit="512" 
  removeAbandoned="true" 
  removeAbandonedTimeout="300" 
  type="javax.sql.DataSource" 
  idleTimeout="25200000"
  maxLifetime="25200000"
  username="root"
  </code></pre>
  
  <p>Add this is how I obtain the connection</p>
  
  <pre><code>Connection connection = null;
  PreparedStatement preparedStatement = null;
  connection = DAOConnectionFactory.getConnection();
  preparedStatement = connection.prepareStatement(SQL_UPDATE);
  
  DAOConnectionFactory.closeConnection(connection, preparedStatement, null);
  </code></pre>
  
  <p>This happens always when I perform load testing otherwise for few random queries I have not noticed this.
  <em>HikariCP version is 2.4.1.</em></p>