📄 dbconnectionmanager.java
字号:
/*建立DBConnectionManager
2002-10-15
ver:1.0
*/
package com.jxyd.sql;
import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
public class DBConnectionManager {
static private DBConnectionManager instance;
static private int clients;
private Vector drivers = new Vector();
// private PrintWriter log;
private Hashtable pools = new Hashtable();
// 返回唯一的实例
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
// System.out.println("客户端连接个数:"+clients +"时间 "+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
return instance;
}
// 构造函数
private DBConnectionManager() {
init();
}
// 释放一个连接
public void freeConnection(String name, Connection con) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
pool.freeConnection(con);
}
// 取得一个连接
public Connection getConnection(String name) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
return pool.getConnection();
return null;
}
public Connection getConnection(String name, long time) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null)
return pool.getConnection(time);
return null;
}
// 关闭所有连接
public synchronized void release() {
// if(--clients!=0)return ;
Enumeration allPools = pools.elements();
while (allPools.hasMoreElements()) {
DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
System.out.println("成功注销JDBC驱动程序:"
+ driver.getClass().getName()+"22");
} catch (SQLException e) {
System.out.println("无法撤消JDBC驱动程序的注册"
+ driver.getClass().getName());
}
}
System.out.println("数据库连接池重新初始化完毕!");
clients = 0;
instance = null;
}
private void createPools(Properties props) {
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String name = (String) propNames.nextElement();
if (name.endsWith(".url")) {
String poolName = name.substring(0, name.lastIndexOf("."));
System.out.println(poolName);
String url = props.getProperty(poolName + ".url");
if (url == null) {
System.out.println("没有连接池" + poolName + "指定的url");
continue;
}
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxconn", "0");
int max;
try {
max = Integer.valueOf(maxconn).intValue();
} catch (NumberFormatException e) {
System.out
.println("错误的最大连接数" + maxconn + ".连接池" + poolName);
max = 0;
}
DBConnectionPool pool = new DBConnectionPool(poolName, url,
user, password, max);
pools.put(poolName, pool);
System.out.println("成功创建连接池" + poolName);
}
}
}
private void init() {
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
} catch (Exception e) {
System.out.println("不能读取属性文件,请确保属性文件在classpath路径中");
return;
}
loadDriver(dbProps);
createPools(dbProps);
}
private void loadDriver(Properties props) {
String driverClasses = props.getProperty("drivers");
StringTokenizer st = new StringTokenizer(driverClasses);
while (st.hasMoreElements()) {
String driverClassName = st.nextToken().trim();
try {
Driver driver = (Driver) Class.forName(driverClassName)
.newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
System.out.println("成功注册驱动程序" + driverClassName);
} catch (Exception e) {
System.out.println("无法注册驱动程序:" + driverClassName + "错误:" + e);
}
}
}
class DBConnectionPool {
private int checkOut;
private Vector freeConnections = new Vector();
private int maxconn;
private String name;
private String password;
private String URL;
private String user;
public DBConnectionPool(String name, String URL, String user,
String password, int maxconn) {
this.name = name;
this.URL = URL;
this.password = password;
this.user = user;
this.maxconn = maxconn;
}
public synchronized void freeConnection(Connection con) {
freeConnections.addElement(con);
checkOut--;
notifyAll();
// System.out.println(" 释放 1 条连接 最大连接数"+maxconn+"
// 目前池中有"+freeConnections.size()+" 条可用连接" );
}
public synchronized Connection getConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
con = getConnection();
}
} catch (SQLException e) {
con = getConnection();
}
} else if (maxconn == 0 || checkOut < maxconn) {
con = newConnection();
}
if (con != null)
checkOut++;
return con;
}
public synchronized Connection getConnection(long timeOut) {
long startTime = new java.util.Date().getTime();
Connection con;
while ((con = getConnection()) == null) {
try {
wait(timeOut);
} catch (InterruptedException e) {
}
if ((new java.util.Date().getTime() - startTime) >= timeOut)
return null;
}
return con;
}
public void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
} catch (SQLException e) {
System.out.println("无法关闭连接池" + name + "中的连接");
}
}
freeConnections.removeAllElements();
}
private Connection newConnection() {
Connection con = null;
try {
con = DriverManager.getConnection(URL, user, password);
} catch (SQLException e) {
System.out.println("无法创建一个URL的连接" + URL);
return null;
}
return con;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -