Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:求一个比较好用的数据库连接池组件 [Re:CrazyJavar]
yung





发贴: 19
积分: 10
于 2003-04-18 15:42 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
File: DBConnectionManager.java:

package beans;

/**
* @author
*
*/

import java.util.Vector;
import java.util.Enumeration;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.MissingResourceException;

/**
* Database Connection Manager
*/
public class DBConnectionManager {

private static final String OPTION_FILE_NAME = "DBOptions";
private final static DBConnectionManager instance=new DBConnectionManager();
private DBConnectionPool pool;

/**
* Use singleton pattern, only return one instance of DBConnectionManager.
* 取得 DBConnectionManager 实例的唯一方式
* @return DBConnectionManager
*/
public static DBConnectionManager getInstance() {
return instance;
}
/**
* Get a connection
* @return Connection
*/
public Connection getConnection() throws SQLException{
return pool.getConnection();
}
/**
* Free a connection
* @param con connection
* @throws SQLException this method
*/
public void freeConnection(Connection con) throws SQLException{
pool.freeConnection(con);

}

private DBConnectionManager() {
init();
}

private void init() {
String db_driver = "";
String db_url = "";
String db_user = "";
String db_password = "";
int db_maxConn=0;

try {
ResourceBundle res = ResourceBundle.getBundle(OPTION_FILE_NAME);
db_driver = res.getString("DB_DRIVER").trim();
db_url = res.getString("DB_URL").trim();
db_user = res.getString("DB_USER").trim();
db_password = res.getString("DB_PASSWORD").trim();
db_maxConn = Integer.parseInt(res.getString("DB_MAX_CONNECTIONS"));

Class.forName(db_driver);
pool = new DBConnectionPool(db_url,db_user,db_password,db_maxConn);
} catch (Exception ex) {
System.out.println(ex);
}
System.out.println("ConnectionPool Init OK ! " +
"URL : "+ db_url +
", User : " + db_user +
", MaxConn : " + db_maxConn);
}

/**
* Closes all open connections.
*/
public void release() {
pool.release();
}

public String toString() {
return pool.toString();
}
/************************************************************************************
************************************************************************************/
// inner class

class DBConnectionPool {

private Vector freeConnections = new Vector();
private int maxConn;
private int connNumb;

private String URL;
private String user;
private String password;

public DBConnectionPool(String URL, String user, String password,int maxConn) {
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}

public synchronized void freeConnection(Connection con) {
freeConnections.addElement(con);
connNumb--;
notifyAll(); // useful??
}

public synchronized Connection getConnection() throws SQLException{
Connection con = null;
if (freeConnections.size() > 0) {
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
con = getConnection();
}
}
catch (SQLException e) {
con = getConnection();
}
}
else if (maxConn == 0 || connNumb < maxConn) {
con = newConnection();
}
if (con != null) {
connNumb++;
}
return con;
}

private Connection newConnection() throws SQLException{
Connection con =DriverManager.getConnection(URL,user, password);
return con;
}

synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
} catch (SQLException e) {
System.out.println("Can't close connection in DBConnectionPool.");
}
}
freeConnections.removeAllElements();
}

public synchronized String toString() {
String info =
"ConnectionPool(" + URL + "," + user + ")" +
", totalConn =" + connNumb +
", freeConnetion=" + freeConnections.size() +
", max=" + maxConn;
return(info);
}
} /// Inner class End.

}///~
-------------------------------------------------------------------------------------------
DBOptions.properties:

DB_DRIVER = org.mysql......Driver
DB_URL = jdbc:mysql...........
DB_USER = yung
DB_PASSWORD = passwd
DB_MAX_CONNECTIONS = 20
-------------------------------------------------------------------------------------------
File Test.java: (测试一下)
package beans;

/**
* @author
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/

import java.sql.*;

public class Test {
  public static void main(String[] args) throws Exception {
    DBConnectionManager dbcm = DBConnectionManager.getInstance();

    dbcm.release();
    Connection conn = dbcm.getConnection();

    System.out.println(dbcm);

    Statement stmt = null;
    ResultSet rs = null;
    ResultSetMetaData md = null;

    try {
      stmt = conn.createStatement();
      rs = stmt.executeQuery("select * from users");
      md = rs.getMetaData();
      while (rs.next()) {
        for (int i = 1; i <= md.getColumnCount(); i++) {
          System.out.print(rs.getStringLight Bulb + " | ");
        }
        System.out.println("");
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      dbcm.freeConnection(conn);
      dbcm.freeConnection(conn1);
    }
    dbcm.release();
    System.out.println(dbcm);

  }
}




关于Jute Forum的长期发展给官方的建议

话题树型展开
人气 标题 作者 字数 发贴时间
10292 求一个比较好用的数据库连接池组件 jfml 2 2003-04-18 11:04
9294 Re:求一个比较好用的数据库连接池组件 jfml 12 2003-04-18 12:05
9197 Re:求一个比较好用的数据库连接池组件 jfml 8 2003-04-19 00:46
8648 Re:求一个比较好用的数据库连接池组件 rainman 80 2003-04-19 02:55
8469 Re:求一个比较好用的数据库连接池组件 jeez 52 2003-04-19 20:46
8538 Re:求一个比较好用的数据库连接池组件 harvshen 18 2003-04-22 14:34
8482 Re:求一个比较好用的数据库连接池组件 DesertApple 34 2003-04-18 13:25
9192 Re:求一个比较好用的数据库连接池组件 jfml 16 2003-04-18 13:26
8882 Re:求一个比较好用的数据库连接池组件 yung 6621 2003-04-18 15:42
9262 Re:求一个比较好用的数据库连接池组件 jfml 30 2003-04-18 18:12
8489 Re:求一个比较好用的数据库连接池组件 jeez 72 2003-04-18 19:29
8435 Re:求一个比较好用的数据库连接池组件 black1234 33 2003-04-18 19:31
9505 Re:求一个比较好用的数据库连接池组件 emarket 107 2003-04-22 15:05
9231 Re:求一个比较好用的数据库连接池组件 jfml 63 2003-04-18 20:10
8501 Re:求一个比较好用的数据库连接池组件 jeez 20 2003-04-18 23:23

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923