private final String driver; private final String url; private final String user; private final String password; private final int minConnection; private final int maxConnection; private final int minIdle; private final long maxWait;
private static final int INITIAL_SIZE = 5; private static final String CLOSE_METHOD = "close"; private static Logger logger; private int size; private ConnectionParam connectionParam; private ArrayBlockingQueue<Connection> idleConnectionQueue; private Vector<Connection> busyConnectionVector;
private static Map<String, ConnectionPool> poolMap = new ConcurrentHashMap<>();
public static Connection getConnection(String poolName) throws SQLException {
nameCheck(poolName);
ConnectionPool connectionPool = poolMap.get(poolName);
return connectionPool.getConnection();
}
public static void registerConnectionPool(String name, ConnectionParam connectionParam) {
registerCheck(name);
poolMap.put(name, new ConnectionPool(connectionParam));
}
// Let GC
public static void unRegisterConnectionPool(String name) {
nameCheck(name);
final ConnectionPool connectionPool = poolMap.get(name);
poolMap.remove(name);
new Thread(new Runnable() {
@Override
public void run() {
connectionPool.clear();
}
}).start();
}
@Override
public Connection getConnection() throws SQLException {
try {
final Connection connection = idleConnectionQueue.poll(connectionParam.getMaxWait(), TimeUnit.MILLISECONDS);
if (connection == null) {
logger.info(emptyMsg());
ensureCapacity();
return null;
}
busyConnectionVector.add(connection);
return (Connection) Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class[]{Connection.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!method.getName().equals(CLOSE_METHOD)) {
return method.invoke(connection, args);
} else {
idleConnectionQueue.offer(connection);
busyConnectionVector.remove(connection);
return null;
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
ConnectionParam connectionParam = new ConnectionParam.ConnectionParamBuilder(driver, url, user, password).build();
ConnectionPoolFactory.registerConnectionPool("test", connectionParam);
Connection connection = ConnectionPoolFactory.getConnection("test");
package database.config;
import java.io.Serializable;
/**
* DataBase Connection Parameters
* Created by Michael Wong on 2016/1/18.
*/
public class ParamConfiguration implements Serializable {
public static final int MIN_CONNECTION = 5;
public static final int MAX_CONNECTION = 50;
public static final int MIN_IDLE = 5;
public static final long MAX_WAIT = 30000;
private ParamConfiguration() {}
}
package database;
/**
* Builder
* Created by Michael Wong on 2016/1/18.
*/
public interface Builder<T> {
T build();
}
package database;
import database.config.ParamConfiguration;
/**
* DataBase Connection Parameters
* Created by Michael Wong on 2016/1/18.
*/
public class ConnectionParam {
private final String driver;
private final String url;
private final String user;
private final String password;
private final int minConnection;
private final int maxConnection;
private final int minIdle;
private final long maxWait;
private ConnectionParam(ConnectionParamBuilder builder) {
this.driver = builder.driver;
this.url = builder.url;
this.user = builder.user;
this.password = builder.password;
this.minConnection = builder.minConnection;
this.maxConnection = builder.maxConnection;
this.minIdle = builder.minIdle;
this.maxWait = builder.maxWait;
}
public String getDriver() {
return this.driver;
}
public String getUrl() {
return this.url;
}
public String getUser() {
return this.user;
}
public String getPassword() {
return this.password;
}
public int getMinConnection() {
return this.minConnection;
}
public int getMaxConnection() {
return this.maxConnection;
}
public int getMinIdle() {
return this.minIdle;
}
public long getMaxWait() {
return this.maxWait;
}
public static class ConnectionParamBuilder implements Builder<ConnectionParam> {
// Required parameters
private final String driver;
private final String url;
private final String user;
private final String password;
// Optional parameters - initialized to default value
private int minConnection = ParamConfiguration.MIN_CONNECTION;
private int maxConnection = ParamConfiguration.MAX_CONNECTION;
private int minIdle = ParamConfiguration.MIN_IDLE;
// Getting Connection wait time
private long maxWait = ParamConfiguration.MAX_WAIT;
public ConnectionParamBuilder(String driver, String url, String user, String password) {
this.driver = driver;
this.url = url;
this.user = user;
this.password = password;
}
public ConnectionParamBuilder minConnection(int minConnection) {
this.minConnection = minConnection;
return this;
}
public ConnectionParamBuilder maxConnection(int maxConnection) {
this.maxConnection = maxConnection;
return this;
}
public ConnectionParamBuilder minIdle(int minIdle) {
this.minIdle = minIdle;
return this;
}
public ConnectionParamBuilder maxWait(int maxWait) {
this.maxWait = maxWait;
return this;
}
@Override
public ConnectionParam build() {
return new ConnectionParam(this);
}
}
}
package database.factory;
import database.ConnectionParam;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Vector;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
* Connection Pool
* Created by Michael Wong on 2016/1/18.
*/
public class ConnectionPool implements DataSource {
private static final int INITIAL_SIZE = 5;
private static final String CLOSE_METHOD = "close";
private static Logger logger;
private int size;
private ConnectionParam connectionParam;
private ArrayBlockingQueue<Connection> idleConnectionQueue;
private Vector<Connection> busyConnectionVector;
protected ConnectionPool(ConnectionParam connectionParam) {
this.connectionParam = connectionParam;
int maxConnection = connectionParam.getMaxConnection();
idleConnectionQueue = new ArrayBlockingQueue<>(maxConnection);
busyConnectionVector = new Vector<>();
logger = Logger.getLogger(this.getClass().getName());
initConnection();
}
private void initConnection() {
int minConnection = connectionParam.getMinConnection();
int initialSize = INITIAL_SIZE < minConnection ? minConnection : INITIAL_SIZE;
try {
Class.forName(connectionParam.getDriver());
for (int i = 0; i < initialSize + connectionParam.getMinConnection(); i++) {
idleConnectionQueue.put(newConnection());
size++;
}
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
@Override
public Connection getConnection() throws SQLException {
try {
final Connection connection = idleConnectionQueue.poll(connectionParam.getMaxWait(), TimeUnit.MILLISECONDS);
if (connection == null) {
logger.info(emptyMsg());
ensureCapacity();
return null;
}
busyConnectionVector.add(connection);
return (Connection) Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class[]{Connection.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!method.getName().equals(CLOSE_METHOD)) {
return method.invoke(connection, args);
} else {
idleConnectionQueue.offer(connection);
busyConnectionVector.remove(connection);
return null;
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
private Connection newConnection() throws SQLException {
String url = connectionParam.getUrl();
String user = connectionParam.getUser();
String password = connectionParam.getPassword();
return DriverManager.getConnection(url, user, password);
}
protected int size() {
return size;
}
protected int idleConnectionQuantity() {
return idleConnectionQueue.size();
}
protected int busyConnectionQuantity() {
return busyConnectionVector.size();
}
private void ensureCapacity() throws SQLException {
int minIdle = connectionParam.getMinIdle();
int maxConnection = connectionParam.getMaxConnection();
int newCapacity = size + minIdle;
newCapacity = newCapacity > maxConnection ? maxConnection : newCapacity;
int growCount = 0;
if (size < newCapacity) {
try {
for (int i = 0; i < newCapacity - size; i++) {
idleConnectionQueue.put(newConnection());
growCount++;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
size = size + growCount;
}
protected void clear() {
try {
while (size-- > 0) {
Connection connection = idleConnectionQueue.take();
connection.close();
}
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}
}
private String emptyMsg() {
return "Database is busy, please wait...";
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
}
@Override
public int getLoginTimeout() throws SQLException {
return 0;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}
package database.factory;
import database.ConnectionParam;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Connection Pool Factory
* Created by Michael Wong on 2016/1/18.
*/
public class ConnectionPoolFactory {
private ConnectionPoolFactory() {}
private static Map<String, ConnectionPool> poolMap = new ConcurrentHashMap<>();
public static Connection getConnection(String poolName) throws SQLException {
nameCheck(poolName);
ConnectionPool connectionPool = poolMap.get(poolName);
return connectionPool.getConnection();
}
public static void registerConnectionPool(String name, ConnectionParam connectionParam) {
registerCheck(name);
poolMap.put(name, new ConnectionPool(connectionParam));
}
// Let GC
public static void unRegisterConnectionPool(String name) {
nameCheck(name);
final ConnectionPool connectionPool = poolMap.get(name);
poolMap.remove(name);
new Thread(new Runnable() {
@Override
public void run() {
connectionPool.clear();
}
}).start();
}
public static int size(String poolName) {
nameCheck(poolName);
return poolMap.get(poolName).size();
}
public static int getIdleConnectionQuantity(String poolName) {
nameCheck(poolName);
return poolMap.get(poolName).idleConnectionQuantity();
}
public static int getBusyConnectionQuantity(String poolName) {
nameCheck(poolName);
return poolMap.get(poolName).busyConnectionQuantity();
}
private static void registerCheck(String name) {
if (name == null) {
throw new IllegalArgumentException(nullName());
}
}
private static void nameCheck(String name) {
if (name == null) {
throw new IllegalArgumentException(nullName());
}
if (!poolMap.containsKey(name)) {
throw new IllegalArgumentException(notExists(name));
}
}
private static String nullName() {
return "Pool name must not be null";
}
private static String notExists(String name) {
return "Connection pool named " + name + " does not exists";
}
}
package database.factory;
import database.ConnectionParam;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
/**
* ConnectionPoolFactory Test
* Created by Michael Wong on 2016/1/20.
*/
public class ConnectionPoolFactoryTest {
@Test
public void testGetConnection() throws SQLException {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
ConnectionParam connectionParam = new ConnectionParam.ConnectionParamBuilder(driver, url, user, password).build();
ConnectionPoolFactory.registerConnectionPool("test", connectionParam);
List<Connection> connectionList = new ArrayList<>();
for(int i = 0; i < 12; i++) {
connectionList.add(ConnectionPoolFactory.getConnection("test"));
}
print();
close(connectionList);
print();
connectionList.clear();
for(int i = 0; i < 12; i++) {
connectionList.add(ConnectionPoolFactory.getConnection("test"));
}
print();
close(connectionList);
ConnectionPoolFactory.unRegisterConnectionPool("test");
}
@Test(expected = IllegalArgumentException.class)
public void testException() {
try {
ConnectionPoolFactory.getConnection("test");
} catch (SQLException e) {
e.printStackTrace();
}
}
private void close(List<Connection> connectionList) throws SQLException {
for(Connection conn : connectionList) {
if (conn != null) {
conn.close();
}
}
}
private void print() {
System.out.println("idle: " + ConnectionPoolFactory.getIdleConnectionQuantity("test"));
System.out.println("busy: " + ConnectionPoolFactory.getBusyConnectionQuantity("test"));
System.out.println("size: " + ConnectionPoolFactory.size("test"));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有