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

下载本文档

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

文档简介

关于ConnectionPool.jar数据连接池文件包的制作流程目的:制作通用型数据连接池文件包,便于应用于通用工程结构:工程包内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 :Oracle JDBC 驱动程序6. oracle :Oracle JDBC 驱动程序制作流程:1. 新建一个工程命名为Pool,在工程内建立一个war模块命名为Poolwar;2. 在工程内新建class文件,文件的包起名为ConnectionPool;3. 依次新建下列文件:ConnectionPool.java、ConnectionWrapper.java、LogFile.java、LogWriter.java、MakeDateTime.java、PoolManager.java4. 将Oracle for JDBC驱动classes12.jar文件用winrar解压缩,得到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. 选择JBuilder工具条上Wizards-Archive Builder ;9.Archive Builder Step 1 of 5 : Archive Type 选择Basic(具体类型含义参考JBuilerX使用手册);10. Archive Builder Step 2 of 5 : 将Name 和 File 命名为需要的名字,这里同意将其命名为ConnectionPool,其他的选项均不做更改;11Archive Builder Step 3 of 5 : 指定结构文件包含内容,这里不做任何修改;12Archive Builder Step 4 of 5 : 将servlet 选择为 Allways include all classes and resources;13Archive Builder Step 4 of 5 : 保持默认状态不改变,按finish完成结构文件的定义;14在工程窗口用鼠标右键点 ConnectionPool 结构,然后选择make 生成结构文件;使用方法:1. 将该jar文件添加到 ToolsConfigure Librarise 中;2. 新加工程,在工程 Required Librarise 中添加该库文件;3. 在工程中新建一个“连接池初始化类”负责建立数据连接;4. 在工程中新建一属性文件-perties,负责描述数据库驱动及用户信息等,该属性文件存放的位置在PoolManager.java中定义;5. 在PoolManager.java中默认为“./ perties”,该位置表示在/WEB-INF 目录下;附录:文件代码*文件开始*perties /属性文件*XBDBManager Propertiespoolname = sqlpool oraclepool 注:中间以空格分隔drivers = net.sourceforge.jtds.jdbc.Driver oracle.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*文件结束*文件开始*ConnectionPool.java*package ConnectionPool;import java.io.*;import java.sql.*;import java.util.*;public class ConnectionPool private String name; private String URL; private String user; 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, 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(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 Connection pc = newConnection(); freeConnections.addElement(pc); catch (SQLException e) public Connection getConnection() throws SQLException logWriter.log(Request for connection received, LogWriter.DEBUG); try Connection conn = getConnection(timeOut * 1000); return new ConnectionWrapper(conn, this); catch(SQLException 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); private 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 = 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-robin usage conn = (Connection) freeConnections.firstElement(); 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.getConnection(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(); logWriter.log(Returned freeConnection to pool, LogWriter.INFO); logWriter.log(getStats(), LogWriter.INFO); public synchronized void release() Enumeration allConnections = freeConnections.elements(); while (allConnections.hasMoreElements() Connection con = (Connection) allConnections.nextElement(); try con.close(); logWriter.log(release Closed connection, LogWriter.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; *文件结束*文件开始*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 Connection. */class ConnectionWrapper implements Connection / realConn should be private but we use package scope to / be able to test removal of bad connections Connection realConn; private ConnectionPool pool; private boolean isClosed = false; public ConnectionWrapper(Connection realConn, ConnectionPool pool) this.realConn = realConn; this.pool = pool; /* * Inform the ConnectionPool that the ConnectionWrapper * is closed. */ public void close() throws SQLException isClosed = true; pool.wrapperClosed(realConn); /* * Returns true if the ConnectionWrapper is closed, false * otherwise. */ public boolean isClosed() throws SQLException return isClosed; /* * Wrapped methods. */ public void clearWarnings() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); realConn.clearWarnings(); public void commit() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); realCmit(); public Statement createStatement() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return realConn.createStatement(); public boolean getAutoCommit() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return realConn.getAutoCommit(); public String getCatalog() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return realConn.getCatalog(); public DatabaseMetaData getMetaData() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return realConn.getMetaData(); public 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.getWarnings(); 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(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 connection 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 SQLException if (isClosed) throw new SQLException(Pooled connection is closed); realConn.setAutoCommit(autoCommit); public void setCatalog(String catalog) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); realConn.setCatalog(catalog); public void setReadOnly(boolean readOnly) throws SQLException if (isClosed) throw new SQLException(Pooled 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 realConn.setHoldability(holdability); public int getHoldability() throws SQLException return realConn.getHoldability(); public Savepoint setSavepoint() throws SQLException return realConn.setSavepoint(); public Savepoint setSavepoint(String name) throws SQLException return realConn.setSavepoint(name); public 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) throws SQLException return realConn.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability); public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException return realConn.prepareStatement(sql,resultSetC

温馨提示

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

评论

0/150

提交评论