前段时间因为不小心删掉了这篇文章,经高人指点,决定再次粘上来。也由衷地感谢这位高人。企业项目中的数据库连接池都是已经写好,程序员直接拿来用就可以了。自己写一下算是了解一下底层的东西吧!写得不好的地方,希望大家多多指教!
public class DbConnectionManager {
private static DbConnectionManager dbConnectionManager= null;
private static int clientCount = 0;
private static Vector<Driver> drivers = new Vector<Driver>();//存放数据库驱动
//pools存放数据库连接池
private static Hashtable<String,DbConnectionPool> pools = new Hashtable<String,DbConnectionPool>();
private PrintWriter log = null;
private DbConnectionManager(){
init();
}
/**
* 〈得到DbConnectionManager实例<单例模式>〉
*/
public static synchronized DbConnectionManager getInstance(){
if(null == dbConnectionManager){
dbConnectionManager = new DbConnectionManager();
}
clientCount++;
return dbConnectionManager;
}
/**
* 〈获取连接〉
*/
public synchronized Connection getConnection(String name){
DbConnectionPool dbConnectionPool = pools.get(name);
Connection connection = null;
if(null != dbConnectionPool){
connection = dbConnectionPool.getConnection();
}
return connection;
}
/**
* 〈释放连接〉
*/
public void freeConnection(String name,Connection connection){
DbConnectionPool dbConnectionPool = pools.get(name);
if(null != dbConnectionPool){
dbConnectionPool.freeConnection(connection);
}
}
/**
* 〈加载数据库属性文件〉
*/
private void init(){
InputStream inputStream = this.getClass().getResourceAsStream("/db.properties");
Properties properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
System.err.println("Can't read the properties file. " + "Make sure db.properties is in the CLASSPATH");
return;
}
String logFile = properties.getProperty("logfile", "DBConnectionManager.log");
try {
log = new PrintWriter(new FileWriter(logFile, true), true);
} catch (IOException e) {
System.err.println("Can't open the log file: " + logFile);
log = new PrintWriter(System.err);
}
loadDrivers(properties);//加载驱动
createPool(properties);//创建数据库连接池
}
/**
* 〈创建连接池〉
*/
private void createPool(Properties properties) {
Enumeration propertyNames = properties.propertyNames();
String propertyName = null;
while(propertyNames.hasMoreElements()){
propertyName = (String) propertyNames.nextElement();
if(propertyName.endsWith("url")){
String poolName = propertyName.substring(0, propertyName.lastIndexOf("."));
String url = properties.getProperty(propertyName);
String userName = properties.getProperty(poolName+".userName");
String password = properties.getProperty(poolName+".password");
String maxConn = properties.getProperty(poolName+".maxConn");
int max = 0;
try{
max = Integer.parseInt(maxConn);
}catch(NumberFormatException e){
max = 0;
}
DbConnectionPool dbConnectionPool = new DbConnectionPool(
poolName,url,userName,password,max);
pools.put(poolName, dbConnectionPool);
}
}
}
/**
* 〈释放连接池〉
*/
public void freePools(){
if(0 == --clientCount){
Enumeration<DbConnectionPool> allPools = pools.elements();
DbConnectionPool dbConnectionPool = null;
while(allPools.hasMoreElements()){
dbConnectionPool = allPools.nextElement();
dbConnectionPool.freeConnections();
}
Enumeration<Driver> allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
} catch (SQLException e) {
}
}
}
}
/**
* 〈加载驱动〉
*/
private void loadDrivers(Properties properties){
String driverClass = properties.getProperty("drivers");
Driver driver = null;
try {
driver = (Driver) Class.forName(driverClass).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
drivers.addElement(driver);
}
private class DbConnectionPool{
private String poolName;
private String url;
private String userName;
private String password;
private int maxConn;
private final Vector<Connection> freeConnections = new Vector<Connection>();
private int checkedOut;
public DbConnectionPool(String poolName,String url,String userName,String password,int maxConn){
this.poolName = poolName;
this.url = url;
this.userName = userName;
this.password = password;
this.maxConn = maxConn;
}
/**
* 〈获取连接〉
*/
public Connection getConnection() {
Connection connection = null;
if(freeConnections.size()>0){
connection = freeConnections.firstElement();
freeConnections.remove(0);
try {
if (connection.isClosed()) {
connection = getConnection();
}
} catch (SQLException e) {
connection = getConnection();
}
}else if((0 == maxConn)|| checkedOut < maxConn){
connection = newConnection();
}
if(null != connection){
checkedOut++;
}
return connection;
}
/**
* 〈释放单个连接〉
*/
public void freeConnection(Connection connection) {
freeConnections.addElement(connection);
checkedOut--;
notifyAll();
}
/**
* 〈创建连接〉
*/
public Connection newConnection() {
Connection connection = null;
try {
if(null == userName){
DriverManager.getConnection(url);
}else {
connection = DriverManager.getConnection(url, userName, password);
}
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 〈释放所有连接〉
*/
public void freeConnections() {
Enumeration<Connection> connections = freeConnections.elements();
Connection connection = null;
while(connections.hasMoreElements()){
connection = connections.nextElement();
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
freeConnections.removeAllElements();
}
}
}
分享到:
相关推荐
数据库连接池,一个是XML版读取属性文件,一个是普通的
对于多应用共享同一数据库的系统而言,可在应用层通过数据库连接的配置,实现数据库连接池技术。某一应用最大可用数据库连接数的限制,避免某一应用独占所有数据库资源。 在较为完备的数据库连接池实现中,可根据...
C# 数据库连接池 C# 数据库连接池 C# 数据库连接池 C# 数据库连接池
* 数据库连接池特点: * 获取连接时不需要了解连接的名字,连接池内部维护连接的名字 * 支持多线程,保证获取到的连接一定是没有被其他线程正在使用 * 按需创建连接,可以创建多个连接,可以控制连接的数量 * 连接...
java手写数据库连接池,使用maven构建,一共有四个类方法,一个是测试类,测试类使用三个线程去操数据库连接
达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,
context.xml, 数据库连接池配置文
数据库连接池的例子.doc数据库连接池的例子.doc数据库连接池的例子.doc
C#高效数据库连接池源码
Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置...
提供了数据库连接池的驱动,分别有c3p0、druid、dbcp三种数据库连接池的驱动
最近写了一个数据库连接池,虽然比不上专业的连接池,但是比普通的jdbc高效,很适合初学者。源码有注释,还带有测试程序。
数据库连接池 数据库连接池 数据库连接池 数据库连接池
Druid为监控而生的数据库连接池,它是阿里巴巴开源平台上的一个项目。Druid是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能.它可以替换DBCP和C3P0连接池。Druid提供了一个高效、功能强大、可...
这个文档详细讲述了mysql数据库连接池的配置以及数据库连接池的工作原理。
收集了常见的数据库连接jar包,包括oracle、mysql、sql server、db2、opta、dbcp连接池、c3p0连接池等等常见的数据库jar包,不断更新中。
发现csdn上的连接池都是配设xml的,就手写了一份数据库连接池(java),连接sqlserver,里面一共两个java代码,Conn类包含了Connection和标志位,myconnection包含了数据库连接池的使用:获取连接,增加连接,释放...
c#语言下,使用netcore2.1框架,调用netstandard数据库连接池组件
数据库连接池案例包括c3p0连接池案例,druid连接池案例,和自己手写的连接池
比较实用的几种数据库连接池详细配置,值得收藏