数据库连接池详细说明
首先建立个池子,里面放这我们需要的链接,党我们需要链接的时候从池子里面取,取的时候先判断是否有空闲的,有就拿来用,否则检查是否全用了,如果没有全用,则新建,否则等待或者异常抛出。
假设我们要链接不同的数据库,把相关的配置写在一个xml文件,格式如下:
。xml
〈 ds—config〉
〈 pool〉
< type〉 mysql< /type>
〈 name> test< /name>
< driver〉 com。。driver〈 /driver〉
< url> jdbc:mysql://localhost:3306/test< /url>
〈 username〉 root〈 /username〉
〈 password> 123456< /password〉
〈 maxconn> 100< /maxconn>
< /pool>
< pool>
< type〉 mysql< /type>
〈 name> user2< /name>
< driver〉 com。mysql。jdbc。driver〈 /driver〉
〈 url〉 jdbc:mysql://localhost:3306/test〈 /url〉
< username> root< /username>
〈 password〉 123456〈 /password>
〈 maxconn〉 10< /maxconn>
〈 /pool〉
< /ds-config〉
然后我们建立个javabean来对应这个xml,
package com。cgogo。dbpool
public class dsconfigbean {
private string type = " ” // 数据库类型
private string name = " ” // 连接池名字
private string driver = ” " // 数据库驱动
private string url = " ” // 数据库url
private string username = " " // 用户名
private string password = ” " // 密码
private int maxconn = 0 // 最大连接数
public dsconfigbean() {
// todo auto-generated constructor stub
}
相关的set和get省略
}
接下来需要建立个池的类,来建立和释放链接
package 。dbpool
import java。sql。connection
import java。
import 。sqlexception
import
import 。iterator
import 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 // 数据库连接地址
private string driver // 驱动
private string user // 用户名
public timer timer // 定时
省略set和get
public dbconnectionpool() {
}
public dbconnectionpool(string name string driver string url
string user
数据库连接池详细说明 来自淘豆网www.taodocs.com转载请标明出处.