M
M
MOWS2015-11-25 12:16:28
Java
MOWS, 2015-11-25 12:16:28

Is it possible to close Statement and Resultset in Connection Pool in java?

For connection pool I use HikariCP.
In theory, if I close the connection, then it closes all open Statements. Do I need to close Statement and Resultset from? It's just that everywhere they are advised to close them, but why pile up the code?

public ConnectionPool(String url, TypeDriver type) throws SQLException {
    HikariConfig hc = new HikariConfig();
    hc.setDriverClassName(Tools.getDriver(type));
    hc.setConnectionTestQuery("SELECT 1");
    hc.setMaximumPoolSize(20);
    hc.setJdbcUrl(url);
    dataSource = new HikariDataSource(hc);

    Connection connection = dataSource.getConnection();
    try {
      Statement statement = connection.createStatement();
      ResultSet resultSet = statement.executeQuery("insert ...");
    } finally {
      //TODO закрывать ли Statement и ResultSet??
      connection.close();
    }
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Azargan, 2015-11-25
@Azargan

Not much and clutter.

public ConnectionPool(String url, TypeDriver type) throws SQLException {
    HikariConfig hc = new HikariConfig();
    hc.setDriverClassName(Tools.getDriver(type));
    hc.setConnectionTestQuery("SELECT 1");
    hc.setMaximumPoolSize(20);
    hc.setJdbcUrl(url);
    dataSource = new HikariDataSource(hc);

    try (Connection connection = dataSource.getConnection();
          Statement statement = connection.createStatement();
          ResultSet resultSet = statement.executeQuery("insert ...")) {
      
    } catch (Exception e) {
        log.error(e);
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question