28 Mayıs 2018 Pazartesi

Tomcat JDBC Connection Pool

Giriş
Açıklaması şöyle. Tomcat'in DataSource arayüzünü kullanan ve yine ismi DataSource olan (bence talihsiz bir seçim) sınıfıdır.
The JDBC Connection Pool org.apache.tomcat.jdbc.pool is a replacement or an alternative to the Apache Commons DBCP connection pool.
Apache DBCP yazısına göz atabilirsiniz.

Önemli Ayarlar
Bazıları şöyle
initialSize
Açıklaması şöyle
It is the number of connections created by the Connection Pool Manager at the start. If it is not defined, the Connection Pool Manager will create 10 threads by default.
maxActive
Havuzun büyüklüğünü belirtir. Şeklen şöyle

maxWait
Havuzdan yeni bir connection almak için beklenmesi istenilen süreyi belirtir. Bu süre içinde connection alınamazsa exception fırlatılır

maxIdle
Connection havuza geri verildiğinde kullanılır. Açıklaması şöyle
The default value of maxIdle is the value assigned to maxActive. If maxActive is not set, we set the same default value of 100 to both these properties.
Şeklen şöyle


timeBetweenEvictionRunsMillis
Havuzun ne kadar sıklıkta temizleneceğini belirtir. Şeklen şöyle

removeAbandoned 
removeAbandonedTimeout
minEvictableIdleTimeMillis
değerleri havuz temizlenirken kullanılır

Maven
Tomcat ile iki tane connection pool kütüphanesi geliyor.
Birincisi tomcat-dbcp,
İkincisi ise tomcat-jdbc.

Her iki kütüphane de tomcat'in lib dizininde mecvut.

Bu yazıda biz jdbc connection pool'u kullanıyoruz. Birincisini için maven'da şu satırı dahil ederiz.
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-dbcp</artifactId>
  <version>8.5.4</version>
</dependency>
İkinci için maven'da şu satırı dahil ederiz.
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-jdbc</artifactId>
  <version>10.0.0-M5</version>
</dependency>
Örnek
Şöyle yaparız.
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;


private static final String USERNAME = "root";
private static final String PASSWORD = "";
private static final String DATABASE_NAME = "store_db";

PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/" + DATABASE_NAME);
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername(USERNAME);
p.setPassword(PASSWORD);

DataSource ds = new DataSource();
ds.setPoolProperties (p);
Örnek
Şöyle yaparız.
<bean id="basicDataSourceWrite" class="org.apache.tomcat.jdbc.pool.DataSource"
  destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="${mysqlEndpointWrite}" />
  <property name="username" value="${mysqlUserWrite}" />
  <property name="password" value="${mysqlPasswordWrite}" />
  <property name="defaultAutoCommit" value="true" />
  <property name="initialSize" value="4" />
  <property name="maxActive" value="5" />
</bean>
Örnek
Şöyle yaparız. Sınıfın aslında milyon tane ayarı var :)
PoolProperties pool = new PoolProperties();
pool.setUrl(dbUrl);   // jdbc:sqlserver://hostname:port;databaseName=*** 
pool.setDriverClassName(provider); //com.microsoft.sqlserver.jdbc.SQLServerDriver
pool.setUsername(userName);
pool.setPassword(password);
pool.setJmxEnabled(true);
pool.setTestWhileIdle(false);
pool.setTestOnBorrow(true);
pool.setValidationQuery("SELECT 1");
pool.setTestOnReturn(false);
pool.setValidationInterval(validationInterval);  // 30000 MS 
pool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 30000 MS 
pool.setMaxActive(maxpoolSize); // 100
pool.setInitialSize(initialSize);  //10
pool.setMaxWait(maxWait); // 15000 MS
pool.setRemoveAbandonedTimeout(removeAbandonedTimeout);
pool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  // 60
pool.setMaxIdle(maxIdle);  // 50
pool.setMinIdle(minIdle);  // 10 
pool.setLogAbandoned(true);
pool.setRemoveAbandoned(true);
pool.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" +
  "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");

dataSource = new DataSource();
dataSource.setPoolProperties(pool);

Hiç yorum yok:

Yorum Gönder