- 浏览: 248137 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
mabusyao:
漠北空城 写道请问下,你这个是JDK版本是多少呢?!忘记了,应 ...
HashMap 源码解读 -
漠北空城:
请问下,你这个是JDK版本是多少呢?!
HashMap 源码解读 -
schumee:
完美团队~
项目沉思录 - 1.1 -
winie:
整理下 搞成引擎嘛 国产需要这样的engine
简单工作流引擎 -
mabusyao:
某位同学给我提供的堪称完美的解决方案:1. 将三个int数组放 ...
CraneWork
感谢网络提供资源的朋友,自己做了个版本,正在测试,看看是否有问题。
2009-09-08 更新成单例模式
2009-09-08 更新成单例模式
/* * $DatabaseInfo.java 2009-01-20 By Bill Yao $ */ package shopping.servlet.db; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; /** * DatabaseInfo class provided the basic information for DB connection. * * @author yaoh * */ public abstract class DatabaseInfo { private static ResourceBundle bundle = PropertyResourceBundle.getBundle("shopping"); protected static final String url = bundle.getString("DB_URL"); protected static final String username = bundle.getString("DB_USERNAME"); protected static final String password = bundle.getString("DB_PASSWORD"); protected static final String driverClass = bundle.getString("DB_DRIVER"); protected static final int INIT_CONNECTION = Integer.valueOf(bundle.getString("INIT_CONNECTION")); protected static final int ADD_CONNECTION = Integer.valueOf(bundle.getString("ADD_CONNECTION")); protected static final int MAX_CONNECTION = Integer.valueOf(bundle.getString("MAX_CONNECTION")); }
package shopping.servlet.db; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Enumeration; import java.util.Vector; import org.apache.log4j.Logger; /** * ConnectionPool provide database connections by pool. * * @author yaoh * */ public final class ConnectionPool extends DatabaseInfo{ static Logger logger = Logger.getLogger(ConnectionPool.class); // The connection pool. private Vector<PooledConnection> connections = null; // Table used to test connection. private String testTable = "TEST"; //Singleton pattern. private static final ConnectionPool pool = new ConnectionPool(); /** * Constructor */ private ConnectionPool() { try { createPool(); } catch (Exception e) { e.printStackTrace(); } } public static ConnectionPool getInstance() { return pool; } /** * Create the connection pool. * */ private synchronized void createPool() throws SQLException, ClassNotFoundException { if (connections != null) return; Class.forName(driverClass); connections = new Vector<PooledConnection>(); createConnections(INIT_CONNECTION); logger.info("Create connection pool successfully."); } /** * Create number of connections, add into connection pool. * @param num, number of connections. */ private void createConnections(int num) throws SQLException { for (int x = 0; x < num; x++) { if (MAX_CONNECTION > 0 && connections.size() >= MAX_CONNECTION) break; //add a new PooledConnection object to connections vector try { connections.addElement(new PooledConnection(newConnection())); } catch (SQLException e) { logger.error(" Create database connection failed. " + e.getMessage()); throw new SQLException(); } logger.info(" Create database connection successfully. "); } } /** * Create and return a new database connection. * * @return a new database connection. */ private Connection newConnection() throws SQLException { Connection conn = DriverManager.getConnection(url, username, password); if (connections.size() == 0) { DatabaseMetaData metaData = conn.getMetaData(); int driverMaxConnections = metaData.getMaxConnections(); if (driverMaxConnections > 0 && MAX_CONNECTION > driverMaxConnections) { //TODO; update the max connection. } } return conn; } /** * Return a avaliable connection. * Wait a while and retrive again if not free connection and the connection * number is max. * * @return a avaliable connection. */ public synchronized Connection getConnection() throws SQLException { if (connections == null) return null; Connection conn = getFreeConnection(); while (conn == null) { wait(250); conn = getFreeConnection(); } return conn; } /** * Retrive an avaliable connection, create some if no avaliable. * If all unavaliable after creation, return null * @return a free connection. */ private Connection getFreeConnection() throws SQLException { Connection conn = findFreeConnection(); if (conn == null) { createConnections(ADD_CONNECTION); conn = findFreeConnection(); if (conn == null) return null; } return conn; } /** * Find a free connection, * if no free connection, return null. * * @return a free connection */ private Connection findFreeConnection() throws SQLException { Connection conn = null; PooledConnection pConn = null; Enumeration<PooledConnection> enumerate = connections.elements(); while (enumerate.hasMoreElements()) { pConn = enumerate.nextElement(); if (!pConn.isBusy()) { conn = pConn.getConnection(); pConn.setBusy(true); if (!testConnection(conn)) { try { conn = newConnection(); } catch (SQLException e) { logger.error("Create database connection failed." + e.getMessage()); return null; } pConn.setConnection(conn); } break; } } return conn; } /** * Testing if a connection works, if not, close it and return false. * else return true. * * @param conn connection need to test. * @return true if connection works. */ private boolean testConnection(Connection conn) { try { if (testTable.equals("")) { conn.setAutoCommit(true); } else { Statement stmt = conn.createStatement(); stmt.execute("select count(*) from " + testTable); stmt.close(); } } catch (SQLException e) { closeConnection(conn); return false; } return true; } /** * Return a connection to pool, set it free。 * * @param free connection. */ public void returnConnection(Connection conn) { if (connections == null) { logger.warn("Connection pool does not exist, could not return connection."); return; } PooledConnection pConn = null; Enumeration<PooledConnection> enumerate = connections.elements(); while (enumerate.hasMoreElements()) { pConn = (PooledConnection) enumerate.nextElement(); if (conn == pConn.getConnection()) { pConn.setBusy(false); conn = null; break; } } } /** * Refresh all connections in the pool. * */ public synchronized void refreshConnections() throws SQLException { if (connections == null) { logger.warn("Connection pool does not exist, could not refresh connection."); return; } PooledConnection pConn = null; Enumeration<PooledConnection> enumerate = connections.elements(); while (enumerate.hasMoreElements()) { pConn = (PooledConnection) enumerate.nextElement(); if (pConn.isBusy()) { wait(5000); } closeConnection(pConn.getConnection()); pConn.setConnection(newConnection()); pConn.setBusy(false); } } /** * Close all connection and clear pool. */ public synchronized void closeConnectionPool() throws SQLException { if (connections == null) { logger.warn("Connection pool does not exist, cound not close."); return; } PooledConnection pConn = null; Enumeration<PooledConnection> enumerate = connections.elements(); while (enumerate.hasMoreElements()) { pConn = (PooledConnection) enumerate.nextElement(); if (pConn.isBusy()) { wait(5000); } closeConnection(pConn.getConnection()); connections.removeElement(pConn); } connections = null; } /** * Close a connection. * * @param the connection need to be closed. */ private void closeConnection(Connection conn) { try { conn.close(); } catch (SQLException e) { logger.error("Close connection failed:" + e.getMessage()); } } /** * Let proccess wait. * * @param ms time. */ private void wait(int mSeconds) { try { Thread.sleep(mSeconds); } catch (InterruptedException e) { } } /** * Inner class for saving database connection. * Field connection * Filed busy */ class PooledConnection { Connection connection = null;// database connection. boolean busy = false; // if this connection is in using, default is used. public PooledConnection(Connection connection) { this.connection = connection; } // Return the connection. public Connection getConnection() { return connection; } // Set the connection. public void setConnection(Connection connection) { this.connection = connection; } // Return if connection is busy. public boolean isBusy() { return busy; } // Set the connection is busy. public void setBusy(boolean busy) { this.busy = busy; } } }
发表评论
-
大数据下的实体解析
2016-07-07 12:03 671大数据时代的实体解析困境 <!--[if !sup ... -
中文相似度匹配算法
2015-12-30 14:44 1921基于音形码的中文字 ... -
简单工作流引擎
2012-07-06 16:58 2352从公司的一个项目中挖出来的工作流引擎的代码,虽然是一个很简单的 ... -
在线词典的数据结构实现。
2012-05-18 08:37 0昨天在网上看到了一道百度的面试题: Baidu写道 ... -
Log4j 代码学习 - Factory
2012-05-17 08:47 1081我们最早提到,Log4j的初始代码在LogManager的静态 ... -
Log4j 代码学习 - Appender
2012-05-16 09:09 1315在上一篇文章里,我们 ... -
Log4j 代码学习
2012-05-15 14:58 1130最近闲来无事,正好手头上有Log4j的代码,于是就拿来学习了下 ... -
java7中的ThreadLocalRandom(转)
2012-01-20 09:08 4302今天早上看到一个关于java7中的ThreadLocalRan ... -
(转)追MM与23种设计模式
2011-11-16 14:13 9661、FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德 ... -
项目开发过程 - 转载
2010-08-04 13:35 1035一位软件工程师的软件过程总结 ... -
LAMP网站架构(转载)
2010-06-30 09:04 866LAMP(Linux-Apache-MySQL-PHP)网 ... -
个人发展成长路线
2010-06-28 14:25 1153其实对自己的发展路线,一直存在着很多困惑,究竟应该成为什么样的 ... -
Decorator
2010-03-15 16:35 1115看java与模式中装饰器模式,没有看懂。。。 在网上找了一篇 ... -
关于WAS LDAP验证页面跳转问题
2009-12-03 09:50 3345很多门户网站的用户验证都是通过服务器提供的验证方式来实现的, ... -
设计模式解读之一: 策略模式
2009-10-13 00:03 752转载 当我们掌握了Java的语法,当我们了解了面向对象的封装 ... -
贫血模式or领域模式
2009-09-13 10:53 4084在论坛看到一个好帖: ... -
整体设计
2009-09-10 14:46 745目前的思路是这样的: -
DaoFactory的设计
2009-09-10 14:29 2146本打算在网上找一些资源的,可是发现大多数人都是用的hibern ... -
Junit 的模式
2009-09-08 23:32 789Junit的Composite模式, 跟享元模式很接近: ... -
IBaseDao的设计
2009-09-08 09:30 1336去网上查了下,发现泛型Dao的设计比较热门,比较起来也相对通用 ...
相关推荐
对于多应用共享同一数据库的系统而言,可在应用层通过数据库连接的配置,实现数据库连接池技术。某一应用最大可用数据库连接数的限制,避免某一应用独占所有数据库资源。 在较为完备的数据库连接池实现中,可根据...
* 数据库连接池特点: * 获取连接时不需要了解连接的名字,连接池内部维护连接的名字 * 支持多线程,保证获取到的连接一定是没有被其他线程正在使用 * 按需创建连接,可以创建多个连接,可以控制连接的数量 * 连接...
C# 数据库连接池 C# 数据库连接池 C# 数据库连接池 C# 数据库连接池
context.xml, 数据库连接池配置文
C#高效数据库连接池源码
达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,达梦7数据库连接池,jdbcDriver,
这个文档详细讲述了mysql数据库连接池的配置以及数据库连接池的工作原理。
数据库连接池的例子.doc数据库连接池的例子.doc数据库连接池的例子.doc
Druid为监控而生的数据库连接池,它是阿里巴巴开源平台上的一个项目。Druid是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能.它可以替换DBCP和C3P0连接池。Druid提供了一个高效、功能强大、可...
提供了数据库连接池的驱动,分别有c3p0、druid、dbcp三种数据库连接池的驱动
Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置Tomcat5的数据库连接池配置...
比较实用的几种数据库连接池详细配置,值得收藏
数据库连接池 数据库连接池 数据库连接池 数据库连接池
c#语言下,使用netcore2.1框架,调用netstandard数据库连接池组件
包含了连接数据库连接池所需的大部分jar包
数据库连接池DBCPjar包,数据库连接时.我们要是用到了数据库连接池,你需要的
java开发,数据库连接池c3p0所需jar包。
JAVA 使用数据库连接池连接Oracle数据库,全代码,附加详细说明
数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接, 当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。 我们可以通过设定连接池最大连接数...
数据库连接池的基础学习,针对mysql数据库的数据库连接池在java中的具体实现及应用配置