title: Java: Concurency with HikariDataSource Object
body: <p>I have a class which looks like that:</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  import com.zaxxer.hikari.HikariConfig;
  import com.zaxxer.hikari.HikariDataSource;
  
  public class ConnectionPool {
      private HikariDataSource hds;
      private final String propertyFileName;
  
      public ConnectionPool(String propertyFileName) {
          if (propertyFileName == null) {
              throw new IllegalArgumentException("propertyFileName can't be null");
          }
  
          this.propertyFileName = propertyFileName;
          reloadFile();
      }
  
      public void reloadFile() {
          if (hds != null) {
              hds.close();
          }
  
          hds = new HikariDataSource(new HikariConfig(propertyFileName));
      }
  
      public HikariDataSource getHikariDataSource() {
          return hds;
      }
  
      public String getPropertyFileName() {
          return propertyFileName;
      }
  
      public void executeQuery(final String sql, final CallBack&lt;ResultSet, SQLException&gt; callBack) {
          new Thread(new Runnable() {
              @Override
              public void run() {
                  Connection connection = null;
                  PreparedStatement preparedStatement = null;
                  ResultSet resultSet = null;
  
                  try {
                      connection = hds.getConnection();
                      preparedStatement = connection.prepareStatement(sql);
                      resultSet = preparedStatement.executeQuery();
                      callBack.call(resultSet, null);
                  } catch (SQLException e) {
                      callBack.call(null, e);
                  } finally {
                      if (resultSet != null) {
                          try {
                              resultSet.close();
                          } catch (SQLException ignored) {}
                      }
  
                      if (preparedStatement != null) {
                          try {
                              preparedStatement.close();
                          } catch (SQLException ignored) {}
                      }
  
                      if (connection != null) {
                          try {
                              connection.close();
                          } catch (SQLException ignored) {}
                      }
                  }
              }
          }).start();
      }
  
      public void executeUpdate(final String sql, final CallBack&lt;Integer, SQLException&gt; callBack) {
          //TODO
      }
  
      public void execute(final String sql, final CallBack&lt;Boolean, SQLException&gt; callBack) {
          //TODO
      }
  
      public void connection(final String sql, final CallBack&lt;Connection, SQLException&gt; callBack) {
          //TODO
      }
  }
  </code></pre>
  
  <p>The problem is that the <code>reloadFile()</code> method can be called from a different thread as <code>hds</code> is used. So it's possible that <code>hds</code> is closed while I use a connection object of it in another thread. What's the best way to solve this problem? Should I wait a few seconds after creating the new <code>HikariDataSource</code> object befor closing the old one (until the queries are finished)?</p>
  
  <p>Edit: Another question: Should <code>hds</code> be <code>volatile</code>, so that the changes of <code>hds</code> are visible for all threads?</p>