Tomcat jdbc pool Setting
下列為JDBC連線方式及相關參數說明
需注意的是RemoveAbandoned這個系列的參數,跟資料庫的連線預時要設定正確
例如主機設定的是2分鐘棄置連線
mysql.cnf
----------------------------------
#Timeout
wait_timeout = 120 interactive_timeout = 120
----------------------------------
setRemoveAbandonedTimeout的參數不可以設定超過兩分鐘,否則程式認為Connection還可以用,但資料庫已經棄置該連線了
---------------Code------------------------
package org.c96.tool.db;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
public class Connection {
static private DataSource datasource;
static {
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://www.example.org:3306/etjhs?characterEncoding=utf8&useUnicode=true");
p.setDriverClassName("com.mysql.jdbc.Driver");
//帳號
p.setUsername("root");
//密碼
p.setPassword("root");
//最大連線數 1000
p.setMaxActive(1000);
//最大閒置連線數 100
p.setMaxIdle(100);
//最小閒置連線數量 10
p.setMinIdle(10);
//初始化連線數 10
p.setInitialSize(10);
//連線最大等待時間 30 s
p.setMaxWait(30000);
//檢測資源是否有效
// p.setTestOnBorrow(false);
// p.setTestOnConnect(false);
// p.setTestOnReturn(false);
// p.setTestWhileIdle(false);
//重新連線SQL
p.setValidationQuery("SELECT 1");
//重新驗證時間 30s
p.setValidationInterval(30000);
//驗證失敗重新驗證時間 10s
p.setValidationQueryTimeout(10);
//JMX
p.setJmxEnabled(true);
//驗證connection狀態,進行驗證及清除不必要的聯結 5s
p.setTimeBetweenEvictionRunsMillis(5000);
//Connection可閒置時間 30s
p.setMinEvictableIdleTimeMillis(30000);
//連線閒置時間超過removeAbandonedTimeout 則棄用
p.setRemoveAbandoned(true);
//設定閒置逾時時間60s
p.setRemoveAbandonedTimeout(60);
//記錄棄用資源
p.setLogAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
datasource = new DataSource();
datasource.setPoolProperties(p);
}
/**
* 取得Connection
*
* @return Connection
* @throws SQLException
*/
static public Connection getConn() throws SQLException, InterruptedException, ExecutionException {
Future<Connection> future = null;
try {
future = datasource.getConnectionAsync();
while (!future.isDone()) {
System.out.println("Connection is not yet available. Do some background work");
try {
Thread.sleep(100); //simulate work
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
}
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
return future.get();
}
}
需注意的是RemoveAbandoned這個系列的參數,跟資料庫的連線預時要設定正確
例如主機設定的是2分鐘棄置連線
mysql.cnf
----------------------------------
#Timeout
wait_timeout = 120 interactive_timeout = 120
----------------------------------
setRemoveAbandonedTimeout的參數不可以設定超過兩分鐘,否則程式認為Connection還可以用,但資料庫已經棄置該連線了
---------------Code------------------------
package org.c96.tool.db;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
public class Connection {
static private DataSource datasource;
static {
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://www.example.org:3306/etjhs?characterEncoding=utf8&useUnicode=true");
p.setDriverClassName("com.mysql.jdbc.Driver");
//帳號
p.setUsername("root");
//密碼
p.setPassword("root");
//最大連線數 1000
p.setMaxActive(1000);
//最大閒置連線數 100
p.setMaxIdle(100);
//最小閒置連線數量 10
p.setMinIdle(10);
//初始化連線數 10
p.setInitialSize(10);
//連線最大等待時間 30 s
p.setMaxWait(30000);
//檢測資源是否有效
// p.setTestOnBorrow(false);
// p.setTestOnConnect(false);
// p.setTestOnReturn(false);
// p.setTestWhileIdle(false);
//重新連線SQL
p.setValidationQuery("SELECT 1");
//重新驗證時間 30s
p.setValidationInterval(30000);
//驗證失敗重新驗證時間 10s
p.setValidationQueryTimeout(10);
//JMX
p.setJmxEnabled(true);
//驗證connection狀態,進行驗證及清除不必要的聯結 5s
p.setTimeBetweenEvictionRunsMillis(5000);
//Connection可閒置時間 30s
p.setMinEvictableIdleTimeMillis(30000);
//連線閒置時間超過removeAbandonedTimeout 則棄用
p.setRemoveAbandoned(true);
//設定閒置逾時時間60s
p.setRemoveAbandonedTimeout(60);
//記錄棄用資源
p.setLogAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
+ "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
datasource = new DataSource();
datasource.setPoolProperties(p);
}
/**
* 取得Connection
*
* @return Connection
* @throws SQLException
*/
static public Connection getConn() throws SQLException, InterruptedException, ExecutionException {
Future<Connection> future = null;
try {
future = datasource.getConnectionAsync();
while (!future.isDone()) {
System.out.println("Connection is not yet available. Do some background work");
try {
Thread.sleep(100); //simulate work
} catch (InterruptedException x) {
Thread.currentThread().interrupt();
}
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
return future.get();
}
}
留言
張貼留言