📄 dbconnpool.java
字号:
package Int.who.database;
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is "The Database Driven Frequently Asked Questions Project of Alex Muc".
*
* The Initial Developer of the Original Code is <a href="mailto:alex.muc@utoronto.ca">Alex Muc</a>.
* Portions created by <a href="mailto:alex.muc@utoronto.ca">Alex Muc</a>
* are Copyright (C) 2000 <a href="mailto:alex.muc@utoronto.ca">Alex Muc</a>.
* All Rights Reserved.
*
* Contributor(s): Alex Muc, Claudia Plesa
*/
import java.sql.*;
import java.util.*;
/**
* A class which implements a general database connection pool for handling
* multiple concurrent database connections. This class is sufficiently general
* to be able to handle any number of connections to any number of databases.
* <br><br>
* Warning: It is imperative that users of this class call close(ResultSet,
* ConnectionInfo) when they are done with their RresultSet objects. This
* is necessary for cleaning up the database connection.
* <br><br>
* This class can handle closing connections to the database to clean
* itself up. However, limitations with the FreeTDS (www.freetds.org) driver
* will mean that it doesn't properly close connections (especially if there has
* been a network error. This will confuse things. The best possible solution
* is with a single call to DROPALL() which will drop all references to
* database connections and re-establish all links.
* <br><br>
* This class should work quite well, but there is still room for improvement.
* Particularly in the close connection methods.
* Creation date: (8/31/2000 1:51:39 PM)
* @author: Alex Muc alex.muc@utoronto.ca
*/
public class DBConnPool {
public static Vector ResultSets = new Vector();
public static Vector Connections = new Vector();
public static Vector Statements = new Vector();
public static Vector Queries = new Vector();
public static Vector ConnInfos = new Vector();
/* Stores the number of updates to the database since the last server restart */
public static int updates;
/* Stores the number of queries on the database since the last server restart */
public static int queries;
/**
* DBConnPool constructor comment.
*/
public DBConnPool() {
super();
}
/**
* Close all database connections.
* This method is not synchronized, so it will kill even active connections.
* It attempts to play nice and drop it's connection with the database server.
* These attempts may not work in cases with the FreeTDS driver and network errors.
* Creation date: (7/18/2000 12:05:54 PM)
*/
public static void close() throws SQLException {
System.out.println("in close");
Iterator conns = Connections.iterator();
Connection c;
while (conns.hasNext()) {
c = (Connection) conns.next();
c.close();
}
conns = Connections.iterator();
int i = 0;
Vector indices = new Vector();
while (conns.hasNext()) {
c = (Connection) conns.next();
if (c.isClosed()) {
indices.addElement(new Integer(i));
}
i ++;
}
Iterator is = indices.iterator();
int subtract = 0;
while (is.hasNext()) {
i = ((Integer) is.next()).intValue();
Queries.removeElementAt(i - subtract);
ResultSets.removeElementAt(i - subtract);
Statements.removeElementAt(i - subtract);
Connections.removeElementAt(i - subtract);
ConnInfos.removeElementAt(i - subtract);
subtract ++;
}
}
/**
* Close all database connections for a give ConnectionInfo object.
* This method is not synchronized, so it will kill even active connections.
* Creation date: (7/18/2000 12:05:54 PM)
*/
public static void close(ConnectionInfo ci) throws SQLException {
System.out.println("in close");
Iterator conns = Connections.iterator();
Iterator cis = ConnInfos.iterator();
Connection c;
ConnectionInfo cie;
while (conns.hasNext()) {
cie = (ConnectionInfo) cis.next();
c = (Connection) conns.next();
if (cie.equals(ci)) {
c.close();
}
}
conns = Connections.iterator();
int i = 0;
Vector indices = new Vector();
while (conns.hasNext()) {
c = (Connection) conns.next();
if (c.isClosed()) {
indices.addElement(new Integer(i));
}
i ++;
}
Iterator is = indices.iterator();
int subtract = 0;
while (is.hasNext()) {
i = ((Integer) is.next()).intValue();
Queries.removeElementAt(i - subtract);
ResultSets.removeElementAt(i - subtract);
Statements.removeElementAt(i - subtract);
Connections.removeElementAt(i - subtract);
ConnInfos.removeElementAt(i - subtract);
subtract ++;
}
}
/**
* Close a given result set. This does not close the database connection, but
* indicates the connection can be re-used.
* Creation date: (7/18/2000 11:42:29 AM)
* @param rs java.sql.ResultSet
*/
public synchronized static void close(ResultSet rs, ConnectionInfo ci) throws SQLException {
Iterator rss = ResultSets.iterator();
Iterator stmts = Statements.iterator();
java.sql.ResultSet curr;
int i = 0;
while (rss.hasNext()) {
Object o = rss.next();
if (o instanceof java.sql.ResultSet) {
curr = (java.sql.ResultSet) o;
if (curr.equals(rs)) {
break;
}
}
i ++;
}
rs.close();
ResultSets.setElementAt(null, i);
Queries.setElementAt("", i);
}
/**
* Close a given Statement. This does not close the database connection, but
* indicates the connection can be re-used.
* Creation date: (7/18/2000 11:42:29 AM)
* @param rs java.sql.ResultSet
*/
private synchronized static void close(java.sql.Statement s) throws SQLException {
Iterator rss = ResultSets.iterator();
Iterator stmts = Statements.iterator();
java.sql.Statement curr;
int i = 0;
while (stmts.hasNext()) {
curr = (java.sql.Statement) stmts.next();
if (curr.equals(s)) {
break;
}
i ++;
}
// ResultSet rs = (ResultSet) ResultSets.elementAt(i);
// rs.close();
ResultSets.setElementAt(null, i);
Queries.setElementAt("", i);
}
/**
* Drops all references to a given statement. This is a dirty way to drop
* a database connection.
* Creation date: (7/18/2000 11:42:29 AM)
* @param rs java.sql.ResultSet
*/
private synchronized static void drop(java.sql.Statement s) throws SQLException {
Iterator rss = ResultSets.iterator();
Iterator stmts = Statements.iterator();
java.sql.Statement curr;
int i = 0;
while (stmts.hasNext()) {
curr = (java.sql.Statement) stmts.next();
if (curr.equals(s)) {
break;
}
i ++;
}
// ResultSet rs = (ResultSet) ResultSets.elementAt(i);
// rs.close();
Connections.removeElementAt(i);
Statements.removeElementAt(i);
ResultSets.removeElementAt(i);
Queries.removeElementAt(i);
ConnInfos.removeElementAt(i);
}
/**
* Completely drops all references to all database connections. This does not
* try to clean them up, but only drop all references to them.
*
* Note: For the FreeTDS driver (www.freetds.org) calling this method may still
* not be enough for re-initializing the database connections due to its poor
* handling of broken database connections. In this case a VM restart is the only
* other option.
* Creation date: (8/31/2000 5:16:27 PM)
*/
public void DROPALL() {
ResultSets = new Vector();
Connections = new Vector();
Statements = new Vector();
Queries = new Vector();
ConnInfos = new Vector();
}
/**
* Because the database system treats single-quotes, "'", specially they must be escaped by
* replacing a single single-quote with two single-quotes (ie. "'" --> "''"). This method
* handles the escaping of strings.
* Creation date: (1/26/00 4:35:15 PM)
* @return java.lang.String The single-quote escaped string.
* @param query java.lang.String The string to be escaped.
*/
public static String escape(String query) {
if (query == null)
return null;
StringBuffer result = new StringBuffer();
if (query.indexOf('\'') != -1) {
for (int i = 0; i < query.length(); i ++){
if (query.charAt(i) == '\''){
result.append("''");
}
else {
result.append(query.charAt(i));
}
}
return result.toString();
}
return query;
}
/**
* Centralized method from which all queries to the database are run. This ensures that the
* database connection is still active. It also handles some concurrency issues. For
* update queries call executeUpdate(...).
* Creation date: (1/23/00 5:57:12 PM)
* @return ResultSet The result of the query from the JDBC call.
* @param query java.lang.String The query string to execute, can ONLY be query.
*/
public synchronized static ResultSet executeQuery(String query, ConnectionInfo ci) throws SQLException {
java.sql.Statement s;
try {
s = getStatement(ci);
} catch (Exception e) {
System.out.println(e);
// reconnect();
try {
s = getStatement(ci);
} catch (Exception f) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -