ConnectionPooljar數(shù)據(jù)連接池文件包的制作流程模板_第1頁
ConnectionPooljar數(shù)據(jù)連接池文件包的制作流程模板_第2頁
ConnectionPooljar數(shù)據(jù)連接池文件包的制作流程模板_第3頁
ConnectionPooljar數(shù)據(jù)連接池文件包的制作流程模板_第4頁
ConnectionPooljar數(shù)據(jù)連接池文件包的制作流程模板_第5頁
已閱讀5頁,還剩76頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

ConnectionPooljar數(shù)據(jù)連接池文件包的制作流程模板資料內(nèi)容僅供您學習參考,如有不當或者侵權(quán),請聯(lián)系改正或者刪除。關(guān)于ConnectionPool.jar數(shù)據(jù)連接池文件包的制作流程目的:制作通用型數(shù)據(jù)連接池文件包,便于應用于通用工程結(jié)構(gòu):工程包內(nèi)classes目錄內(nèi)1.ConnectionPool:連接池管理程序 |______ConnectionPool.java |______ConnectionWrapper.java |______LogFile.java |______LogWriter.java |______MakeDateTime.java |______PoolM:MySqlJDBC驅(qū)動程序3.org:MySqlJDBC驅(qū)動程序4.net:MSSqlserverJTDS驅(qū)動程序5.javax:OracleJDBC驅(qū)動程序6.oracle:OracleJDBC驅(qū)動程序制作流程:新建一個工程命名為Pool,在工程內(nèi)建立一個war模塊命名為Poolwar;在工程內(nèi)新建class文件,文件的包起名為ConnectionPool;依次新建下列文件:ConnectionPool.java、ConnectionWrapper.java、LogFile.java、LogWriter.java、MakeDateTime.java、PoolManager.java將OracleforJDBC驅(qū)動classes12.jar文件用winrar解壓縮,得到javax和oracle兩個文件夾,將這兩個文件夾復制到Pool工程內(nèi)的classes目錄下;將MS-SqlserverforJDBC驅(qū)動jtds-1.2.jar文件用winrar解壓縮,得到net這個文件夾,將這個文件夾復制到Pool工程內(nèi)的classes目錄下;將MySqlforJDBC驅(qū)動mysql-connector-java-3.0.16-ga-bin.jar文件用winrar解壓縮,得到com和org兩個文件夾,將這兩個文件夾復制到Pool工程內(nèi)的classes目錄下;如果需要添加其它驅(qū)動程序,可參照4-6進行;選擇JBuilder工具條上Wizards--ArchiveBuilder;9.ArchiveBuilder–Step1of5:ArchiveType選擇Basic(具體類型含義參考JBuilerX使用手冊);10.ArchiveBuilder–Step2of5:將Name和File命名為需要的名字,這里同意將其命名為ConnectionPool,其它的選項均不做更改;11.ArchiveBuilder–Step3of5:指定結(jié)構(gòu)文件包含內(nèi)容,這里不做任何修改;12.ArchiveBuilder–Step4of5:將servlet選擇為Allwaysincludeallclassesandresources;13.ArchiveBuilder–Step4of5:保持默認狀態(tài)不改變,按finish完成結(jié)構(gòu)文件的定義;14.在工程窗口用鼠標右鍵點ConnectionPool結(jié)構(gòu),然后選擇make生成結(jié)構(gòu)文件;使用方法:將該jar文件添加到ToolsConfigureLibrarise中;新加工程,在工程RequiredLibrarise中添加該庫文件;在工程中新建一個”連接池初始化類”負責建立數(shù)據(jù)連接;在工程中新建一屬性文件perties,負責描述數(shù)據(jù)庫驅(qū)動及用戶信息等,該屬性文件存放的位置在PoolManager.java中定義;在PoolManager.java中默認為”../perties”,該位置表示在/WEB-INF目錄下;附錄:文件代碼************************文件開始***************************perties//屬性文件***********************************************************XBDBManagerPropertiespoolname=sqlpooloraclepool注:中間以空格分隔drivers=net.sourceforge.jtds.jdbc.Driveroracle.jdbc.driver.OracleDriver注:中間以空格分隔logfile=d:\\logfile.txtsqlpool.url=jdbc:jtds:sqlserver://:1433;DatabaseName=pdasqlpool.initconns=50sqlpool.maxconns=80sqlpool.user=pdasqlpool.password=pdapass1234oraclepool.url=jdbc:oracle:thin:@:1521:infodboraclepool.initconns=50oraclepool.maxconns=80oraclepool.user=tjoldoraclepool.password=tjold************************文件結(jié)束***************************************************文件開始***************************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){=name;this.URL=URL;this.user=user;this.password=password;this.maxConns=maxConns;this.timeOut=timeOut>0?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;i<initConns;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(SQLExceptione){if(testStmt!=null){try{testStmt.close();}catch(SQLExceptionse){}}logWriter.log(e,"PooledConnectionwasnotokay",LogWriter.ERROR);returnfalse;}returntrue;}privateConnectiongetPooledConnection()throwsSQLException{Connectionconn=null;if(freeConnections.size()>0){//PickthefirstConnectionintheVector//togetround-robinusageconn=(Connection)freeConnections.firstElement();freeConnections.removeElementAt(0);}elseif(maxConns==0||checkedOut<maxConns){conn=newConnection();}returnconn;}privateConnectionnewConnection()throwsSQLException{Connectionconn=null;if(user==null){conn=DriverManager.getConnection(URL);}else{conn=DriverManager.getConnection(URL,user,password);}logWriter.log("Openedanewconnection",LogWriter.INFO);logWriter.log(getStats(),LogWriter.INFO);returnconn;}publicsynchronizedvoidfreeConnection(Connectionconn){//PuttheconnectionattheendoftheVectorfreeConnections.addElement(conn);checkedOut--;notifyAll();logWriter.log("ReturnedfreeConnectiontopool",LogWriter.INFO);logWriter.log(getStats(),LogWriter.INFO);}publicsynchronizedvoidrelease(){EnumerationallConnections=freeConnections.elements();while(allConnections.hasMoreElements()){Connectioncon=(Connection)allConnections.nextElement();try{con.close();logWriter.log("releaseClosedconnection",LogWriter.INFO);}catch(SQLExceptione){logWriter.log(e,"releaseCouldn'tcloseconnection",LogWriter.ERROR);}}freeConnections.removeAllElements();}privateStringgetStats(){return"Totalconnections:"+(freeConnections.size()+checkedOut)+"Available:"+freeConnections.size()+"Checked-out:"+checkedOut;}}************************文件結(jié)束***************************************************文件開始***************************ConnectionWrapper.java***********************************************************packageConnectionPool;importjava.sql.*;importjava.util.*;/***ThisclassisawrapperaroundaConnection,overridingthe*closemethodtojustinformthepoolthatit'savailablefor*reuseagain,andtheisClosedmethodtoreturnthestate*ofthewrapperinsteadoftheConnection.*/classConnectionWrapperimplementsConnection{//realConnshouldbeprivatebutweusepackagescopeto//beabletotestremovalofbadconnectionsConnectionrealConn;privateConnectionPoolpool;privatebooleanisClosed=false;publicConnectionWrapper(ConnectionrealConn,ConnectionPoolpool){this.realConn=realConn;this.pool=pool;}/***InformtheConnectionPoolthattheConnectionWrapper*isclosed.*/publicvoidclose()throwsSQLException{isClosed=true;pool.wrapperClosed(realConn);}/***ReturnstrueiftheConnectionWrapperisclosed,false*otherwise.*/publicbooleanisClosed()throwsSQLException{returnisClosed;}/**Wrappedmethods.*/publicvoidclearWarnings()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}realConn.clearWarnings();}publicvoidcommit()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}realCmit();}publicStatementcreateStatement()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.createStatement();}publicbooleangetAutoCommit()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.getAutoCommit();}publicStringgetCatalog()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.getCatalog();}publicDatabaseMetaDatagetMetaData()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.getMetaData();}publicintgetTransactionIsolation()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.getTransactionIsolation();}publicSQLWarninggetWarnings()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.getWarnings();}publicbooleanisReadOnly()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.isReadOnly();}publicStringnativeSQL(Stringsql)throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.nativeSQL(sql);}publicCallableStatementprepareCall(Stringsql)throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.prepareCall(sql);}publicPreparedStatementprepareStatement(Stringsql)throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}returnrealConn.prepareStatement(sql);}publicvoidrollback()throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}realConn.rollback();}publicvoidsetAutoCommit(booleanautoCommit)throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}realConn.setAutoCommit(autoCommit);}publicvoidsetCatalog(Stringcatalog)throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}realConn.setCatalog(catalog);}publicvoidsetReadOnly(booleanreadOnly)throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}realConn.setReadOnly(readOnly);}publicvoidsetTransactionIsolation(intlevel)throwsSQLException{if(isClosed){thrownewSQLException("Pooledconnectionisclosed");}realConn.setTransactionIsolation(level);}publicvoidsetHoldability(intholdability)throwsSQLException{realConn.setHoldability(holdability);}publicintgetHoldability()throwsSQLException{returnrealConn.getHoldability();}publicSavepointsetSavepoint()throwsSQLException{returnrealConn.setSavepoint();}publicSavepointsetSavepoint(Stringname)throwsSQLException{returnrealConn.setSavepoint(name);}publicvoidreleaseSavepoint(Savepointsavepoint)throwsSQLException{realConn.releaseSavepoint(savepoint);}publicvoidrollback(Savepointsavepoint)throwsSQLException{realConn.rollback(savepoint);}publicStatementcreateStatement(intresultSetType,intresultSetConcurrency,intresultSetHoldability)throwsSQLException{returnrealConn.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);}publicPreparedStatementprepareStatement(Stringsql,intresultSetType,intresultSetConcurrency,intresultSetHoldability)throwsSQLException{returnrealConn.prepareStatement(sql,resultSetConcurrency,resultSetHoldability);}publicCallableStatementprepareCall(Stringsql,intresultSetType,intresultSetConcurrency,intresultSetHoldability)throwsSQLException{returnrealConn.prepareCall(sql,resultSetConcurrency,resultSetHoldability);}publicPreparedStatementprepareStatement(Stringsql,intautoGeneratedKeys)throwsSQLException{returnrealConn.prepareStatement(sql,autoGeneratedKeys);}publicPreparedStatementprepareStatement(Stringsql,int[]columnIndexes)throwsSQLException{returnrealConn.prepareStatement(sql,columnIndexes);}publicPreparedStatementprepareStatement(Stringsql,String[]columnNames)throwsSQLException{returnrealConn.prepareStatement(sql,columnNames);}publicStatementcreateStatement(intresultSetType,intresultSetConcurrency)throwsSQLException{returnrealConn.createStatement(resultSetType,resultSetConcurrency);}publicPreparedStatementprepareStatement(Stringsql,intresultSetType,intresultSetConcurrency)throwsSQLException{returnrealConn.prepareStatement(sql,resultSetType,resultSetConcurrency);}publicCallableStatementprepareCall(Stringsql,intresultSetType,intresultSetConcurrency)throwsSQLException{returnrealConn.prepareCall(sql,resultSetType,resultSetConcurrency);}publicMapgetTypeMap()throwsSQLException{returnrealConn.getTypeMap();}publicvoidsetTypeMap(Mapmap)throwsSQLException{realConn.setTypeMap(map);}}************************文件結(jié)束***************************************************文件開始***************************LogFile.java***********************************************************packageConnectionPool;importjava.io.*;importjava.util.*;publicclassLogFile{privatestaticStringmyDir="c:\\test\\";privatestaticStringmyFile="_tbit.log";publicvoidLogFile(){}publicstaticvoidlog(Strings){try{MakeDateTimeqdt=newMakeDateTime();StringdateString=qdt.getTDashDate1();Stringstemp=qdt.getDate8();Stringfilename=myDir+"HK"+stemp+".log";BufferedWriterout=newBufferedWriter(newFileWriter(filename,true));out.write(dateString+":"+s);out.write("\n");out.close();}catch(Exceptione){};}publicstaticvoidlog(String_dir,Strings){try{MakeDateTimeqdt=newMakeDateTime();StringdateString=qdt.getTDashDate1();Stringstemp=qdt.getDate8();Stringfilename=_dir;BufferedWriterout=newBufferedWriter(newFileWriter(filename,true));out.write(dateString+":"+s);out.write("\n");out.close();}catch(Exceptione){};}}************************文件結(jié)束***************************************************文件開始***************************LogWriter.java***********************************************************packageConnectionPool;importjava.io.*;importjava.util.*;publicclassLogWriter{publicstaticfinalintNONE=0;publicstaticfinalintERROR=1;publicstaticfinalintINFO=2;publicstaticfinalintDEBUG=3;privatestaticfinalStringERROR_TEXT="error";privatestaticfinalStringINFO_TEXT="info";privatestaticfinalStringDEBUG_TEXT="debug";privatePrintWriterpw;privateStringowner;privateintlogLevel;publicLogWriter(Stringowner,intlogLevel,PrintWriterpw){this.pw=pw;this.owner=owner;this.logLevel=logLevel;}publicLogWriter(Stringowner,intlogLevel){this(owner,logLevel,null);}publicintgetLogLevel(){returnlogLevel;}publicPrintWritergetPrintWriter(){returnpw;}publicvoidsetLogLevel(intlogLevel){this.logLevel=logLevel;}publicvoidsetPrintWriter(PrintWriterpw){this.pw=pw;}publicvoidlog(Stringmsg,intseverityLevel){if(pw!=null){if(severityLevel<=logLevel){pw.println("["+newDate()+"]"+getSeverityString(severityLevel)+":"+owner+":"+msg);}}}publicvoidlog(Throwablet,Stringmsg,intseverityLevel){log(msg+":"+toTrace(t),severityLevel);}privateStringgetSeverityString(intseverityLevel){switch(severityLevel){caseERROR:returnERROR_TEXT;caseINFO:returnINFO_TEXT;caseDEBUG:returnDEBUG_TEXT;default:return"Unknown";}}privateStringtoTrace(Throwablee){StringWritersw=newStringWriter();PrintWriterpw=newPrintWriter(sw);e.printStackTrace(pw);pw.flush();returnsw.toString();}}************************文件結(jié)束***************************************************文件開始***************************MakeDateTime.java***********************************************************packageConnectionPool;importjava.io.*;importjava.util.*;importjava.lang.*;importjava.text.*;publicclassMakeDateTime{privateStringqDate;privateStringqDashDate;privateStringqDotDate;privateStringqTime;privateStringqTimeSSS;privateStringqDate17;privateStringqTDate0;privateStringqTDate1;privateStringqTDashDate0;privateStringqTDashDate1;privateStringqTDotDate0;privateStringqTDotDate1;privateStringqDate14;privateStringqDate8;privateStringqTime9;privateStringqTime6;privateStringqYear;privateStringqMonth;privateStringqDay;privateStringqHH;privateStringqMM;privateStringqSS;privateStringqSSS;privateStringqrandom;publicMakeDateTime(){Datedate=newDate();GregorianCalendarcalendar=newGregorianCalendar();calendar.setTime(date);Localelocale=newLocale("kr","KR");SimpleTimeZonetimeZone=newSimpleTimeZone(32400000,"KST");SimpleDateFormatdFormat0=newSimpleDateFormat("yyyy/MM/dd",locale);dFormat0.setTimeZone(timeZone);SimpleDateFormatdFormat1=newSimpleDateFormat("yyyy-MM-dd",locale);dFormat1.setTimeZone(timeZone);SimpleDateFormatdFormat2=newSimpleDateFormat("yyyy.MM.dd",locale);dFormat2.setTimeZone(timeZone);SimpleDateFormatdFormat3=newSimpleDateFormat("HH:mm:ss",locale);dFormat3.setTimeZone(timeZone);SimpleDateFormatdFormat4=newSimpleDateFormat("HH:mm:ss:SSS",locale);dFormat4.setTimeZone(timeZone);SimpleDateFormatdFormat5=newSimpleDateFormat("yyyyMMddHHmmssSSS",locale);dFormat5.setTimeZone(timeZone);StringqDate1=dFormat0.format(date);StringqDate2=dFormat1.format(date);StringqDate3=dFormat2.format(date);StringqsTime=dFormat3.format(date);StringqtTime=dFormat4.format(date);StringqsDate=dFormat5.format(date);this.qDate=qDate1;this.qDashDate=qDate2;this.qDotDate=qDate3;this.qTime=qsTime;this.qTimeSSS=qtTime;this.qDate17=qsDate;qYear=qsDate.substring(0,4);qMonth=qsDate.substring(4,6);qDay=qsDate.substring(6,8);qHH=qsDate.substring(8,10);qMM=qsDate.substring(10,12);qSS=qsDate.substring(12,14);qSSS=qsDate.substring(14,17);this.qTDate0=qDate+""+qTime;this.qTDate1=qDate+""+qTimeSSS;this.qTDashDate0=qDashDate+""+qTime;this.qTDashDate1=qDashDate+""+qTimeSSS;this.qTDotDate0=qDotDate+""+qTime;this.qTDotDate1=qDotDate+""+qTimeSSS;this.qDate14=qYear+qMonth+qDay+qHH+qMM+qSS;this.qDate8=qYear+qMonth+qDay;this.qTime9=qHH+qMM+qSS+qSSS;this.qTime6=qHH+qMM+qSS;this.qYear=qYea

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論