免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 798 | 回复: 0
打印 上一主题 下一主题

JDBC连接池 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-15 12:02 |只看该作者 |倒序浏览

                在连接数量及连接时间(是否超时)上面没有很好的处理,望多指教!
package com.dlut.zxf.sql;import java.util.*;import java.sql.*;public class ConnectionPool {    private Hashtable connections = new Hashtable();    private Properties props;        public ConnectionPool(Properties pros, int initialConnections)    throws SQLException, ClassNotFoundException {        this.props = props;        initializePool(props, initialConnections);    }    public ConnectionPool(String driverClassName, String dbURL,             String user, String password, int initialConnections)     throws SQLException, ClassNotFoundException {        props = new Properties();        props.put("connection.driver", driverClassName);        props.put("connection.url", dbURL);        props.put("user", user);        props.put("password", password);        initializePool(props, initialConnections);    }        /*     * 得到一个新的连接     */    public Connection getConnection() throws SQLException {        Connection conn = null;        Enumeration connEnum = connections.keys();        synchronized(connections) {            while(connEnum.hasMoreElements()) {                conn = connEnum.nextElement();                Boolean b = connections.get(conn);                if (b.equals(Boolean.FALSE)) {                    try {                        conn.setAutoCommit(true);                    } catch(SQLException e) {                        e.printStackTrace();                        connections.remove(conn);                        conn = getNewConnection();                    }                    connections.put(conn, Boolean.TRUE);                    //connections.put(conn, Boolean.FALSE); ?                    return conn;                }            }            conn = getNewConnection();            connections.put(conn, Boolean.TRUE);            return conn;        }    }        public void returnConnection(Connection returned) {        if (connections.containsKey(returned)) {            connections.put(returned, Boolean.FALSE);        }    }        private void initializePool(Properties props, int initialConnections)    throws SQLException, ClassNotFoundException {        Class.forName(props.getProperty("connection.driver"));        for (int i=0; i            Connection conn = getNewConnection();            connections.put(conn, Boolean.FALSE);        }    }        private Connection getNewConnection() throws SQLException {        return DriverManager.getConnection(props.getProperty("connection.url"), props);    }}
源代码下载

       
        文件:ConnectionPool.rar
        大小:0KB
        下载:
下载
       
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/35675/showart_496954.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP