7 Temmuz 2020 Salı

DBCP BasicDataSource Sınıfı

Giriş
Tomcat JDBC Connection Pool kütüphanesinin Apache DBCP'den daha iyi olduğunu iddia ediyor.

constructor
Şöyle yaparız
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  <property name="url" value="jdbc:mysql://localhost:3306/chorechart" />  
  <property name="username" value="username" />  
  <property name="password" value="password*" />  
</bean>
Şöyle yaparız.
<bean id="misDataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
    <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
    <property name="url" value="xxx"/>
    <property name="username" value="xxx"/>
    <property name="password" value="xxx"/>
    <property name="maxActive" value="100"/>
    <property name="maxIdle" value="10"/>
    <property name="minIdle" value="1"/>
    <property name="maxWait" value="3000"/>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="false"/>
    <property name="testWhileIdle" value="true"/>
    <property name="timeBetweenEvictionRunsMillis" value="10000"/>
    <property name="numTestsPerEvictionRun" value="50"/>
    <property name="minEvictableIdleTimeMillis" value="10000"/>
    <property name="poolPreparedStatements" value="true"/>
</bean>
Şöyle yaparız
<bean id="XXX" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
  <property name="url" value="${oracle.url}"/>
  <property name="username" value="${oracle.user}"/>
  <property name="password" value="${oracle.password}"/>
  <property name="initialSize" value="10"/>
  <property name="minIdle" value="10"/>
  <property name="maxIdle" value="30"/>
  <property name="maxActive" value="50"/>
  <property name="testOnBorrow" value="true"/>
  <property name="testOnReturn" value="true"/>
  <property name="testWhileIdle" value="true"/>
  <property name="validationQuery" value="SELECT 1 FROM DUAL"/>
  <property name="connectionProperties" value="oracle.jdbc.ReadTimeout=10000"/>
  <property name="timeBetweenEvictionRunsMillis" value="-1"/>
</bean>
getConnection metodu
Şöyle yaparız.
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver")
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setUrl(connectURI);
...
Connection conn = ds.getConnection();
setInitialSize metodu
Örnek
Şöyle yaparız
public static DataSource getDataSource(){
  BasicDataSource ds = new BasicDataSource();
  ds.setUrl(JDBC_URL);
  ds.setUsername(JDBC_USER);
  ds.setPassword(JDBC_PASS);

  ds.setInitialSize(3);
  return ds;
}
setLogAbandoned metodu
Açıklaması şöyle
The logAbandoned property is also very important as it can log the complete stack-trace which might be leaking the connection, thus can be very useful to identify connection leak in application. Stack-Trace is logged in terminal itself. In Red Hat Fuse we can see these stack-traces logged by logAbandoned in karaf terminal and not in application log or fuse.log file. 
setMinIdle metodu
Açıklaması şöyle. Yani havuzdaki en az connection sayısı.
The minimum number of active connections that can remain idle in the pool, without extra ones being created, or 0 to create none.
Bir örneğin açıklaması şöyle. Başlangıç ve az az sayısı 10 verirsek, havuzda her zaman 10 tane bağlantı görmemiz gerekir. Bu bağlantı kullanılıyor veya bekliyor olabilir.
While in connection pool minIdle and InitialSize was set to 10. Hence it was expected that connection should remain 10 at any point
setMaxActive metodu
Açıklaması şöyle.
The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
setMaxIdle metodu
Açıklaması şöyle. Yani havuzdaki en fazla connection sayısı
The maximum number of connections that should be kept in the pool at all times. Default value is maxIdle:100. Idle connections are checked periodically (if enabled) and connections that have been idle for longer than minEvitableIdleTimeInMillis will be relased (also see testWhileIdle)
setMaxWait metodu
Açıklaması şöyle. Yani havuzdaki en fazla connection sayısı
The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception. Default value is 30000 (30 seconds)
setRemoveAbandoned  metodu
Açıklaması şöyle
When set to true to try to remove abandoned connections and return them to pool again in configured removeAbandonedTimeout in seconds. Setting this to true can recover database connections from poorly written applications that fail to close a connection. 
setTimeBetweenEvictionRunsMillis metodu
Açıklaması şöyle
The timeBetweenEvictionRunsMillis property can also be helpful, it is set in milliseconds. When set than a separate thread will run to remove idle object evictor thread in every configured millisecond. Its default value is -1 which means this idle object evictor thread wouldn't be active and running and only when set to a positive integer then it would be effective. Sometimes firewall in between or even database close idle connections permanently, having this timeBetweenEvictionRunsMillis will help those connections to return to pool.




Hiç yorum yok:

Yorum Gönder