1. 引言
近年来,随着internet/intranet建网技术的飞速发展和在世界范围内的迅速普及,计算机
应用程序已从传统的桌面应用转到web应用。基于b/s(browser/server)架构的3层开发模式逐渐取代c/s(client/server)架构的开发模式,成为开发企业级应用和电子商务普遍采用的技术。在web应用开发的早期,主要使用的技术是cgi﹑asp﹑php等。之后,sun公司推出了基于java语言的servlet+jsp+javabean技术。相比传统的开发技术,它具有跨平台﹑安全﹑有效﹑可移植等特性,这使其更便于使用和开发。
java应用程序访问数据库的基本原理
在java语言中,jdbc(java database connection)是应用程序与数据库沟通的桥梁
即java语言通过jdbc技术访问数据库。jdbc是一种“开放”的方案,它为数据库应用开发人员﹑数据库前台工具开发人员提供了一种标准的应用程序设计接口,使开发人员可以用纯java语言编写完整的数据库应用程序。jdbc提供两种api,分别是面向开发人员的api和面向底层的jdbc驱动程序api,底层主要通过直接的jdbc驱动和jdbc-odbc桥驱动实现与数据库的连接。
一般来说,java应用程序访问数据库的过程(如图1所示)是:
①装载数据库驱动程序;
②通过jdbc建立数据库连接;
③访问数据库,执行sql语句;
④断开数据库连接。
图1 java数据库访问机制
jdbc作为一种数据库访问技术,具有简单易用的优点。但使用这种模式进行web应用
程序开发,存在很多问题:首先,每一次web请求都要建立一次数据库连接。建立连接是一个费时的活动,每次都得花费0.05s~1s的时间,而且系统还要分配内存资源。这个时间对于一次或几次数据库操作,或许感觉不出系统有多大的开销。可是对于现在的web应用,尤其是大型电子商务网站,同时有几百人甚至几千人在线是很正常的事。在这种情况下,频繁的进行数据库连接操作势必占用很多的系统资源,网站的响应速度必定下降,严重的甚至会造成服务器的崩溃。不是危言耸听,这就是制约某些电子商务网站发展的技术瓶颈问题。其次,对于每一次数据库连接,使用完后都得断开。否则,如果程序出现异常而未能关闭,将会导致数据库系统中的内存泄漏,最终将不得不重启数据库。还有,这种开发不能控制被创建的连接对象数,系统资源会被毫无顾及的分配出去,如连接过多,也可能导致内存泄漏,服务器崩溃。
数据库连接池(connection pool)的工作原理
1、基本概念及原理
由上面的分析可以看出,问题的根源就在于对数据库连接资源的低效管理。我们知道,
对于共享资源,有一个很著名的设计模式:资源池(resource pool)。该模式正是为了解决资源的频繁分配﹑释放所造成的问题。为解决上述问题,可以采用数据库连接池技术。数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。我们可以通过设定连接池最大连接数来防止系统无尽的与数据库连接。更为重要的是我们可以通过连接池的管理机制监视数据库的连接的数量﹑使用情况,为系统开发﹑测试及性能调整提供依据。连接池的基本工作原理见下图2。
图2 连接池的基本工作原理
2、服务器自带的连接池
jdbc的api中没有提供连接池的方法。一些大型的web应用服务器如bea的weblogic和ibm的websphere等提供了连接池的机制,但是必须有其第三方的专用类方法支持连接池的用法。
连接池关键问题分析
1、并发问题
为了使连接管理服务具有最大的通用性,必须考虑多线程环境,即并发问题。这个问题相对比较好解决,因为java语言自身提供了对并发管理的支持,使用synchronized关键字即可确保线程是同步的。使用方法为直接在类方法前面加上synchronized关键字,如:
public synchronized connection getconnection()
2、多数据库服务器和多用户
对于大型的企业级应用,常常需要同时连接不同的数据库(如连接oracle和sybase)。如何连接不同的数据库呢?我们采用的策略是:设计一个符合单例模式的连接池管理类,在连接池管理类的唯一实例被创建时读取一个资源文件,其中资源文件中存放着多个数据库的url地址(< poolname.url> )﹑用户名(< poolname.user> )﹑密码(< poolname.password> )等信息。如tx.url=192.168.1.123:5000/tx_it,tx.user=cyl,tx.password=123456。根据资源文件提供的信息,创建多个连接池类的实例,每一个实例都是一个特定数据库的连接池。连接池管理类实例为每个连接池实例取一个名字,通过不同的名字来管理不同的连接池。
对于同一个数据库有多个用户使用不同的名称和密码访问的情况,也可以通过资源文件处理,即在资源文件中设置多个具有相同url地址,但具有不同用户名和密码的数据库连接信息。
3、事务处理
我们知道,事务具有原子性,此时要求对数据库的操作符合“all-all-nothing”原则 即对于一组sql语句要么全做,要么全不做。
在java语言中,connection类本身提供了对事务的支持,可以通过设置connection的autocommit属性为false 然后显式的调用commit或rollback方法来实现。但要高效的进行connection复用,就必须提供相应的事务支持机制。可采用每一个事务独占一个连接来实现,这种方法可以大大降低事务管理的复杂性。
4、连接池的分配与释放
连接池的分配与释放,对系统的性能有很大的影响。合理的分配与释放,可以提高连接的复用度,从而降低建立新连接的开销,同时还可以加快用户的访问速度。
对于连接的管理可使用空闲池。即把已经创建但尚未分配出去的连接按创建时间存放到一个空闲池中。每当用户请求一个连接时,系统首先检查空闲池内有没有空闲连接。如果有就把建立时间最长(通过容器的顺序存放实现)的那个连接分配给他(实际是先做连接是否有效的判断,如果可用就分配给用户,如不可用就把这个连接从空闲池删掉,重新检测空闲池是否还有连接);如果没有则检查当前所开连接池是否达到连接池所允许的最大连接数(maxconn) 如果没有达到,就新建一个连接,如果已经达到,就等待一定的时间(timeout)。如果在等待的时间内有连接被释放出来就可以把这个连接分配给等待的用户,如果等待时间超过预定时间timeout 则返回空值(null)。系统对已经分配出去正在使用的连接只做计数,当使用完后再返还给空闲池。对于空闲连接的状态,可开辟专门的线程定时检测,这样会花费一定的系统开销,但可以保证较快的响应速度。也可采取不开辟专门线程,只是在分配前检测的方法。
5、连接池的配置与维护
连接池中到底应该放置多少连接,才能使系统的性能最佳?系统可采取设置最小连接数(minconn)和最大连接数(maxconn)来控制连接池中的连接。最小连接数是系统启动时连接池所创建的连接数。如果创建过多,则系统启动就慢,但创建后系统的响应速度会很快;如果创建过少,则系统启动的很快,响应起来却慢。这样,可以在开发时,设置较小的最小连接数,开发起来会快,而在系统实际使用时设置较大的,因为这样对访问客户来说速度会快些。最大连接数是连接池中允许连接的最大数目,具体设置多少,要看系统的访问量,可通过反复测试,找到最佳点。
如何确保连接池中的最小连接数呢?有动态和静态两种策略。动态即每隔一定时间就对连接池进行检测,如果发现连接数量小于最小连接数,则补充相应数量的新连接 以保证连接池的正常运转。静态是发现空闲连接不够时再去检查。
连接池的实现
1、连接池模型
本文讨论的连接池包括一个连接池类(dbconnectionpool)和一个连接池管理类(dbconnetionpoolmanager)和一个配置文件操作类(parsedsconfig)。连接池类是对某一数据库所有连接的“缓冲池”,主要实现以下功能:①从连接池获取或创建可用连接;②使用完毕之后,把连接返还给连接池;③在系统关闭前,断开所有连接并释放连接占用的系统资源;④还能够处理无效连接(原来登记为可用的连接,由于某种原因不再可用,如超时,通讯问题),并能够限制连接池中的连接总数不低于某个预定值和不超过某个预定值。(5)当多数据库时 且数据库是动态增加的话 将会加到配置文件中。
连接池管理类是连接池类的外覆类(wrapper) 符合单例模式,即系统中只能有一个连接池管理类的实例。其主要用于对多个连接池对象的管理,具有以下功能:①装载并注册特定数据库的jdbc驱动程序;②根据属性文件给定的信息,创建连接池对象;③为方便管理多个连接池对象,为每一个连接池对象取一个名字,实现连接池名字与其实例之间的映射;④跟踪客户使用连接情况,以便需要是关闭连接释放资源。连接池管理类的引入主要是为了方便对多个连接池的使用和管理,如系统需要连接不同的数据库,或连接相同的数据库但由于安全性问题,需要不同的用户使用不同的名称和密码。
2、连接池实现(经过本人改版 可以适用多数据库类型的应用以及一种数据库类型多个数据库且数据 库的数量可以动态增加的应用程序)
1) dbconnectionpool.java 数据库连接池类
2) dbconnectionmanager .java 数据库管理类
3) dsconfigbean .java 单个数据库连接信息bean
4) parsedsconfig.java 操作多(这个' 多' 包括不同的数据库和同一种数据库有多个数据库)
数据 配置文件xml
5) ds.config.xml 数据库配置文件xml
原代码如下:
dbconnectionpool.java
----------------------------------------------------------
/
数据库连接池类
/
package com.chunkyo.db
import java.sql.connection
import java.sql.drivermanager
import java.sql.sqlexception
import java.util.arraylist
import java.util.iterator
import java.util.timer
/
author chenyanlin
/
public class dbconnectionpool implements timerlistener {
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 //定时
/
/
public dbconnectionpool() {
// todo auto-generated constructor stub
}
/
创建连接池
param driver
param name
param url
param user
param password
param maxconn
/
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.password=password
this.maxconn=maxconn
}
/
用完,释放连接
param con
/
public synchronized void freeconnection(connection con)
{
this.freeconnections.add(con) //添加到空闲连接的末尾
this.inused--
}
/
timeout 根据timeout得到连接
param timeout
return
/
public synchronized connection getconnection(long timeout)
{
connection con=null
if(this.freeconnections.size()> 0)
{
con=(connection)this.freeconnections.get(0)
if(con==null)con=getconnection(timeout) //继续获得连接
}
else
{
con=newconnection() //新建连接
}
if(this.maxconn==0||this.maxconn< this.inused)
{
con=null //达到最大连接数,暂时不能获得连接了。
}
if(con!=null)
{
this.inused++
}
return con
}
/
从连接池里得到连接
return
/
public synchronized connection getconnection()
{
connection con=null
if(this.freeconnections.size()> 0)
{
con=(connection)this.freeconnections.get(0)
this.freeconnections.remove(0) //如果连接分配出去了,就从空闲连接里删除
if(con==null)con=getconnection() //继续获得连接
}
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.freeconnections.iterator()
while(allconns.hasnext())
{
connection con=(connection)allconns.next()
try
{
con.close()
}
catch(sqlexception e)
{
e.printstacktrace()
}
}
this.freeconnections.clear()
}
/
创建新连接
return
/
private connection newconnection()
{
try {
class.forname(driver)
con=drivermanager.getconnection(url user password)
} catch (classnotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
system.out.println(" sorry can' t find db driver!" )
} catch (sqlexception e1) {
// todo auto-generated catch block
e1.printstacktrace()
system.out.println(" sorry can' t create connection!" )
}
return con
}
/
定时处理函数
/
public synchronized void timerevent()
{
//暂时还没有实现以后会加上的
}
/
param args
/
public static void main(string[] args) {
// todo auto-generated method stub
}
/
return the driver
/
public string getdriver() {
return driver
}
/
param driver the driver to set
/
public void setdriver(string driver) {
this.driver = driver
}
/
return the maxconn
/
public int getmaxconn() {
return maxconn
}
/
param maxconn the maxconn to set
/
public void setmaxconn(int maxconn) {
this.maxconn = maxconn
}
/
return the minconn
/
public int getminconn() {
return minconn
}
/
param minconn the minconn to set
/
public void setminconn(int minconn) {
this.minconn = minconn
}
/
return the name
/
public string getname() {
return name
}
/
param name the name to set
/
public void setname(string name) {
this.name = name
}
/
return the password
/
public string getpassword() {
return password
}
/
param password the password to set
/
public void setpassword(string password) {
this.password = password
}
/
return the url
/
public string geturl() {
return url
}
/
param url the url to set
/
public void seturl(string url) {
this.url = url
}
/
return the user
/
public string getuser() {
return user
}
/
param user the user to set
/
public void setuser(string user) {
this.user = user
}
}
-------------------------------------------
dbconnectionmanager .java
------------------------------------------
/
数据库连接池管理类
/
package com.chunkyo.db
import java.sql.connection
import java.util.arraylist
import java.util.enumeration
import java.util.hashmap
import java.util.hashtable
import java.util.iterator
import java.util.properties
import java.util.vector
import com.chunkyo.db.parsedsconfig
import com.chunkyo.db.dsconfigbean
import com.chunkyo.db.dbconnectionpool
/
author chenyanlin
/
public class dbconnectionmanager {
static private dbconnectionmanager instance //唯一数据库连接池管理实例类
static private int clients //客户连接数
private vector drivers = new vector() //驱动信息
private hashtable pools=new hashtable() //连接池
/
实例化管理类
/
public dbconnectionmanager() {
// todo auto-generated constructor stub
this.init()
}
/
得到唯一实例管理类
return
/
static synchronized public dbconnectionmanager getinstance()
{
if(instance==null)
{
instance=new dbconnectionmanager()
}
return instance
}
/
释放连接
param name
param con
/
public void freeconnection(string name connection con)
{
dbconnectionpool pool=(dbconnectionpool)pools.get(name) //根据关键名字得到连接池
if(pool!=null)
pool.freeconnection(con) //释放连接
}
/
得到一个连接根据连接池的名字name
param name
return
/
public connection getconnection(string name)
{
dbconnectionpool pool=null
connection con=null
pool=(dbconnectionpool)pools.get(name) //从名字中获取连接池
con=pool.getconnection() //从选定的连接池中获得连接
if(con!=null)
system.out.println(" 得到连接。。。" )
return con
}
/
得到一个连接,根据连接池的名字和等待时间
param name
param time
return
/
public connection getconnection(string name long timeout)
{
dbconnectionpool pool=null
connection con=null
pool=(dbconnectionpool)pools.get(name) //从名字中获取连接池
con=pool.getconnection(timeout) //从选定的连接池中获得连接
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()
}
/
创建连接池
param props
/
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.getpassword())
dbpool.setmaxconn(dsb.getmaxconn())
system.out.println(" ioio:" +dsb.getmaxconn())
pools.put(dsb.getname() dbpool)
}
/
初始化连接池的参数
/
private void init()
{
//加载驱动程序
this.loaddrivers()
//创建连接池
iterator alldriver=drivers.iterator()
while(alldriver.hasnext())
{
this.createpools((dsconfigbean)alldriver.next())
system.out.println(" 创建连接池。。。" )
}
system.out.println(" 创建连接池完毕。。。" )
}
/
加载驱动程序
param props
/
private void loaddrivers()
{
parsedsconfig pd=new parsedsconfig()
//读取数据库配置文件
drivers=pd.readconfiginfo(" ds.config.xml" )
system.out.println(" 加载驱动程序。。。" )
}
/
param args
/
public static void main(string[] args) {
// todo auto-generated method stub
}
}
----------------------------------------
dsconfigbean.java
----------------------------------------
/
配置文件bean类
/
package com.chunkyo.db
/
author chenyanlin
/
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
}
/
param args
/
public static void main(string[] args) {
// todo auto-generated method stub
}
/
return the driver
/
public string getdriver() {
return driver
}
/
param driver the driver to set
/
public void setdriver(string driver) {
this.driver = driver
}
/
return the maxconn
/
public int getmaxconn() {
return maxconn
}
/
param maxconn the maxconn to set
/
public void setmaxconn(int maxconn) {
this.maxconn = maxconn
}
/
return the name
/
public string getname() {
return name
}
/
param name the name to set
/
public void setname(string name) {
this.name = name
}
/
return the password
/
public string getpassword() {
return password
}
/
param password the password to set
/
public void setpassword(string password) {
this.password = password
}
/
return the type
/
public string gettype() {
return type
}
/
param type the type to set
/
public void settype(string type) {
this.type = type
}
/
return the url
/
public string geturl() {
return url
}
/
param url the url to set
/
public void seturl(string url) {
this.url = url
}
/
return the username
/
public string getusername() {
return username
}
/
param username the username to set
/
public void setusername(string username) {
this.username = username
}
}
-----------------------------------------------------
parsedsconfig.java
-----------------------------------------------------
/
操作配置文件类 读 写 修改 删除等操作
/
package com.chunkyo.db
import java.io.fileinputstream
import java.io.filenotfoundexception
import java.io.fileoutputstream
import java.io.ioexception
import java.io.inputstream
import java.util.list
import java.util.vector
import java.util.iterator
import org.jdom.document
import org.jdom.element
import org.jdom.jdomexception
import org.jdom.input.saxbuilder
import org.jdom.output.format
import org.jdom.output.xmloutputter
/
author chenyanlin
/
public class parsedsconfig {
/
构造函数
/
public parsedsconfig() {
// todo auto-generated constructor stub
}
/
读取xml配置文件
param path
return
/
public vector readconfiginfo(string path)
{
string rpath=this.getclass().getresource(" " ).getpath().substring(1)+path
vector dsconfig=null
fileinputstream fi = null
try
{
fi=new fileinputstream(rpath) //读取路径文件
dsconfig=new vector()
saxbuilder sb=new saxbuilder()
document doc=sb.build(fi)
element root=doc.getrootelement()
list pools=root.getchildren()
element pool=null
iterator allpool=pools.iterator()
while(allpool.hasnext())
{
pool=(element)allpool.next()
dsconfigbean dscbean=new dsconfigbean()
dscbean.settype(pool.getchild(" type" ).gettext())
dscbean.setname(pool.getchild(" name" ).gettext())
system.out.println(dscbean.getname())
dscbean.setdriver(pool.getchild(" driver" ).gettext())
dscbean.seturl(pool.getchild(" url" ).gettext())
dscbean.setusername(pool.getchild(" username" ).gettext())
dscbean.setpassword(pool.getchild(" password" ).gettext())
dscbean.setmaxconn(integer.parseint(pool.getchild(" maxconn" ).gettext()))
dsconfig.add(dscbean)
}
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (jdomexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
finally
{
try {
fi.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
return dsconfig
}
/
修改配置文件 没时间写 过段时间再贴上去 其实一样的
/
public void modifyconfiginfo(string path dsconfigbean dsb) throws exception
{
string rpath=this.getclass().getresource(" " ).getpath().substring(1)+path
fileinputstream fi=null //读出
fileoutputstream fo=null //写入
}
/
增加配置文件
/
public void addconfiginfo(string path dsconfigbean dsb)
{
string rpath=this.getclass().getresource(" " ).getpath().substring(1)+path
fileinputstream fi=null
fileoutputstream fo=null
try
{
fi=new fileinputstream(rpath) //读取xml流
saxbuilder sb=new saxbuilder()
document doc=sb.build(fi) //得到xml
element root=doc.getrootelement()
list pools=root.getchildren() //得到xml子树
element newpool=new element(" pool" ) //创建新连接池
element pooltype=new element(" type" ) //设置连接池类型
pooltype.settext(dsb.gettype())
newpool.addcontent(pooltype)
element poolname=new element(" name" ) //设置连接池名字
poolname.settext(dsb.getname())
newpool.addcontent(poolname)
element pooldriver=new element(" driver" ) //设置连接池驱动
pooldriver.addcontent(dsb.getdriver())
newpool.addcontent(pooldriver)
element poolurl=new element(" url" ) //设置连接池url
poolurl.settext(dsb.geturl())
newpool.addcontent(poolurl)
element poolusername=new element(" username" ) //设置连接池用户名
poolusername.settext(dsb.getusername())
newpool.addcontent(poolusername)
element poolpassword=new element(" password" ) //设置连接池密码
poolpassword.settext(dsb.getpassword())
newpool.addcontent(poolpassword)
element poolmaxconn=new element(" maxconn" ) //设置连接池最大连接
poolmaxconn.settext(string.valueof(dsb.getmaxconn()))
newpool.addcontent(poolmaxconn)
pools.add(newpool) //将child添加到root
format format = format.getprettyformat()
format.setindent(" " )
format.setencoding(" utf-8" )
xmloutputter outp = new xmloutputter(format)
fo = new fileoutputstream(rpath)
outp.output(doc fo)
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (jdomexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
finally
{
}
}
/
删除配置文件
/
public void delconfiginfo(string path string name)
{
string rpath=this.getclass().getresource(" " ).getpath().substring(1)+path
fileinputstream fi = null
fileoutputstream fo=null
try
{
fi=new fileinputstream(rpath) //读取路径文件
saxbuilder sb=new saxbuilder()
document doc=sb.build(fi)
element root=doc.getrootelement()
list pools=root.getchildren()
element pool=null
iterator allpool=pools.iterator()
while(allpool.hasnext())
{
pool=(element)allpool.next()
if(pool.getchild(" name" ).gettext().equals(name))
{
pools.remove(pool)
break
}
}
format format = format.getprettyformat()
format.setindent(" " )
format.setencoding(" utf-8" )
xmloutputter outp = new xmloutputter(format)
fo = new fileoutputstream(rpath)
outp.output(doc fo)
} catch (filenotfoundexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (jdomexception e) {
// todo auto-generated catch block
e.printstacktrace()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
finally
{
try {
fi.close()
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace()
}
}
}
/
param args
throws exception
/
public static void main(string[] args) throws exception {
// todo auto-generated method stub
parsedsconfig pd=new parsedsconfig()
string path=" ds.config.xml"
pd.readconfiginfo(path)
//pd.delconfiginfo(path " tj012006" )
dsconfigbean dsb=new dsconfigbean()
dsb.settype(" oracle" )
dsb.setname(" yyy004" )
dsb.setdriver(" org.oracle.jdbc" )
dsb.seturl(" jdbc:oracle://localhost" )
dsb.setusername(" sa" )
dsb.setpassword(" " )
dsb.setmaxconn(1000)
pd.addconfiginfo(path dsb)
pd.delconfiginfo(path " yyy001" )
}
}
--------------------------------------
ds.config.xml 配置文件
--------------------------------------
< ds-config>
< pool>
< type> mysql< /type>
< name> user< /name>
< driver> com.mysql.jdbc.driver< /driver>
< url> jdbc:mysql://localhost:3306/user< /url>
< username> sa< /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/user2< /url>
< username> sa< /username>
< password> 1234< /password>
< maxconn> 10< /maxconn>
< /pool>
< pool>
< type> sql2000< /type>
< name> books< /name>
< driver> com.microsoft.sqlserver.driver< /driver>
< url> jdbc:sqlserver://localhost:1433/books:databasename=books< /url>
< username> sa< /username>
< password> < /password>
< maxconn> 100< /maxconn>
< /pool>
< /ds-config>
3. 连接池的使用
1。connection的获得和释放
dbconnectionmanager connectionman=dbconnectionmanager .getinstance() //得到唯一实例
//得到连接
string name=" mysql" //从上下文得到你要访问的数据库的名字
connection con=connectionman.getconnection(name)
//使用
。。。。。。。
// 使用完毕
connectionman.freeconnection(name con) //释放,但并未断开连接
2。数据库连接的动态增加和连接池的动态增加
1。调用xml操作增加类
2。重新实例华连接池管理池类