20 Eylül 2022 Salı

Hikari API HikariConfig Sınıfı

Giriş
Şu satırı dahil ederiz
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
HikariConfig  içinde tanımlı bazı alanlar şöyle

connectionTimeout : Uygulama Connection almak için kaç milisaniye bekler
Açıklaması şöyle
This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool. If this time is exceeded without a connection becoming available, a SQLException will be thrown. Lowest acceptable connection timeout is 250 ms. Default: 30000 (30 seconds)
idleTimeout : Kullanılmayan connection kapatılır. How long an unused connection lives in the pool
Açıklaması şöyle 
This property controls the maximum amount of time that a connection is allowed to sit idle in the pool. This setting only applies when minimumIdle is defined to be less than maximumPoolSize. Idle connections will not be retired once the pool reaches minimumIdle connections. Whether a connection is retired as idle or not is subject to a maximum variation of +30 seconds, and average variation of +15 seconds. A connection will never be retired as idle before this timeout. A value of 0 means that idle connections are never removed from the pool. The minimum allowed value is 10000ms (10 seconds). Default: 600000 (10 minutes)
initializationFailTimeout
Açıklama yaz

leak-detection-threshold
Açıklama yaz

maxLifetime : how long a connection will live in the pool before being closed
Açıklaması şöyle
sets the maximum lifetime for a connection in the pool, after this the connection is closed and is replaced with the new connection in the pool. This helps in preventing issues with the long-lived connections.

maximumPoolSize : Havuzun en fazla toplam Connection sayısı. Varsayılan değer 10. 
Açıklaması şöyle
This property controls the maximum size that the pool is allowed to reach, including both idle and in-use connections. Basically this value will determine the maximum number of actual connections to the database backend. A reasonable value for this is best determined by your execution environment. When the pool reaches this size, and no idle connections are available, calls to getConnection() will block for up to connectionTimeout milliseconds before timing out. Default: 10
minimumIdle - Idle Connection Sayısı
 Açıklaması şöyle 
This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool. If the idle connections dip below this value and total connections in the pool are less than maximumPoolSize, HikariCP will make a best effort to add additional connections quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not setting this value and instead allowing HikariCP to act as a fixed size connection pool. Default: same as maximumPoolSize
pool-name
Açıklama yaz

registerMBeans 
Açıklama yaz


Not : Eğer property ismi dataSource. ile başlıyorsa bu HikariConfig.addDataSourceProperty() çağrısı ile atanır. Yani isim ile belirtilen değer altta kullanılan DataSource nesnesi içindir


constructor - String
Örnek
Hikari sayfasındaki örnek şöyle
HikariConfig config = new HikariConfig("/some/path/hikari.properties");
HikariDataSource ds = new HikariDataSource(config);
properties dosyası şöyledir. Ancak buradaki farklı bir kullanım var. Tüm property isimleri dataSource. ile başlıyor
dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
dataSource.user=test
dataSource.password=test
dataSource.databaseName=mydb
dataSource.portNumber=5432
dataSource.serverName=localhost
constructor - Properties
Örnek
Şöyle yaparız. Burada Properties dosyadan yükleniyor
HikariDataSource ds;
try(InputStream input = Files.newInputStream(Paths.get("config/datasource.properties"))) {
  Properties prop = new Properties();
  prop.load(input);
  HikariConfig config = new HikariConfig(prop);
  ds = new HikariDataSource(config);

} catch (IOException e) {
  ...
}
properties dosyası şöyledir
jdbcUrl=jdbc:postgresql://localhost:5432/pg_dev
username=postgres
password=example
connectionTimeout=5000
maxLifetime=30000
maximumPoolSize=10
Örnek
Şöyle yaparız. Burada normalde boolean veya long olması gereken değerleri de String olarak geçebiliyoruz.
Properties props = new Properties();
props.setProperty("jdbcUrl", "jdbc:mysql://localhost:3306/mydatabase");
props.setProperty("username", "myuser");
props.setProperty("password", "mypassword");
props.setProperty("cachePrepStmts", "true");
props.setProperty("prepStmtCacheSize", "250");
props.setProperty("prepStmtCacheSqlLimit", "2048");

HikariConfig config = new HikariConfig(props);
setPoolName
Birden fazla Connection Pool varsa isim vermek iyi olabilir. Şöyle yaparız
Properties prop = new Properties();
prop.put("poolName","mypool");
setDataSource metodu
Mevcut DataSource nesnesini Hikari olarak sarmalar. Şöyle yaparız. Burada PGSimpleDataSource nesnesi PostgreSQL sürücüsüne ait bir DataSource
@Bean
public DataSource readWriteDataSource() {
  PGSimpleDataSource dataSource = new PGSimpleDataSource();
  dataSource.setURL(primaryUrl);
  dataSource.setUser(username);
  dataSource.setPassword(password);
  return connectionPoolDataSource(dataSource);
}

@Bean
public DataSource readOnlyDataSource() {
  PGSimpleDataSource dataSource = new PGSimpleDataSource();
  dataSource.setURL(replicaUrl);
  dataSource.setUser(username);
  dataSource.setPassword(password);
  return connectionPoolDataSource(dataSource);
}

protected HikariConfig hikariConfig(DataSource dataSource) {
  HikariConfig hikariConfig = new HikariConfig();
  int cpuCores = Runtime.getRuntime().availableProcessors();
  hikariConfig.setMaximumPoolSize(cpuCores * 4);
  hikariConfig.setDataSource(dataSource);

  hikariConfig.setAutoCommit(false);
  return hikariConfig;
}

protected HikariDataSource connectionPoolDataSource(DataSource dataSource) {
  HikariConfig hikariConfig = hikariConfig(dataSource);
  return new HikariDataSource(hikariConfig);
}
setHealthCheckInterval metodu
Hikari'nin health check işlemini hangi sıklıkta yapacağını belirtir
Örnek
Şöyle yaparız
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost/mydatabase");
config.setUsername("myusername");
config.setPassword("mypassword");
config.setMinimumIdle(2);
config.setMaximumPoolSize(5);
config.setConnectionTimeout(5_000);
config.setHealthCheckInterval(60_000); // Set the health check interval to 60 seconds
HikariDataSource dataSource = new HikariDataSource(config);
setMaximumPoolSize metodu
Örnek
Şöyle yaparız
HikariConfig config = new HikariConfig();
config.setJdbcUrl("...");
config.setMaximumPoolSize(10);
return new HikariDataSource(config);
setRegisterMbeans metodu
Açıklaması şöyle
In the JMX console, navigate to “MBeans” tab and select the “com.zaxxer.hikari”
Örnek
Şöyle yaparız
@Configuration
public class HikariCPConfig {
  @Bean
  public HikariDataSource dataSource(){
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl("jdbc:postgresql://<url>:<port>/<db_name>");
    config.setUsername("<username>");
    config.setPassword("<password>");

    config.setRegisterMbeans(true);
    return new HikariDataSource(config);
  }
}





Hiç yorum yok:

Yorum Gönder