数据库连接池详细说明

上传人:平*** 文档编号:16334800 上传时间:2017-11-07 格式:DOC 页数:13 大小:46.25KB
返回 下载 相关 举报
数据库连接池详细说明_第1页
第1页 / 共13页
数据库连接池详细说明_第2页
第2页 / 共13页
数据库连接池详细说明_第3页
第3页 / 共13页
数据库连接池详细说明_第4页
第4页 / 共13页
数据库连接池详细说明_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《数据库连接池详细说明》由会员分享,可在线阅读,更多相关《数据库连接池详细说明(13页珍藏版)》请在金锄头文库上搜索。

1、数据库连接池详细说明首先建立个池子,里面放这我们需要的链接,党我们需要链接的时候从池子里面取,取的时候先判断是否有空闲的,有就拿来用,否则检查是否全用了,如果没有全用,则新建,否则等待或者异常抛出。假设我们要链接不同的数据库,把相关的配置写在一个 xml 文件,格式如下:ds.config.xmlmysql test com.mysql.jdbc.driver jdbc:mysql:/localhost:3306/test root 123456 100 mysql user2 com.mysql.jdbc.driver jdbc:mysql:/localhost:3306/test root

2、 123456 10 然后我们建立个 javabean 来对应这个 xml,dsconfigbean.javapackage com.cgogo.dbpool public class dsconfigbean private string type = / 数据库类型private string name = / 连接池名字private string driver = / 数据库驱动private string url = / 数据库 urlprivate string username = / 用户名private string password = / 密码private int ma

3、xconn = 0 / 最大连接数public dsconfigbean() / todo auto-generated constructor stub相关的 set 和 get 省略接下来需要建立个池的类,来建立和释放链接dbconnectionpool.javapackage com.cgogo.dbpool import java.sql.connection import java.sql.drivermanager import java.sql.sqlexception import java.util.arraylist import java.util.iterator im

4、port java.util.timer public class dbconnectionpool private connection con = null private int inused = 0 / 使用的连接数private arraylist freeconnections = new arraylist() / 容器,空闲连接private int minconn / 最小连接数private int maxconn / 最大连接 private string name / 连接池名字private string password / 密码private string url

5、 / 数据库连接地址private string driver / 驱动private string user / 用户名public timer timer / 定时省略 set 和 getpublic dbconnectionpool() public dbconnectionpool(string name string driver string url string user string password int maxconn) this.name = name this.driver = driver this.url = url this.user = user this.p

6、assword = password this.maxconn = maxconn /用完,释放连接public synchronized void freeconnection(connection con) this.freeconnections.add(con) / 添加到空闲连接的末尾this.inused- / timeout 根据 timeout 得到连接public synchronized connection getconnection(long timeout) connection con = null if (this.freeconnections.size() 0

7、) con = (connection) this.freeconnections.get(0) if (con = null) con = getconnection(timeout) / 继续获得连接 else con = newconnection() / 新建连接if (this.maxconn = 0 | this.maxconn 0) con = (connection) this.freeconnections.get(0) this.freeconnections.remove(0) / 如果连接分配出去了,就从空闲连接里删除if (con = null)con = getco

8、nnection() / 继续获得连接 else con = newconnection() / 新建连接if (this.maxconn = 0 | this.maxconn this.inused) con = null / 等待 超过最大连接时if (con != null) this.inused+ system.out.println( 得到 + this.name + 的连接,现有 + inused + 个连接在使用! ) return con / 释放全部连接public synchronized void release() iterator allconns = this.f

9、reeconnections.iterator() while (allconns.hasnext() connection con = (connection) allconns.next() try con.close() catch (sqlexception e) e.printstacktrace() this.freeconnections.clear() /创建新连接private connection newconnection() try class.forname(driver) con = drivermanager.getconnection(url user pass

10、word) catch (classnotfoundexception e) e.printstacktrace() system.out.println( sorry can t find db driver! ) catch (sqlexception e1) e1.printstacktrace() system.out.println( sorry can t create connection! ) return con 接下来有个管理连接池的类dbconnectionmanager.javapackage com.cgogo.dbpool import java.sql.conne

11、ction import java.util.enumeration import java.util.hashtable import java.util.iterator import java.util.vector public class dbconnectionmanager static private dbconnectionmanager instance / 唯一数据库连接池管理实例类static private int clients / 客户连接数private vector drivers = new vector() / 驱动信息private hashtable

12、pools = new hashtable() / 连接池/ 实例化管理类public dbconnectionmanager() this.init() / 得到唯一实例管理类static synchronized public dbconnectionmanager getinstance() if (instance = null) instance = new dbconnectionmanager() return instance / 释放连接public void freeconnection(string name connection con) dbconnectionpoo

13、l pool = (dbconnectionpool) pools.get(name) / 根据关键名字得到连接池if (pool != null)pool.freeconnection(con) / 释放连接/得到一个连接根据连接池的名字 namepublic connection getconnection(string name) dbconnectionpool pool = null connection con = null pool = (dbconnectionpool) pools.get(name) / 从名字中获取连接池con = pool.getconnection()

14、 / 从选定的连接池中获得连接if (con != null)system.out.println( 得到连接。 。 。 ) return con / 得到一个连接,根据连接池的名字和等待时间public connection getconnection(string name long timeout) dbconnectionpool pool = null connection con = null pool = (dbconnectionpool) pools.get(name) / 从名字中获取连接池con = pool.getconnection(timeout) / 从选定的连接

15、池中获得连接system.out.println( 得到连接。 。 。 ) return con / 释放所有连接public synchronized void release() enumeration allpools = pools.elements() while (allpools.hasmoreelements() dbconnectionpool pool = (dbconnectionpool) allpools.nextelement() if (pool != null)pool.release() pools.clear() / 创建连接池private void createpools(dsconfigbean dsb) dbconnectionpool dbpool = new dbconnectionpool() dbpool.setname(dsb.getname() dbpool.setdriver(dsb.getdriver() dbpool.seturl(dsb.geturl() dbpool.setuser(dsb.getusername() dbpool.setpassword(dsb.getp

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号