result = null;
}
if(stmt != null)
{
stmt.close();
stmt = null;
}
conn.setautocommit(true);
connmgr.freeconnection(user, conn);
connmgr.release(user);
isclosed = true;
return true;
}
/**
* 没有调用 close() 时,执行 close()
*/
protected void finalize()
throws sqlexception
{
if(!isclosed)
close();
}
/**
* 取得 http 参数值,所有得到的值都做了
* string (request.getparameter(s.trim()).trim().getbytes("iso8859_1"), "gb2312") 处理
*/
public static string getparameter(httpservletrequest httpservletrequest, string s)
{
try
{
if(httpservletrequest.getparameter(s.trim()) != null)
return new string(httpservletrequest.getparameter(s.trim()).trim().getbytes("iso8859_1"), "gb2312");
else
return null;
}
catch(exception _ex)
{
return httpservletrequest.getparameter(s.trim());
}
}
private string host;
private string port;
private string database;
private string user;
private string password;
private string sid;
private boolean bautocommit;
public resultset result;
public statement stmt;
public int affectedrows;
public connection conn;
private boolean isclosed;
private dbconnectionmanager connmgr;
}
--------------------------------------------------------
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.date;
public class dbconnectionmanager {
static private dbconnectionmanager instance; // the single instance
private vector drivers = new vector();
private printwriter log;
private hashtable pools = new hashtable();
private hashtable clients = new hashtable();
/**
* a private constructor since this is a singleton
*/
private dbconnectionmanager()
throws sqlexception {
init();
}
/**
* returns the single instance, creating one if it's the
* first time this method is called.
*
* @return dbconnectionmanager the single instance.
*/
public static synchronized dbconnectionmanager getinstance()
throws sqlexception
{
if (instance == null) {
instance = new dbconnectionmanager();
}
return instance;
}
/**
* returns a connection to the named pool.
*
* @param name the pool name as defined in the properties file
* @param con the connection
*/
public void freeconnection(string name, connection con) {
dbconnectionpool pool = (dbconnectionpool) pools.get(name);
if (pool != null) {
pool.freeconnection(con);
}
}
/**
* returns an open connection. if no one is available, and the max
* number of connections has not been reached, a new connection is
* created.
*
* @param name the pool name as defined in the properties file
* @return connection the connection or null
*/
public connection getconnection(string name,string url,
string username,string password) throws sqlexception {
if (!clients.containskey(name))
{
createpools(name,url,username,password,0);
clients.put(name,new integer(0));
}
int icurr = ((integer)clients.get(name)).intvalue();
icurr++;
clients.put(name,new integer(icurr));
dbconnectionpool pool = (dbconnectionpool) pools.get(name);
if (pool != null) {
return pool.getconnection();
}
return null;
}
/**
* closes all open connections and deregisters all drivers.
*/
public synchronized void release(string name) throws sqlexception {
// wait until called by the last client
if (!clients.containskey(name)) return;
int icurr = ((integer)clients.get(name)).intvalue();
icurr--;
clients.put(name,new integer(icurr));
if (icurr > 0) return;
clients.remove(name);
dbconnectionpool pool = (dbconnectionpool)pools.get(name);
pool.release();
enumeration alldrivers = drivers.elements();
while (alldrivers.hasmoreelements()) {
driver driver = (driver) alldrivers.nextelement();
drivermanager.deregisterdriver(driver);
}
}
private void createpools(string poolname,string url,
string username,string password,int max) {
dbconnectionpool pool =
new dbconnectionpool(poolname,url,username,password, max);
pools.put(poolname, pool);
log("initialized pool " + poolname);
}
/**