最新在阅读公司自己写的数据连接池(比较简单的),参考其仿照写了一个数据库连接池,由于过于简单,取名SimpleDataSourceFactory,在tomcat上使用的时候需如下配置:
<GlobalNamingResources>
<Resource name="jdbc/ctpDataSource" auth="Container" factory="com.xx.yy.dbpool.SimpleDataSourceFactory" type="javax.sql.DataSource" />
</GlobalNamingResources>
完整代码如下:
package com.xx.yy.dbpool;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.spi.ObjectFactory;
import javax.sql.DataSource;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
public class SimpleDataSourceFactory implements ObjectFactory {
private static final Log log = LogFactory.getLog(SimpleDataSourceFactory.class);
/**
* 数据源名称
*/
private String name;
/**
* Jdbc地址
*/
private String url;
/**
* jdbc驱动
*/
private String driverClassName;
/**
* 数据库账号
*/
private String username;
/**
* 数据库密码
*/
private String password;
/**
* 最小连接数
*/
private int minCount = 0;
/**
* 最大连接数
*/
private int maxCount = 2000;
/**
* 尝试连接次数
*/
private int tryCount = 5;
/**
* 尝试连接等待时间
*/
private int tryWait = 100;
private static final String datasource = "xx.yy.datasource.properies.filepath";
private static final Logger logger = Logger.getLogger(SimpleDataSourceFactory.class.getCanonicalName());
/**
* 弱口令
*/
private Set<String> weakPassword = new HashSet<String>(){
private static final long serialVersionUID = 1L;
{
add("123456");
add("111111");
add("222222");
add("333333");
add("444444");
add("555555");
add("666666");
add("777777");
add("888888");
add("999999");
add("000000");
add("123123");
add("abc123");
}
};
private void initialize(Name name) {
this.name = name.get(0);
Properties p = getProperties(System.getProperty(datasource));
String property = p.getProperty(name + ".url");
if (property != null && property.length() > 0) {
url = property;
}
property = p.getProperty(name + ".driverClassName");
if (property != null && property.length() > 0) {
driverClassName = property;
}
property = p.getProperty(name + ".username");
if (property != null && property.length() > 0) {
username = property;
}
property = p.getProperty(name + ".password");
if (property != null && property.length() > 0) {
password = property ;
}
property = p.getProperty(name + ".minCount");
if (property != null && property.length() > 0) {
minCount = Integer.parseInt(property);
}
property = p.getProperty(name + ".maxCount");
if (property != null && property.length() > 0) {
maxCount = Integer.parseInt(property);
}
property = p.getProperty(name + ".tr