数据库连接池问题
数据库连接池怎样写给,请教以下高手,给个例子
[解决办法]
有必要自己写吗?开源的已经够用,一般应用服务器也提供数据库连接池
[解决办法]
用应用服务器提供的数据库连接池
[解决办法]
用现成的就ok了。
[解决办法]
用应用服务器提供的数据库连接池
开源的框架可以用C3P0
到google搜一下,现成的例子多的一大把
对于个人来说,明白连接池的原理是最重要的
[解决办法]
贴个简单的连接池程序,你参考一下吧
package common;
import java.sql.*;
import java.lang.reflect.*;
import java.util.*;
import java.io.*;
/**
* <p> Title: 简单的数据库连接池程序 </p>
*
* <p> Description: 实现简单的数据库连接池的管理功能 </p>
*
* <p> Copyright: Copyright (c) 2005 </p>
*
* <p> Company: EagleInfo </p>
*
* @author feng_mingjie
* @version 1.0
*/
public class SimpleConnetionPool {
private static LinkedList m_notUsedConnection = new LinkedList();
private static HashSet m_usedUsedConnection = new HashSet();
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 hours
static {
initDriver();
}
private SimpleConnetionPool() {
}
private static void initDriver() {
Driver driver = null;
//load oracle driver
try {
driver = (Driver) Class.forName( "oracle.jdbc.driver.OracleDriver ").newInstance();
installDriver(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
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();
}
[解决办法]
http://community.csdn.net/Expert/topic/5057/5057319.xml?temp=.9077265
http://community.csdn.net/Expert/topic/5082/5082758.xml?temp=.3046686
连接池有很多,帮你找了2个连接.希望对你有帮助.
我也是在学习中..
[解决办法]
推荐 Apache 的 DBCP 连接池,是一个成熟的开原产品,效率很高。
[解决办法]
mark