数据连接池的工作机制 J2EE服务器启动时会建立一定数量的池连接,并一直维持不少于此数目的池连接。客户端程序需要连接时,池驱动程序会返回一个未使用的池连接并将其表记为忙。如果当前没有空闲连接,池驱动程序就新建一定数量的连接,新建连接的数量有配置参数决定。当使用的池连接调用完成后,池驱动程序将此连接表记为空闲,其他调用就可以使用这个连接。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在书上看到的一个实现缓冲池的例子,希望对大家有用package sqlpool;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import java.util.Iterator;import java.util.Vector;public class PooledConnection { private static int maxConns=10; private static int activeConns;//当前活动的connection private int maxStmts=10; private int activeStmts=0; private static String strCon=null; private static String DBuser=""; private static String DBpasswd=""; private static Vector freeConnections=new Vector(); private Vector freeStatements=new Vector(); private Connection conn=null;//包装的connection实例 static void ininPooledConection(String URL,String user,String password,int max) {//初始化配置 strCon=URL; DBuser=user; DBpasswd=password; maxConns=max; activeConns=0; } private Connection getConn() { return conn; } private void setConn(Connection conn) { this.conn=conn; } //从缓冲池获取connection,无可用连接则新建 public static synchronized PooledConnection getConnection() throws Exception { PooledConnection conn=null; if(freeConnections.size()>0) { conn=(PooledConnection)freeConnections.firstElement(); freeConnections.removeElementAt(0); try{ if(conn.getConn().isClosed()) { conn=getConnection(); } }catch(SQLException e) { conn=getConnection(); } } else if(activeConns<maxConns) { conn=createConnection(); if(conn!=null) activeConns++; else throw new NullPointerException(); }return conn; } private static PooledConnection createConnection() { // TODO Auto-generated method stub PooledConnection conn=null; try { conn=new PooledConnection(); if(DBuser==null)conn.setConn(DriverManager.getConnection(strCon)); else conn.setConn(DriverManager.getConnection(strCon,DBuser,DBpasswd)); System.out.println("pooledconnection created new connection successful"); }catch(Exception e) { e.printStackTrace(); } return conn; } public synchronized void close() { freeConnections.addElement(this); activeConns--; notifyAll();//同步通知 } //释放所有资源 public synchronized void releaseAll() { for(Iterator it=freeConnections.iterator();it.hasNext();) { PooledConnection conn=(PooledConnection)it.next(); try { conn.getConn().close(); conn.releaseAllStatement(); }catch(Exception e) { } } } public synchronized Statement createStatement() { Statement stmt=null; if(freeStatements.size()>0) { stmt=(Statement)freeStatements.firstElement(); freeStatements.removeElement(0); }else if(activeStmts<maxStmts) try { stmt=getConn().createStatement(); }catch(SQLException e) { e.printStackTrace(); } if(stmt!=null) activeStmts++; return stmt; } public synchronized void closeStatement(Statement stmt) { freeStatements.addElement(stmt); activeStmts--; notifyAll(); } public synchronized void releaseAllStatement() { // TODO Auto-generated method stub for(Iterator it=freeStatements.iterator();it.hasNext();) { Statement stmt=(Statement)it.next(); try { stmt.close(); }catch(Exception e) { e.printStackTrace(); } } freeStatements.removeAllElements(); } } 这个问题有谁见过,怎么解决的? 如何在无JDK的电脑上运行jar 交换得回文串 怎么样用GUI做个跟QQ界面一样,基本登陆界面已经有,插入画面应该用什么方法 如何让java的应用程序支持鼠标中键 100分向多线程高手求救!!!急急!!!!!! 调用dll的问题???请高手指教!!! 怎样用Java实现对access数据库建的题库进行抽题 微信JSSDK录音 java windows桌面共享 一个生画图生成图片的问题!!!! 关于JFiedText.setText()的一个问题
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Vector;public class PooledConnection {
private static int maxConns=10;
private static int activeConns;//当前活动的connection
private int maxStmts=10;
private int activeStmts=0;
private static String strCon=null;
private static String DBuser="";
private static String DBpasswd="";
private static Vector freeConnections=new Vector();
private Vector freeStatements=new Vector();
private Connection conn=null;//包装的connection实例
static void ininPooledConection(String URL,String user,String password,int max)
{//初始化配置
strCon=URL;
DBuser=user;
DBpasswd=password;
maxConns=max;
activeConns=0;
}
private Connection getConn()
{
return conn;
}
private void setConn(Connection conn)
{
this.conn=conn;
}
//从缓冲池获取connection,无可用连接则新建
public static synchronized PooledConnection getConnection() throws Exception
{
PooledConnection conn=null;
if(freeConnections.size()>0)
{
conn=(PooledConnection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
try{
if(conn.getConn().isClosed())
{
conn=getConnection();
}
}catch(SQLException e)
{
conn=getConnection();
}
}
else if(activeConns<maxConns)
{
conn=createConnection();
if(conn!=null) activeConns++;
else throw new NullPointerException();
}return conn;
}
private static PooledConnection createConnection() {
// TODO Auto-generated method stub
PooledConnection conn=null;
try
{
conn=new PooledConnection();
if(DBuser==null)conn.setConn(DriverManager.getConnection(strCon));
else conn.setConn(DriverManager.getConnection(strCon,DBuser,DBpasswd));
System.out.println("pooledconnection created new connection successful");
}catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
public synchronized void close()
{
freeConnections.addElement(this);
activeConns--;
notifyAll();//同步通知
}
//释放所有资源
public synchronized void releaseAll()
{
for(Iterator it=freeConnections.iterator();it.hasNext();)
{
PooledConnection conn=(PooledConnection)it.next();
try
{
conn.getConn().close();
conn.releaseAllStatement();
}catch(Exception e)
{
}
}
}
public synchronized Statement createStatement()
{
Statement stmt=null;
if(freeStatements.size()>0)
{
stmt=(Statement)freeStatements.firstElement();
freeStatements.removeElement(0);
}else if(activeStmts<maxStmts)
try
{
stmt=getConn().createStatement();
}catch(SQLException e)
{
e.printStackTrace();
}
if(stmt!=null)
activeStmts++;
return stmt;
}
public synchronized void closeStatement(Statement stmt)
{
freeStatements.addElement(stmt);
activeStmts--;
notifyAll();
}
public synchronized void releaseAllStatement() {
// TODO Auto-generated method stub
for(Iterator it=freeStatements.iterator();it.hasNext();)
{
Statement stmt=(Statement)it.next();
try
{
stmt.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
freeStatements.removeAllElements();
}
}