///连接池 import java.sql.*; import java.util.*;public class ConnectionPoolBean {
static private int checkedOut; private Vector freeConnections=new Vector(); static private int maxConn;//the up-bound of the connections available; // 0 denotes no up-bound private String sDBDriver = "oracle.jdbc.driver.OracleDriver"; private String sConnStr = "jdbc:oracle:thin:@datanames:1521:sid"; private String username; private String passwd; public ConnectionPoolBean() //initialization { checkedOut=0; maxConn=8; passwd=new String("pass"); username=new String("user"); } public ConnectionPoolBean(String username,String passwd) //initialization { checkedOut=0; maxConn=8; this.username=new String(username); this.passwd=new String(passwd); }
public synchronized void freeConnection(Connection con) //return a connection to the ConnectionPool { freeConnections.addElement(con); checkedOut--; }
public synchronized Connection getConnection() throws Exception //to get a usable connection; // if returnvalue=null, then no connection available { Connection con=null; if (freeConnections.size()>0) //there are connections which can be reused { con=(Connection)freeConnections.firstElement(); //get the first available connection freeConnections.removeElementAt(0); //remove it from the connectionPool try { if (con.isClosed()) //if the connection is closed { con=getConnection(); } } catch(SQLException e) //if the connection is not valid { con=getConnection(); } } else //if there are no existing connections, then create a new one if (maxConn==0 || checkedOut<maxConn) { try { con=newConnection(); } catch (Exception e) { throw e; } }
if (con!=null) { checkedOut++; } return con; }
public synchronized void release() throws Exception //to close all connections { Enumeration allConnections=freeConnections.elements(); while (allConnections.hasMoreElements()) { Connection con=(Connection)allConnections.nextElement(); try { con.close(); } catch (Exception e) { throw e; } } freeConnections.removeAllElements(); } private Connection newConnection() throws Exception //create a new connection { Connection con=null; try { Class.forName(sDBDriver); con = DriverManager.getConnection(sConnStr,username,passwd); } catch(Exception e) { throw e; } return con; } }
finally{
conn.close();
}
import java.sql.*;
import java.util.*;public class ConnectionPoolBean
{
static private int checkedOut;
private Vector freeConnections=new Vector();
static private int maxConn;//the up-bound of the connections available;
// 0 denotes no up-bound private String sDBDriver = "oracle.jdbc.driver.OracleDriver";
private String sConnStr = "jdbc:oracle:thin:@datanames:1521:sid";
private String username;
private String passwd;
public ConnectionPoolBean() //initialization
{
checkedOut=0;
maxConn=8;
passwd=new String("pass");
username=new String("user");
}
public ConnectionPoolBean(String username,String passwd) //initialization
{
checkedOut=0;
maxConn=8;
this.username=new String(username);
this.passwd=new String(passwd);
}
public synchronized void freeConnection(Connection con)
//return a connection to the ConnectionPool
{
freeConnections.addElement(con);
checkedOut--;
}
public synchronized Connection getConnection() throws Exception
//to get a usable connection;
// if returnvalue=null, then no connection available
{
Connection con=null;
if (freeConnections.size()>0) //there are connections which can be reused
{
con=(Connection)freeConnections.firstElement();
//get the first available connection
freeConnections.removeElementAt(0);
//remove it from the connectionPool
try
{
if (con.isClosed()) //if the connection is closed
{
con=getConnection();
}
}
catch(SQLException e) //if the connection is not valid
{
con=getConnection();
}
}
else //if there are no existing connections, then create a new one
if (maxConn==0 || checkedOut<maxConn)
{
try
{
con=newConnection();
}
catch (Exception e)
{
throw e;
}
}
if (con!=null)
{
checkedOut++;
}
return con;
}
public synchronized void release() throws Exception
//to close all connections
{
Enumeration allConnections=freeConnections.elements();
while (allConnections.hasMoreElements())
{
Connection con=(Connection)allConnections.nextElement();
try
{
con.close();
}
catch (Exception e)
{ throw e;
}
}
freeConnections.removeAllElements();
} private Connection newConnection() throws Exception
//create a new connection
{
Connection con=null;
try
{
Class.forName(sDBDriver);
con = DriverManager.getConnection(sConnStr,username,passwd);
}
catch(Exception e)
{
throw e;
}
return con;
}
}
import java.sql.*;public class sqlpool_bean
{
Statement stmt=null;
ResultSet rs = null; public sqlpool_bean()
{
}
public void databaseOpen(Connection conn) throws Exception
{
try
{
stmt = conn.createStatement();
} catch(Exception ex)
{
throw ex;
}
} public void databaseClose() throws Exception
{
try
{
stmt.close();
}
catch(Exception ex)
{
throw ex;
}
}
public ResultSet executeQuery(Connection conn,String sql) throws Exception
{
try
{
rs = stmt.executeQuery(sql);
}
catch(Exception ex)
{
throw ex;
}
return rs;
}
public void executeUpdate(Connection conn,String sql) throws Exception
{
try
{
stmt.executeUpdate(sql);
}
catch(Exception ex)
{
throw ex;
}
}
}
尝试在jsp程序的末尾关闭,好像也不行sandsworlf(安) ,你的连接池在什么时候释放数据库的连接啊?详细说说