📄 dbconnectionmanager.java
字号:
package com.laoer.bbscs.db;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
import org.apache.log4j.*;
import com.laoer.bbscs.sysinfo.*;
public class DBConnectionManager {
static private DBConnectionManager instance;
static private int clients;
static Logger logger = Logger.getLogger(DBConnectionManager.class.getName());
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++;
return instance;
}
public static synchronized void setNull() {
instance = null;
}
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);
log("Deregistered JDBCdriver " + driver.getClass().getName());
}
catch (SQLException e) {
log(e, "Can not deregistered JDBC driver: " + driver.getClass().getName());
}
}
}
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("."));
String url = props.getProperty(poolName + ".url");
if (url == null) {
log("No URL specified for" + poolName);
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) {
log("Invalid maxconn value: " + maxconn + " .for: " + poolName);
max = 0;
}
DBConnectionPool pool =
new DBConnectionPool(poolName, url, user, password, max);
pools.put(poolName, pool);
log("Initialized pool:" + poolName);
}
}
}
private void createPools() {
//Enumeration propNames = props.propertyNames();
for (int i = 0; i < Sys.SYSINFO.DATABASE.size(); i++) {
String[] databaseinfo = (String[]) Sys.SYSINFO.DATABASE.get(i);
String poolName = databaseinfo[0];
String url = databaseinfo[1];
if (url == null) {
log("No URL specified for" + poolName);
continue;
}
String user = databaseinfo[2];
String password = databaseinfo[3];
String maxconn = databaseinfo[4];
int max;
try {
max = Integer.valueOf(maxconn).intValue();
}
catch (NumberFormatException e) {
log("Invalid maxconn value: " + maxconn + " .for: " + poolName);
max = 0;
}
DBConnectionPool pool =
new DBConnectionPool(poolName, url, user, password, max);
pools.put(poolName, pool);
log("Initialized pool:" + poolName);
}
}
private void init() {
/*
InputStream is = getClass().getResourceAsStream("db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
System.err.println("Can not read the properties file; " +
"Make sure db.properties is in the Classpath");
return;
}
*/
/*
String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log");
try {
log = new PrintWriter(new FileWriter(logFile, true), true);
}
catch (IOException e) {
System.err.println("Can not open the log files: " + logFile);
log = new PrintWriter(System.err);
}*/
//loadDrivers(dbProps);
//createPools(dbProps);
loadDrivers();
createPools();
}
private void loadDrivers(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);
log("Registered JDBC driver:" + driverClassName);
}
catch (Exception e) {
log("Can not register JDBC driver " +
driverClassName + ",Exception:" + e);
}
}
}
private void loadDrivers() {
String driverClasses = Sys.SYSINFO.DBDRIVERS;
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);
log("Registered JDBC driver:" + driverClassName);
}
catch (Exception e) {
log("Can not register JDBC driver " +
driverClassName + ",Exception:" + e);
}
}
}
private void log(String msg) {
//log.println(new Date() + ": " + msg);
logger.info(new Date() + ": " + msg);
}
private void log(Throwable e, String msg) {
//log.println(new Date() + ": " + msg);
//e.printStackTrace(log);
logger.info(new Date() + ": " + msg, e);
}
class DBConnectionPool {
private int checkedOut;
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.user = user;
this.password = password;
this.maxConn = maxConn;
}
public synchronized void freeConnection(Connection con) {
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}
public synchronized Connection getConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
log("Removed bad connection from " + name);
con = getConnection();
}
}
catch (SQLException e) {
log("Removed bad connection from " + name);
con = getConnection();
}
}
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}
return con;
}
public synchronized Connection getConnection(long timeout) {
long startTime = new Date().getTime();
Connection con;
while ( (con = getConnection()) == null) {
try {
wait(timeout);
}
catch (InterruptedException e) {}
if ( (new Date().getTime() - startTime) >= timeout) {
// wait()���ص�ԭ���dz�ʱ
return null;
}
}
return con;
}
public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
log("Closed connection for pool " + name);
}
catch (SQLException e) {
log(e, "Can not close connection for pool " + name);
}
}
freeConnections.removeAllElements();
}
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
log("Created a new Connection in pool " + name);
}
catch (SQLException e) {
log(e, "Can not create a new Connection for " + URL);
return null;
}
return con;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -