SimpleConnetionPool.rar

  • PUDN用户
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 4
    下载次数
  • 2009-11-26 11:02
    上传日期
数据库连接池代码,如果要 new Connection 就可以直接从数据库连接池中返回 Connection
SimpleConnetionPool.rar
  • SimpleConnetionPool.java
    8.2KB
内容介绍
package com.database; import java.sql.*; import java.lang.reflect.*; import java.util.*; import java.io.*; public class SimpleConnetionPool { private static LinkedList m_notUsedConnection = new LinkedList(); private static HashSet m_usedUsedConnection = new HashSet(); private static String m_driver = ""; private static String m_url = ""; private static String m_user = ""; private static String m_password = ""; static final boolean DEBUG = true; static private long m_lastClearClosedConnection = System.currentTimeMillis(); public static long CHECK_CLOSED_CONNECTION_TIME = 4 * 60 * 60 * 1000; // 4 static { initDriver(); } private SimpleConnetionPool() { } public static void initDriver() { Driver driver = null; //load the special db_driver try { driver = (Driver) Class.forName(m_driver).newInstance(); installDriver(driver); } catch (Exception e) { e.printStackTrace(); } // // load mysql driver // try { // driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance(); // installDriver(driver); // } catch (Exception e) { // } // // // load postgresql driver // try { // driver = (Driver) Class.forName("org.postgresql.Driver").newInstance(); // installDriver(driver); // } catch (Exception e) { // } } public static void installDriver(Driver driver) { try { DriverManager.registerDriver(driver); } catch (Exception e) { e.printStackTrace(); } } public static synchronized Connection getConnection() { clearClosedConnection(); while (m_notUsedConnection.size() > 0) { try { ConnectionWrapper wrapper = (ConnectionWrapper) m_notUsedConnection.removeFirst(); if (wrapper.connection.isClosed()) { continue; } m_usedUsedConnection.add(wrapper); if (DEBUG) { wrapper.debugInfo = new Throwable( "Connection initial statement"); } return wrapper.connection; } catch (Exception e) { } } int newCount = getIncreasingConnectionCount(); LinkedList list = new LinkedList(); ConnectionWrapper wrapper = null; for (int i = 0; i < newCount; i++) { wrapper = getNewConnection(); if (wrapper != null) { list.add(wrapper); } } if (list.size() == 0) { return null; } wrapper = (ConnectionWrapper) list.removeFirst(); m_usedUsedConnection.add(wrapper); m_notUsedConnection.addAll(list); list.clear(); return wrapper.connection; } private static ConnectionWrapper getNewConnection() { try { Connection con = DriverManager.getConnection(m_url, m_user, m_password); ConnectionWrapper wrapper = new ConnectionWrapper(con); return wrapper; } catch (Exception e) { e.printStackTrace(); } return null; } static synchronized void pushConnectionBackToPool(ConnectionWrapper con) { boolean exist = m_usedUsedConnection.remove(con); if (exist) { m_notUsedConnection.addLast(con); } } public static int close() { int count = 0; Iterator iterator = m_notUsedConnection.iterator(); while (iterator.hasNext()) { try { ((ConnectionWrapper) iterator.next()).close(); count++; } catch (Exception e) { } } m_notUsedConnection.clear(); iterator = m_usedUsedConnection.iterator(); while (iterator.hasNext()) { try { ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next(); wrapper.close(); if (DEBUG) { wrapper.debugInfo.printStackTrace(); } count++; } catch (Exception e) { } } m_usedUsedConnection.clear(); return count; } private static void clearClosedConnection() { long time = System.currentTimeMillis(); // sometimes user change system time,just return if (time < m_lastClearClosedConnection) { time = m_lastClearClosedConnection; return; } // no need check very often if (time - m_lastClearClosedConnection < CHECK_CLOSED_CONNECTION_TIME) { return; } m_lastClearClosedConnection = time; // begin check Iterator iterator = m_notUsedConnection.iterator(); while (iterator.hasNext()) { ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next(); try { if (wrapper.connection.isClosed()) { iterator.remove(); } } catch (Exception e) { iterator.remove(); if (DEBUG) { System.out .println("connection is closed, this connection initial StackTrace"); wrapper.debugInfo.printStackTrace(); } } } // make connection pool size smaller if too big int decrease = getDecreasingConnectionCount(); if (m_notUsedConnection.size() < decrease) { return; } while (decrease-- > 0) { ConnectionWrapper wrapper = (ConnectionWrapper) m_notUsedConnection .removeFirst(); try { wrapper.connection.close(); } catch (Exception e) { } } } /** * get increasing connection count, not just add 1 connection * * @return count */ public static int getIncreasingConnectionCount() { int count = 1; int current = getConnectionCount(); count = current / 4; if (count < 1) { count = 1; } return count; } /** * get decreasing connection count, not just remove 1 connection * * @return count */ public static int getDecreasingConnectionCount() { int count = 0; int current = getConnectionCount(); if (current < 10) { return 0; } return current / 3; } public synchronized static void printDebugMsg() { printDebugMsg(System.out); } public synchronized static void printDebugMsg(PrintStream out) { if (DEBUG == false) { return; } StringBuffer msg = new StringBuffer(); msg.append("debug message in " + SimpleConnetionPool.class.getName()); msg.append("\r\n"); msg.append("total count is connection pool: " + getConnectionCount()); msg.append("\r\n"); msg.append("not used connection count: " + getNotUsedConnectionCount()); msg.append("\r\n"); msg.append("used connection, count: " + getUsedConnectionCount()); out.println(msg); Iterator iterator = m_usedUsedConnection.iterator(); while (iterator.hasNext()) { ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next(); wrapper.debugInfo.printStackTrace(out); } out.println(); } public static synchronized int getNotUsedConnectionCount() { return m_notUsedConnection.size(); } public static synchronized int getUsedConnectionCount() { return m_usedUsedConnection.size(); } public static synchronized int getConnectionCount() { return m_notUsedConnection.size() + m_usedUsedConnection.size(); } public static String getUrl() { return m_url; } public static void setUrl(String url) { if (url == null) { return; } m_url = url.trim(); } public static String getUser() { return m_user; } public static void setUser(String user) { if (user == null) { return; } m_user = user.trim(); } public static String getPassword() { return m_password; } public static void setPassword(String password) { if (password == null) { return; } m_password = password.trim(); } public static String getDriver() { return m_driver; } public static void setDriver(String m_driver) { SimpleConnetionPool.m_driver = m_driver; } } class ConnectionWrapper implements InvocationHandler { private final static String CLOSE_METHOD_NAME = "close"; public Connection connection = null; private Connection m_originConnection = null; public long lastAccessTime = System.currentTimeMillis(); Throwable debugInfo = new Throwable("Connection initial statement"); ConnectionWrapper(Connection con) { Class[] interfaces = { java.sql.Connection.class }; this.connection = (Connection) Proxy.newProxyInstance(con.getClass() .getClassLoader(), interfaces, this); m_originConnection =
评论
    相关推荐
    • 数据库课程设计
      一个数据库课程设计,access管理工具实现,用的是窗体!
    • 数据库课程设计
      数据库课程设计十分完整有需要的请下载啊谢谢
    • 数据库课程设计
      广东工业大学数据库课程设计,可视化界面连接数据库,delphi7
    • 数据库课程设计
      数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述 数据库课程设计实验及其描述
    • 数据库课程设计
      数据库课程设计》由周爱武、汪海威、肖云编著,遵循数据库课程设计的具体要求,独立于具体的数据库教材,从实际应用系统的需求着手,引导读者逐步完成数据库设计全过程,重点讲解数据库系统的需求分析、概念设计、...
    • 数据库课程设计
      数据库课程设计人事管理系统 数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计人事管理系统数据库课程设计...
    • 数据库课程设计
      数据库课程设计,基于visual basic自助银行管理系统,界面很清爽,实用。同学都说好,所以就上传了!!!
    • 数据库课程设计
      数据库课程设计 里面有详细的文档资料 包含数据库一切的图 以及生成的数据库表文件 期末得分为优秀
    • 数据库课程设计
      可以作为数据库课程设计,也可以作为Java的课程设计,内容全面。本资源转载的,非本人原创。用于交流学习,特此申明!
    • 数据库课程设计
      数据库课程设计蓝天大学学生管理系统 2.商店信息管理系统 3.实验室机房收费管理系统 4.图书馆资料检索系统 5.企业库存管理系统 6.仓库管理系统 7.工程项目管理系统 8.教材管理系统 9.企业人事管理系统 10.企业财务...