どのように私は適切に近いHikariCP接続プール

ロシャンUpreti:

私が使用しているHikariDataSourceに接続するためのMariaDBデータベース。以下のクラスが返されますConnection

public class DataSource {

private HikariDataSource ds;

// The constructor takes db name as an argument and creates a new datasource for the connection accordingly.
public DataSource(String dbString) {
    HikariConfig config = new HikariConfig();
    Map map = DbConfigParser.configKeyValue(dbString);
    config.setJdbcUrl(String.valueOf(map.get("uri")));
    config.setUsername(String.valueOf(map.get("uname")));
    config.setPassword(String.valueOf(map.get("pwd")));
    config.addDataSourceProperty("cachePrepStmts", "true");
    config.addDataSourceProperty("prepStmtCacheSize", "250");
    config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    ds = new HikariDataSource(config);
}

// Returns a Connection to the database
public Connection getConnection() throws SQLException {
    return ds.getConnection();
}

// Close the datasource
public void close(){
    if (ds != null) {
        ds.close();
    }
  }
}

これは、選択クエリを実行する方法です。クラスには、closeメソッドが含まれています

public List<DataFile> getAllFiles() throws SQLException {
try (Connection connection = dataSource.getConnection();
    DSLContext ctx = DSL.using(connection, SQLDialect.MARIADB)) {
  List<DataFile> dataFileList = new DataFileQueries().selectQuery(ctx)
      .fetchInto(DataFile.class);
  if (dataFileList == null || dataFileList.isEmpty()) {
    throw new IllegalStateException("The List is Empty!");
  }
  return dataFileList;
   }
}

public void close() {
try {
  dataSource.close();
} catch (Exception e) {
  LOG.error("A SQLException was caught", e);
 }
}

try-とブロックが閉じてConnection自動的にオブジェクトを、どのように私は、接続プールを閉じていますか?私は、例えば、データベース操作後のcloseメソッドの呼び出しを呼び出す必要があります

public static void main(String[] args) throws SQLException {
DataFileDaoImpl service = new DataFileDaoImpl("testi");
List<DataFile> list = service.getAllFiles();
list.stream().forEach(
    e -> System.out.println(e.toString())
);
service.close();
}

私は呼んでいない場合はclose()方法を、私はシャットダウン開始に関するいかなるコンソール出力が表示されません。これは近くに正しい方法であるHikariDataSourceと接続プール?

user7294900:

あなたは、データソースの呼び出す必要はありません近い()すべての接続のために:

シャットダウンデータソースとそれに関連するプール。

それだけのために定義されていますアプリケーションの終了

close() アプリケーションの終了時に不可欠です

あなたは、プール、あなたが閉じているの通知(正常)で作業を続けるべき接続リソースと試みとし

try (Connection connection = dataSource.getConnection()

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=138024&siteId=1