关于ConnectionPooljar数据连接池文件包的制作流程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

关于ConnectionPool.jar数据连接池文件包的制作流程目的:制作通用型数据连接池文件包,便于应用于通用工程结构:工程包内classes目录内1.ConnectionPool:连接池管理程序|______ConnectionPool.java|______ConnectionWrapper.java|______LogFile.java|______LogWriter.java|______MakeDateTime.java|______PoolManager.java2.com:MySqlJDBC驱动程序3.org:MySqlJDBC驱动程序4.net:MSSqlserverJTDS驱动程序5.javax:OracleJDBC驱动程序6.oracle:OracleJDBC驱动程序制作流程:1.新建一个工程命名为Pool,在工程内建立一个war模块命名为Poolwar;2.在工程内新建class文件,文件的包起名为ConnectionPool;3.依次新建下列文件:ConnectionPool.java、ConnectionWrapper.java、LogFile.java、LogWriter.java、MakeDateTime.java、PoolManager.java4.将OracleforJDBC驱动classes12.jar文件用winrar解压缩,得到javax和oracle两个文件夹,将这两个文件夹复制到Pool工程内的classes目录下;5.将MS-SqlserverforJDBC驱动jtds-1.2.jar文件用winrar解压缩,得到net这个文件夹,将这个文件夹复制到Pool工程内的classes目录下;6.将MySqlforJDBC驱动mysql-connector-java-3.0.16-ga-bin.jar文件用winrar解压缩,得到com和org两个文件夹,将这两个文件夹复制到Pool工程内的classes目录下;7.如果需要添加其他驱动程序,可参照4-6进行;8.选择JBuilder工具条上Wizards--ArchiveBuilder;9.ArchiveBuilder–Step1of5:ArchiveType选择Basic(具体类型含义参考JBuilerX使用手册);10.ArchiveBuilder–Step2of5:将Name和File命名为需要的名字,这里同意将其命名为ConnectionPool,其他的选项均不做更改;11.ArchiveBuilder–Step3of5:指定结构文件包含内容,这里不做任何修改;12.ArchiveBuilder–Step4of5:将servlet选择为Allwaysincludeallclassesandresources;13.ArchiveBuilder–Step4of5:保持默认状态不改变,按finish完成结构文件的定义;14.在工程窗口用鼠标右键点ConnectionPool结构,然后选择make生成结构文件;使用方法:1.将该jar文件添加到ToolsConfigureLibrarise中;2.新加工程,在工程RequiredLibrarise中添加该库文件;3.在工程中新建一个“连接池初始化类”负责建立数据连接;4.在工程中新建一属性文件----db.properties,负责描述数据库驱动及用户信息等,该属性文件存放的位置在PoolManager.java中定义;5.在PoolManager.java中默认为“../db.properties”,该位置表示在/WEB-INF目录下;附录:文件代码************************文件开始***************************db.properties//属性文件***********************************************************XBDBManagerPropertiespoolname=sqlpooloraclepool注:中间以空格分隔drivers=net.sourceforge.jtds.jdbc.Driveroracle.jdbc.driver.OracleDriver注:中间以空格分隔logfile=d:\\logfile.txtsqlpool.url=jdbc:jtds:sqlserver://10.1.1.2:1433;DatabaseName=pdasqlpool.initconns=50sqlpool.maxconns=80sqlpool.user=pdasqlpool.password=pdapass1234oraclepool.url=jdbc:oracle:thin:@172.168.72.8:1521:infodboraclepool.initconns=50oraclepool.maxconns=80oraclepool.user=tjoldoraclepool.password=tjold************************文件结束***************************************************文件开始***************************ConnectionPool.java***********************************************************packageConnectionPool;importjava.io.*;importjava.sql.*;importjava.util.*;publicclassConnectionPool{privateStringname;privateStringURL;privateStringuser;privateStringpassword;privateintmaxConns;privateinttimeOut;privateLogWriterlogWriter;privateintcheckedOut;privateVectorfreeConnections=newVector();publicConnectionPool(Stringname,StringURL,Stringuser,Stringpassword,intmaxConns,intinitConns,inttimeOut,PrintWriterpw,intlogLevel){this.name=name;this.URL=URL;this.user=user;this.password=password;this.maxConns=maxConns;this.timeOut=timeOut0?timeOut:5;logWriter=newLogWriter(name,logLevel,pw);initPool(initConns);logWriter.log(Newpoolcreated,LogWriter.INFO);Stringlf=System.getProperty(line.separator);logWriter.log(url=+URL+user=+user+//password=+password+initconns=+initConns+maxconns=+maxConns+logintimeout=+this.timeOut,LogWriter.INFO);logWriter.log(getStats(),LogWriter.INFO);}privatevoidinitPool(intinitConns){for(inti=0;iinitConns;i++){try{Connectionpc=newConnection();freeConnections.addElement(pc);}catch(SQLExceptione){}}}publicConnectiongetConnection()throwsSQLException{logWriter.log(Requestforconnectionreceived,LogWriter.DEBUG);try{Connectionconn=getConnection(timeOut*1000);returnnewConnectionWrapper(conn,this);}catch(SQLExceptione){logWriter.log(e,Exceptiongettingconnection,logWriter.ERROR);throwe;}}synchronizedvoidwrapperClosed(Connectionconn){freeConnections.addElement(conn);checkedOut--;notifyAll();logWriter.log(ReturnedwrapperClosedtopool,LogWriter.INFO);logWriter.log(getStats(),LogWriter.INFO);}privatesynchronizedConnectiongetConnection(longtimeout)throwsSQLException{//GetapooledConnectionfromthecacheoranewone.//Waitifallarecheckedoutandthemaxlimithas//beenreached.longstartTime=System.currentTimeMillis();longremaining=timeout;Connectionconn=null;while((conn=getPooledConnection())==null){try{logWriter.log(Waitingforconnection.Timeout=+remaining,LogWriter.DEBUG);wait(remaining);}catch(InterruptedExceptione){}remaining=timeout-(System.currentTimeMillis()-startTime);if(remaining=0){//TimeouthasexpiredlogWriter.log(Time-outwhilewaitingforconnection,LogWriter.DEBUG);thrownewSQLException(getConnection()timed-out);}}//CheckiftheConnectionisstillOKif(!isConnectionOK(conn)){//Itwasbad.TryagainwiththeremainingtimeoutlogWriter.log(Removedselectedbadconnectionfrompool,LogWriter.ERROR);returngetConnection(remaining);}checkedOut++;logWriter.log(DeliveredgetConnectionfrompool,LogWriter.INFO);logWriter.log(getStats(),LogWriter.INFO);returnconn;}privatebooleanisConnectionOK(Connectionconn){StatementtestStmt=null;try{if(!conn.isClosed()){//TrytocreateStatementtoseeifit'sreallyalivetestStmt=conn.createStatement();testStmt.close();}else{returnfalse;}}catch(SQLExcept

1 / 41
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功