关于ConnectionPooljar数据连接池文件包的制作流程_第1页
关于ConnectionPooljar数据连接池文件包的制作流程_第2页
关于ConnectionPooljar数据连接池文件包的制作流程_第3页
关于ConnectionPooljar数据连接池文件包的制作流程_第4页
关于ConnectionPooljar数据连接池文件包的制作流程_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、关于 ConnectionPooljar 数据连接池文件包的制作流程目的:制作通用型数据连接池文件包,便于应用于通用工程结构:工程包内 classes目录内1. ConnectionPool :连接池管理程序|ConnectionPool.java|ConnectionWrapper.java|LogFile.java|LogWriter.java|MakeDateTime.java|PoolManager.java2. com : MySql JDBC 驱动程序3. org : MySql JDBC 驱动程序4. net : MSSqlserver JTDS 驱动程序5. javax : O

2、racle JDBC 驱动程序6. oracle : Oracle JDBC 驱动程序制作流程:1. 新建一个工程命名为 Pool,在工程内建立一个war模块命名为Poolwar;2. 在工程内新建class文件,文件的包起名为ConnectionPoo;3. 依次新建下列文件: ConnectionPool.java、ConnectionWrapper.java、 LogFile.java、LogWriter.java、MakeDateTime.java、PoolManager.java4. 将 Oracle for JDBC 驱动 classes12.jar文件用 win rar 解压缩,

3、得到javax和oracle两个文件夹,将这两个文件夹复制到Pool工程内的classes目 录下;5. 将 MS-Sqlserver for JDBC 驱动 jtds-1.2.jar 文件用 winrar 解压缩,得到net这个文件夹,将这个文件夹复制到Pool工程内的classes目录下;6. 将 MySql for JDBC 驱动 mysql-connector-java-3.0.16-ga-bin.jar文件用 winrar 解压缩,得到 com 和 org 两个文件夹,将这两个文件夹复制到Pool工程内的classes目录下;7. 如果需要添加其他驱动程序,可参照 4-6 进行;8.

4、 选择 JBuilder 工具条上 Wizards- Archive Builder ;(3 JBuilder X - k/codjjool pooljpxFieEditSActi RfrlAdOf* VT PrJeet Ruh Tea肚 VizardsToeis曲岭HA9BriMtarB觀 Project1 Imp IrrterteOvcrf 力 Mdhqrf暫園国呻Lf? pooljpx+ Prqied Source=B3ESE3SE1GQfwcctiQnPQor.jaYa.空 CanneciiDrViflftcippei .java 盘 LjogFile.jeva F女 LoWWiter

5、 javaMMeDaielime.Jsvft撇 Poolivfanga.javai1 W pool* 帘 Conned lonPo oiF帝备;韦京片XE,JBusr:-Irtc:仍CC、Native t leGutatie Builds-;I UsetreM.cdL/&甲J 空 mYFg Strings. 专 Cactus Setup .I Archive Builder.Qj Javadoc .I ? Extern Build Ta?lr1 M,/TtEx port Io Ant 鲨import Source .9. Archive Builder -Step 1 of 5 : Archi

6、ve Type 选择 Basic (具体类型含义参考JBuilerX使用手册);Type: BsieIManie: IComectionPool10. Archive Builder -Step 2 of 5 :将 Name 和 File 命名为需要的名字,这里同意将其命名为 ConnectionPool,其他的选项均不做更改;Archive Builder - Step Z of 5Specily the tie to be created b/ the ondiiving processThe 沁屜Q will gw the 年呦门gw on this 耳nd subsequenttoan

7、aw project node that represents the ar driving process. You can change ar chiving settings al any time by bringing tip the properties dialog from lhe s:rthi?e rtccfestneftu.File. |D: fcodeCX)1A2orriecticiriPoc)I .jarDocumentation-nclude project documentstiori in archiver iretc ryrne- I docuni亍ntMim厂

8、 Compr&ss the contents of llie archivef7 Atwayi-. creste archive when building the projecitNe)d aFinishCancelHelp11. Archive Builder -Step 3 of 5 :指定结构文件包含内容,这里不做任何修改;12. Archive Builder -Step 4 of 5 :将 servlet 选择为 Allways in elude all classes and resources;13. Archive Builder -Step 4 of 5 :保持默认状态不改

9、变,按 finish 完成结构文件的定义;14. 在工程窗口用鼠标右键点 ConnectionPool结构,然后选择make生成结构文件;:JBuilder X - D:/code/poof/pool.jpFHe Edit Search Refactor View Project Run Team Wizards Tools VWWcw HelfIgHHI!MBi|HliPWt忙:言色:* iHK ye=I li3 JaM一 1就I ProjectF XF E2- .d .r poo bL1P pcoljpx+=Pfq 已?t 立 jr: e =r曰 Cunnu jlijnPoji務 Ccnr

10、 ecticriFol. sys* l_,r-ir i V-:i产r j =i.:=iLog File.Java*.hlkeD iiteTiriie jci. rPoDirdaraierh mC onne ctio田 W pool 赵曉 Acid Files / Packages Klass&s. I匹 Remove rom ProjectRename *CormedionPcjori.哮 CleanM MakeQ Rebuildn-i-?iPropertiesn使用方法:1. 将该 jar 文件添加到 Tools Con figure Librarise 中;2. 新加工程,在工程 Requ

11、ired Librarise 中添加该库文件;3. 在工程中新建一个“连接池初始化类”负责建立数据连接;4. 在工程中新建一属性文件-perties,负责描述数据库驱动及用户信息等,该属性文件存放的位置在PoolMa nager.java中定义;5. 在 PoolManager.java 中默认为“ ./ perties”,该位置表示在 /WEB-INF 目录下;附录:文件代码 perties / 属性文件*XBDBManager Propertiespoolname = sqlpool oraclepool 注:中间以空格分隔drivers = net.s

12、ourceforge.jtds.jdbc.Driver oracle.jdbc.driver.OracleDriver注:中间以空格分隔logfile = d:logfile.txt sqlpool.url = jdbc:jtds:sqlserver:/:1433;DatabaseName=pda sqlpool.initconns=50sqlpool.maxconns=80 sqlpool.user = pda sqlpool.password = pdapass1234 oraclepool.url = jdbc:oracle:thin::1521:

13、infodb oraclepool.initconns=50oraclepool.maxconns=80 oraclepool.user = tjold oraclepool.password = tjold*文件结束*文件开始*ConnectionPool.java* package ConnectionPool;import java.io.*;import java.sql.*;import java.util.*;public class ConnectionPool private String name; private String URL; private String use

14、r; private String password; private int maxConns; private int timeOut; private LogWriter logWriter;private int checkedOut;private Vector freeConnections = new Vector();public ConnectionPool(String name, String URL, String user,String password, int maxConns, int initConns, int timeOut, PrintWriter pw

15、, int logLevel) = name;this.URL = URL;this.user = user;this.password = password; this.maxConns = maxConns;this.timeOut = timeOut 0 ? timeOut : 5;logWriter = new LogWriter(name, logLevel, pw); initPool(initConns);logWriter.log(New pool created, LogWriter.INFO); String lf = System.getProperty

16、(line.separator);logWriter.log( url= + URL + user= + user +/ password= + password + initconns= + initConns + maxconns= + maxConns + logintimeout= + this.timeOut, LogWriter.INFO); logWriter.log(getStats(), LogWriter.INFO);private void initPool(int initConns) for (int i = 0; i initConns; i+) try Conne

17、ction pc = newConnection(); freeConnections.addElement(pc);catch (SQLException e) public Connection getConnection() throws SQLExceptionlogWriter.log(Request for connection received, LogWriter.DEBUG);tryConnection conn = getConnection(timeOut * 1000); return new ConnectionWrapper(conn, this);catch(SQ

18、LException e)logWriter.log(e, Exception getting connection, logWriter.ERROR);throw e;synchronized void wrapperClosed(Connection conn)freeConnections.addElement(conn);checkedOut-;notifyAll();logWriter.log(Returned wrapperClosed to pool, LogWriter.INFO); logWriter.log(getStats(), LogWriter.INFO);priva

19、te synchronized Connection getConnection(long timeout) throws SQLException / Get a pooled Connection from the cache or a new one./ Wait if all are checked out and the max limit has/ been reached.long startTime = System.currentTimeMillis();long remaining = timeout;Connection conn = null;while (conn =

20、 getPooledConnection() = null) try logWriter.log(Waiting for connection. Timeout= + remaining,LogWriter.DEBUG); wait(remaining); catch (InterruptedException e) remaining = timeout - (System.currentTimeMillis() - startTime); if (remaining 0) / Pick the first Connection in the Vector / to get round-ro

21、bin usage freeConnections.removeElementAt(0);else if (maxConns = 0 | checkedOut maxConns) conn = newConnection(); return conn;private Connection newConnection() throws SQLException Connection conn = null; if (user = null) conn = DriverManager.getConnection(URL); else conn = DriverManager.getConnecti

22、on(URL, user, password); logWriter.log(Opened a new connection, LogWriter.INFO); logWriter.log(getStats(), LogWriter.INFO); return conn;public synchronized void freeConnection(Connection conn) / Put the connection at the end of the Vector freeConnections.addElement(conn); checkedOut-;notifyAll();log

23、Writer.log(Returned freeConnection to pool, LogWriter.INFO); logWriter.log(getStats(), LogWriter.INFO);public synchronized void release() Enumeration allConnections = freeConnections.elements(); while (allConnections.hasMoreElements() try con.close(); logWriter.log(release Closed connection, LogWrit

24、er.INFO); catch (SQLException e) logWriter.log(e, release Couldnt close connection, LogWriter.ERROR); freeConnections.removeAllElements();private String getStats() return Total connections: + (freeConnections.size() + checkedOut)+ Available: + freeConnections.size() + Checked-out: + checkedOut;*文件结束

25、*ConnectionWrapper.java文件开始*package ConnectionPool;import java.sql.*;import java.util.*;/* This class is a wrapper around a Connection, overriding the* close method to just inform the pool that its available for* reuse again, and the isClosed method to return the state* of the wrapper instead of the

26、 Connection.*/class ConnectionWrapper implements Connection / realConn should be private but we use package scope to/ be able to test removal of bad connectionsConnection realConn;private ConnectionPool pool;private boolean isClosed = false;public ConnectionWrapper(Connection realConn, ConnectionPoo

27、l pool) this.realConn = realConn;this.pool = pool;* Inform the ConnectionPool that the ConnectionWrapper* is closed.*/public void close() throws SQLExceptionisClosed = true; pool.wrapperClosed(realConn);* Returns true if the ConnectionWrapper is closed, false * otherwise.*/public boolean isClosed()

28、throws SQLExceptionreturn isClosed;/* Wrapped methods.*/public void clearWarnings() throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection is closed); realConn.clearWarnings();public void commit() throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection is closed);

29、public Statement createStatement() throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection is closed); return realConn.createStatement();public boolean getAutoCommit() throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection is closed); return realConn.getAutoCommit(

30、);public String getCatalog() throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection is closed); return realConn.getCatalog();public DatabaseMetaData getMetaData() throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection is closed);return realConn.getMetaData();publi

31、c int getTransactionIsolation() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed);return realConn.getTransactionIsolation();public SQLWarning getWarnings() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return realConn.getWa

32、rnings();public boolean isReadOnly() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed);return realConn.isReadOnly();public String nativeSQL(String sql) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed);return realConn.nativeSQL(

33、sql);public CallableStatement prepareCall(String sql) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return realConn.prepareCall(sql);public PreparedStatement prepareStatement(String sql) throws SQLException if (isClosed) throw new SQLException(Pooled connecti

34、on is closed);return realConn.prepareStatement(sql);public void rollback() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed);realConn.rollback();public void setAutoCommit(boolean autoCommit) throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection

35、is closed); realConn.setAutoCommit(autoCommit);public void setCatalog(String catalog) throws SQLExceptionif (isClosed)throw new SQLException(Pooled connection is closed); realConn.setCatalog(catalog);public void setReadOnly(boolean readOnly) throws SQLException if (isClosed) throw new SQLException(P

36、ooled connection is closed); realConn.setReadOnly(readOnly);public void setTransactionIsolation(int level) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); realConn.setTransactionIsolation(level);public void setHoldability(int holdability) throws SQLException re

37、alConn.setHoldability(holdability);public int getHoldability() throws SQLExceptionreturn realConn.getHoldability();public Savepoint setSavepoint() throws SQLExceptionreturn realConn.setSavepoint();public Savepoint setSavepoint(String name) throws SQLExceptionreturn realConn.setSavepoint(name);public

38、 void releaseSavepoint(Savepoint savepoint) throws SQLException realConn.releaseSavepoint(savepoint);public void rollback(Savepoint savepoint) throws SQLException realConn.rollback(savepoint);public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) thro

39、ws SQLExceptionreturn realConn.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLExceptionreturn realConn.prepareStatement(sql,resultSetConcur

40、rency,resultSetHoldability);public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLExceptionreturn realConn.prepareCall(sql,resultSetConcurrency,resultSetHoldability);public PreparedStatement prepareStatement(String sql, int

41、autoGeneratedKeys) throws SQLExceptionreturn realConn.prepareStatement(sql,autoGeneratedKeys);public PreparedStatement prepareStatement(String sql, int columnIndexes) throws SQLExceptionreturn realConn.prepareStatement(sql,columnIndexes);public PreparedStatement prepareStatement(String sql, String c

42、olumnNames) throws SQLExceptionreturn realConn.prepareStatement(sql,columnNames);public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLExceptionreturn realConn.createStatement(resultSetType,resultSetConcurrency);public PreparedStatement prepareStatement(String sql,

43、int resultSetType, int resultSetConcurrency) throws SQLExceptionreturn realConn.prepareStatement(sql,resultSetType ,resultSetConcurrency);public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLExceptionreturn realConn.prepareCall(sql,resultSetType,res

44、ultSetConcurrency);public Map getTypeMap() throws SQLException return realConn.getTypeMap();public void setTypeMap(Map map) throws SQLException realConn.setTypeMap(map);*文件结束*文件开始*package ConnectionPool;LogFile.java*import java.io.*; import java.util.*;public class LogFile private static String myDi

45、r = c:test; private static String myFile = _tbit.log;public void LogFile() public static void log(String s) try MakeDateTime qdt = new MakeDateTime();String dateString = qdt.getTDashDate1();String stemp= qdt.getDate8();String filename = myDir + HK + stemp + .log;BufferedWriter out = new BufferedWrit

46、er(new FileWriter(filename, true);out.write(dateString + : + s);out.write(n);out.close(); catch(Exception e) ;public static void log(String _dir, String s) try MakeDateTime qdt = new MakeDateTime();String dateString = qdt.getTDashDate1();String stemp= qdt.getDate8();String filename = _dir;BufferedWr

47、iter out = new BufferedWriter(new FileWriter(filename, true);out.write(dateString + : + s);out.write(n);out.close(); catch(Exception e) ;*文件开始*LogWriter.java*package ConnectionPool;import java.io.*; import java.util.*;public class LogWriter public static final int NONE = 0;public static final int ER

48、ROR = 1;public static final int INFO = 2;public static final int DEBUG = 3;private static final String ERROR_TEXT = error; private static final String INFO_TEXT= info;private static final String DEBUG_TEXT = debug;private PrintWriter pw;private String owner;private int logLevel;public LogWriter(String owner, int logLevel, PrintWriter pw) this.pw = pw; this.owner = owner;this.logLevel = logLevel;publ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论