关于数据库连接池的一个代码

soft1999 2006-03-07 11:41:29
因为小弟对JNDI以及配置容器不熟悉.所以直接找了个池代码.

public class ConnectionPool {

//连接池的管理器,首先初始化,仅仅有一个对象,管理连接池
private static HashMap connectionPoolManager = new HashMap();
//没有用过的连接池,用vector实现同步
private static Vector noUseConnectionPool= new Vector();
//没有用过的连接池
private static HashMap nowUseConnectionPool = new HashMap();

private static String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String dbUrl = "jdbc:oracle:thin:@localhost:1521:db";
private static String userName = "yourname";
private static String userPassword = "yourpassword";

//默认为100个连接池
private static int MAX_POOL = 100;

//singleTon 设计模式
private ConnectionPool(String driver, String url, String name,
String password, int max) throws
ClassNotFoundException {
Class.forName(driver);
dbUrl = url;
userName = name;
userPassword = password;
MAX_POOL = max;
}

public static ConnectionPool getConnManagerInstance(String poolName) throws
ClassNotFoundException {
ConnectionPool tempPool = (ConnectionPool) connectionPoolManager.get(
poolName);
if (tempPool == null) {
tempPool = new ConnectionPool(dbDriver, dbUrl, userName, userPassword,
MAX_POOL);
connectionPoolManager.put(poolName, tempPool);
return tempPool;
}
else {
return tempPool;
}
}

//通过连接池获得真正的链接
public static Connection getConnection() throws java.sql.SQLException {
Connection conn = null;
synchronized (noUseConnectionPool) {
if (noUseConnectionPool.size() > 0) {
conn = (Connection) noUseConnectionPool.firstElement();
noUseConnectionPool.remove(conn);
return conn;
}
}
//如果数据库连接池没有链接了,自己创建一个
if (conn == null) {
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
else if (conn.isClosed()) {
nowUseConnectionPool.remove(conn);
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
conn.setAutoCommit(false);
nowUseConnectionPool.put(conn, conn);
return conn;
}

//如果连接池没有链接了,就需要产生一个链接
private static Connection createConnection(String driver, String url,
String user, String password) throws
java.sql.SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}

public static void releaseConnection(Connection conn, boolean isCommit) throws
java.sql.SQLException {
if (isCommit) {
conn.commit();
}
else {
conn.rollback();
}
nowUseConnectionPool.remove(conn);
if (noUseConnectionPool.size() + nowUseConnectionPool.size() < MAX_POOL) {
synchronized (noUseConnectionPool) {
noUseConnectionPool.add(conn);
}
}
else {
conn.close();
}
}
}

这是我从网上找的一段cp的代码.自己研究了下,有些地方不大明白.因为用到SINGLETON模式.所以我觉得取得Connection应该是
Connection conn=getConnManagerInstance(String poolName).getConnection();//但是这里的poolName怎么用啊??还有就是createConnection和releaseConnection什么时候用?高手方便时候能写个例子看下么??
...全文
132 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
光明唧 2006-03-27
<%@ page import="连接池的包名.连接池的类名" %>
回复
sunwatch 2006-03-27
请教一下啊
这个池代码转换为class文件之后,在jsp里怎么用它啊?
回复
soft1999 2006-03-08
搞定了
回复
soft1999 2006-03-07
createConnection知道了。就是releaseConnection还有那个poolName参数
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2006-03-07 11:41
社区公告
暂无公告