Topic: 求一个比较好用的数据库连接池组件 |
Print this page |
1.求一个比较好用的数据库连接池组件 | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-18 11:04 谢谢 |
2.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-18 12:05 不要太庞大的 谢谢 |
3.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: DesertApple Posted on: 2003-04-18 13:25 IBM java Developer 专区有教程,自己参照做一个吧~ |
4.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-18 13:26 那里的文章只是简单地介绍一下而已 |
5.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: yung Posted on: 2003-04-18 15:42 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.getString + " | "); } System.out.println(""); } } catch (Exception e) { e.printStackTrace(); } finally { dbcm.freeConnection(conn); dbcm.freeConnection(conn1); } dbcm.release(); System.out.println(dbcm); } } |
6.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-18 18:12 上面这个程序一旦得8到连接就返回NULL 觉得不太好吧 |
7.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jeez Posted on: 2003-04-18 19:29 DBCP是Apache的一个项目,详细内容见http://jakarta.apache.org/commons/dbcp/index.html。 |
8.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: black1234 Posted on: 2003-04-18 19:31 楼上的组件容易在池中保留死连接。 最好还是用server自带的。 |
9.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-18 20:10 是这样的 我做的是个大型的Swing Application 不是B/S的阿 想用个简单但又不能太差的连接池 |
10.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jeez Posted on: 2003-04-18 23:23 从Tomcat,Jboss等项目中扒吧。 |
11.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jfml Posted on: 2003-04-19 00:46 独立出来容易么? |
12.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: rainman Posted on: 2003-04-19 02:55 如果是Swing程序就没必要用Connection Pool了,因为你不会弄几个并发线程去访问数据库吧?用一个保持数据库连接就可以了。如果断了再自动重新连一次。 |
13.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: jeez Posted on: 2003-04-19 20:46 像Tomcat,JBoss这类软件,架构一般都比较灵活,设计较好,也就是说比较独立的组件很容易剥离出来。 |
14.Re:求一个比较好用的数据库连接池组件 [Re: CrazyJavar] | Copy to clipboard |
Posted by: harvshen Posted on: 2003-04-22 14:34 留下你的EMAIL, 我发一个给你. |
15.Re:求一个比较好用的数据库连接池组件 [Re: black1234] | Copy to clipboard |
Posted by: emarket Posted on: 2003-04-22 15:05 DBCP是tomcat缺省得 connection pool 我一直用它,很少,死连我也遇到过(连接MySQL),是因为 MySQL得问题,在url中把加入autoReconnect=true就可以解决了 |
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 |