請教一個Java.sql.SQLException錯誤問題

TyroneChen 2003-05-06 02:01:35
我在Java裡用連接池的方式來訪問數據庫,共有50條連接在連接池中,為什麼在真正多用戶使用時經常會出現:連接正忙碌於別一個hstml結果?
...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
TyroneChen 2003-05-06
  • 打赏
  • 举报
回复
隨便幫我看看有什麼地方得改進的沒有
TyroneChen 2003-05-06
  • 打赏
  • 举报
回复
以下是我在用的連接池代碼,一起研究研究:
package mrp.util;
import java.util.Stack;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.util.Enumeration;

public final class ConnectionPool {

public Stack pool;
private long timeout;
private static ConnectionPool mySelf;
private Connection con;

private ConnectionPool() {
}

public synchronized static ConnectionPool getInstance(){

if(mySelf == null){
mySelf = new ConnectionPool();
}

return mySelf;
}

public synchronized void initialize(int numCons,long timeout) throws SQLException{
if(pool != null){
throw new SQLException ("Pool already initialized");
}
try{
this.timeout = timeout;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
pool = new Stack();
for(int i=0 ; i < numCons;i++){
pool.push(DriverManager.getConnection("jdbc:odbc:MyDB","sa","123456"));
}
}
catch(ClassNotFoundException ex){
throw new SQLException("Driver not found!!!");
}
}
public synchronized Connection getConnection() throws SQLException{
if(pool == null){
throw new SQLException ("Pool not initialized,不能連接到數據庫,請重新登錄");
}
/*
while(pool.empty()){
try{
wait(timeout);
}
catch(InterruptedException ex){
throw new SQLException ("Connection not available,目前無有效的數據庫連接,請待會再試");
}
}
*/
con = (Connection) pool.pop();
return con;
}

public synchronized void releaseConnection(Connection con) throws SQLException {
if(pool == null){
throw new SQLException ("Pool not initialized,不能連接到數據庫,請重新登錄");
}
pool.push(con);
notifyAll();
}
public synchronized int getConnectionCount() throws Exception{
//返回當前連接池中可用的連接數
return pool.size();
}
public void destroyPool() throws SQLException {
if(pool == null){
throw new SQLException("Pool not initialized,不能連接到數據庫,請重新登錄");
}
while(!pool.empty()){
((Connection)pool.pop()).close();
pool = null;
}
}
}
enhydraboy 2003-05-06
  • 打赏
  • 举报
回复
你的DBManager有没有做过连接回收的清理工作?
还有,把你的连接池的java代码贴出来。

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧