一个简单的数据库连接池(连接个数,连接时间限制)
2009-09-10 14:46
567 查看
/* 数据库连接池 ***********模块说明************** getInstance()返回POOL唯一实例,第一次调用时将执行构造函数 构造函数Pool()调用驱动装载loadDrivers()函数;连接池创建createPool()函数 loadDrivers()装载驱动 createPool()建连接池 getConnection()返回一个连接实例 getConnection(long time)添加时间限制 freeConnection(Connection con)将con连接实例返回到连接池 getnum()返回空闲连接数 getnumActive()返回当前使用的连接数 */ import java.sql.*; public class Pool { static private Pool instance = null; //定义唯一实例 private int maxConnect = 100;//最大连接数 private int normalConnect = 10;//保持连接数 private String password = "";//密码 private String url = "jdbc:mysql://localhost:3306/DB";//连接URL private String user = "";//用户名 private String driverName = "";//驱动类 Driver driver = null;//驱动变量 DBConnectionPool pool = null;//连接池实例变量 /** * 返回唯一实例 * @return */ static synchronized public Pool getInstance() { if (instance == null) { instance = new Pool(); } return instance; } /** * 将构造函数私有,不允许外界访问 */ private Pool() { loadDrivers(driverName); createPool(); } /** * 装载和注册所有JDBC驱动程序 * @param dri */ private void loadDrivers(String dri) { String driverClassName = dri; try{ driver = (Driver) Class.forName(driverClassName).newInstance(); DriverManager.registerDriver(driver); System.out.println("成功注册JDBC驱动程序" + driverClassName); }catch (Exception e) { System.out.println("无法注册JDBC驱动程序:" + driverClassName + ",错误:" + e); } } /** * 创建连接池 */ private void createPool() { pool = new DBConnectionPool(password, url, user, normalConnect, maxConnect); if (pool != null) { System.out.println("创建连接池成功"); }else { System.out.println("创建连接池失败"); } } /** * 获得一个可用的连接,如果没有则创建一个连接,且小于最大连接限制 * @return */ public Connection getConnection() { if (pool != null) { return pool.getConnection(); } return null; } /** * 获得一个连接,有时间限制 * @param time * @return */ public Connection getConnection(long time) { if (pool != null) { return pool.getConnection(time); } return null; } /** * 将连接对象返回给连接池 * @param con */ public void freeConnection(Connection con) { if (pool != null) { pool.freeConnection(con); } } /** * 返回当前空闲连接数 * @return */ public int getnum(){ return pool.getnum(); } /** * 返回当前连接数 * @return */ public int getnumActive(){ return pool.getnumActive(); } /** * 关闭所有连接,撤销驱动注册 */ public synchronized void release() { pool.release(); try { DriverManager.deregisterDriver(driver);//撤销驱动 System.out.println("撤销JDBC驱动程序 " + driver.getClass().getName()); }catch (SQLException e) { System.out.println("无法撤销JDBC驱动程序的注册:" + driver.getClass().getName()); } } } //生成数据连接池 import java.sql.*; import java.util.*; import java.util.Date; public class DBConnectionPool { private int checkedOut; private Vector<Connection> freeConnections = new Vector<Connection>(); private int maxConn; private String password; private String url; private String user; private static int num=0;//空闲的连接数 private static int numActive=0;//当前的连接数 public DBConnectionPool(String password, String url, String user,int normalConn, int maxConn) { this.password = password; this.url = url; this.user = user; this.maxConn = maxConn; for (int i = 0; i < normalConn; i++) { //初始normalConn个连接 Connection c = newConnection(); if (c != null) {freeConnections.addElement(c);num++;} } } /** * 释放不用的连接到连接池 */ public synchronized void freeConnection(Connection con) { freeConnections.addElement(con); num++; checkedOut--; numActive--; notifyAll(); } /** * 获取一个可用连接 * @return */ public synchronized Connection getConnection() { Connection con = null; if (freeConnections.size() > 0) { //还有空闲的连接 num--; con = (Connection) freeConnections.firstElement(); freeConnections.removeElementAt(0); try { if (con.isClosed()) { System.out.println("从连接池删除一个无效连接"); con = getConnection(); } }catch (SQLException e) { System.out.println("从连接池删除一个无效连接"); con = getConnection(); } } else if (maxConn == 0 || checkedOut < maxConn) { //没有空闲连接且当前连接小于最大允许值,最大值为0则不限制 con = newConnection(); } if (con != null) { //当前连接数加1 checkedOut++; } numActive++; return con; } /** * 获取一个连接,并加上等待时间限制,时间为毫秒 * @param timeout * @return */ public synchronized Connection getConnection(long timeout) { long startTime = new Date().getTime(); Connection con; while ((con = getConnection()) == null){ try { wait(timeout); }catch (InterruptedException e) {} if((new Date().getTime() - startTime) >= timeout) { System.out.println("超时了!!"); return null; //超时返回 } } return con; } /** * 关闭所有连接 */ @SuppressWarnings("unchecked") public synchronized void release() { Enumeration allConnections = freeConnections.elements(); while (allConnections.hasMoreElements()) { Connection con = (Connection) allConnections.nextElement(); try { con.close(); num--; }catch (SQLException e) { System.out.println("无法关闭连接池中的连接"); } } freeConnections.removeAllElements(); numActive=0; } /** * 创建一个新连接 * @return */ private Connection newConnection() { Connection con = null; try { if (user == null) { //用户,密码都为空 con = DriverManager.getConnection(url); }else { con = DriverManager.getConnection(url, user, password); } System.out.println("连接池创建一个新的连接"); }catch (SQLException e) { System.out.println("无法创建这个URL的连接" + url); return null; } return con; } /** * 返回当前空闲连接数 * @return */ public int getnum(){ return num; } /** * 返回当前连接数 * @return */ public int getnumActive(){ return numActive; } }
相关文章推荐
- 用JS写的简单的猜数的游戏,一个是步数限制的,一个是有时间限制的。
- C语言连接到数据库实现一个具有简单的client端功能
- 通过动态代理(Proxy)实现的数据库连接池的创建连接与归还链接的操作的简单的实现流程
- 一个简单的连接数据库程序(JDBC)
- 用jsp写一个简单登录界面(非连接数据库)
- A Simple JDBC MySQL Connection.一个JDBC到MySQL的简单数据库连接.
- Spring Boot应用连接数据库MySQL、及一个简单的demo
- 通过动态代理(Proxy)实现的数据库连接池的创建连接与归还链接的操作的简单的实现流程
- 自己实现的一个简单的数据库连接的插件————JdbcPlugin
- [原创]java WEB学习笔记12:一个简单的serlet连接数据库实验
- 一个简单的数据库连接池示例
- 一个简单的反射连接程序(修改文件时间,以及创建Windows服务)
- 一个简单的数据库插入时间插入策略
- 通过一个简单的数据库连接池分析一下“等待超时模式”
- 以一个简单的数据库表为例来展示创建全局/局部数据源和连接池的配置与测试
- 写了一个简单的数据库连接池
- Ruby on Rails,一个简单的CMS系统,创建应用骨架并与数据库连接
- 菜鸟的 PHP 学习之路(三):一个简单的连接数据库并查询的小程序(1)
- 试着用Java写的一个管理数据库连接的简单数据库连接池