




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、關(guān)于ConnectionPool.jar數(shù)據(jù)連接池文件包的制作流程目的:制作通用型數(shù)據(jù)連接池文件包,便于應(yīng)用于通用工程結(jié)構(gòu):工程包內(nèi)classes目錄內(nèi)1.ConnectionPool :連接池管理程序|_ConnectionPool.java|_ConnectionWrapper.java|_LogFile.java|_LogWriter.java|_MakeDateTime.java|_PoolManager.java2. com :MySql JDBC驅(qū)動程序3. org :MySql JDBC驅(qū)動程序4. net :MSSqlserver JTDS 驅(qū)動程序5. javax :Orac
2、le JDBC 驅(qū)動程序6. oracle :Oracle JDBC 驅(qū)動程序制作流程:1. 新建一個工程命名為Pool,在工程內(nèi)建立一個war模塊命名為Poolwar;2. 在工程內(nèi)新建class文件,文件的包起名為ConnectionPool;3. 依次新建下列文件:ConnectionPool.java、ConnectionWrapper.java、LogFile.java、LogWriter.java、MakeDateTime.java、PoolManager.java4. 將Oracle for JDBC驅(qū)動classes12.jar文件用winrar解壓縮,得到j(luò)avax和orac
3、le兩個文件夾,將這兩個文件夾復(fù)制到Pool工程內(nèi)的classes目錄下;5. 將MS-Sqlserver for JDBC驅(qū)動jtds-1.2.jar文件用winrar解壓縮,得到net這個文件夾,將這個文件夾復(fù)制到Pool工程內(nèi)的classes目錄下;6. 將MySql for JDBC驅(qū)動mysql-connector-java-3.0.16-ga-bin.jar文件用winrar解壓縮,得到com和org兩個文件夾,將這兩個文件夾復(fù)制到Pool工程內(nèi)的classes目錄下;7. 如果需要添加其他驅(qū)動程序,可參照4-6進行;8. 選擇JBuilder工具條上Wizards-àA
4、rchive 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 : 指定結(jié)構(gòu)文件包含內(nèi)容,這里不做任何修改;12Archive Builder Step 4 of 5 : 將servlet 選擇為 Allways include all c
5、lasses and resources;13Archive Builder Step 4 of 5 : 保持默認(rèn)狀態(tài)不改變,按finish完成結(jié)構(gòu)文件的定義;14在工程窗口用鼠標(biāo)右鍵點 ConnectionPool 結(jié)構(gòu),然后選擇make 生成結(jié)構(gòu)文件;使用方法:1. 將該jar文件添加到 ToolsàConfigure Librarise 中;2. 新加工程,在工程 Required Librarise 中添加該庫文件;3. 在工程中新建一個“連接池初始化類”負(fù)責(zé)建立數(shù)據(jù)連接;4. 在工程中新建一屬性文件-perties,負(fù)責(zé)描述數(shù)據(jù)庫驅(qū)動及用戶信息等,該屬性文件存放
6、的位置在PoolManager.java中定義;5. 在PoolManager.java中默認(rèn)為“./ perties”,該位置表示在/WEB-INF 目錄下;附錄:文件代碼*文件開始*perties /屬性文件*XBDBManager Propertiespoolname = sqlpool oraclepool 注:中間以空格分隔drivers = net.sourceforge.jtds.jdbc.Driver oracle.jdbc.driver.OracleDriver 注:中間以空格分隔logfile = d:logfile.txtsqlpool.url =
7、 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é)束*文件
8、開始*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 c
9、heckedOut; 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 =
10、 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=&q
11、uot; + 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;
12、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 Connectio
13、nWrapper(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", Log
14、Writer.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.currentTimeMilli
15、s(); 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() - startTi
16、me); if (remaining <= 0) / Timeout has expired logWriter.log("Time-out while waiting for connection", LogWriter.DEBUG); throw new SQLException("getConnection() timed-out"); / Check if the Connection is still OK if (!isConnectionOK(conn) / It was bad. Try again with the remaini
17、ng timeout logWriter.log("Removed selected bad connection from pool", LogWriter.ERROR); return getConnection(remaining); checkedOut+; logWriter.log("Delivered getConnection from pool", LogWriter.INFO); logWriter.log(getStats(), LogWriter.INFO); return conn; private boolean isConn
18、ectionOK(Connection conn) Statement testStmt = null; try if (!conn.isClosed() / Try to createStatement to see if it's really alive testStmt = conn.createStatement(); testStmt.close(); else return false; catch (SQLException e) if (testStmt != null) try testStmt.close(); catch (SQLException se) lo
19、gWriter.log(e, "Pooled Connection was not okay", LogWriter.ERROR); return false; return true; private Connection getPooledConnection() throws SQLException Connection conn = null; if (freeConnections.size() > 0) / Pick the first Connection in the Vector / to get round-robin usage conn =
20、(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(U
21、RL); 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 freeCo
22、nnections.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.hasMoreElemen
23、ts() Connection con = (Connection) allConnections.nextElement(); try con.close(); logWriter.log("release Closed connection", LogWriter.INFO); catch (SQLException e) logWriter.log(e, "release Couldn't close connection", LogWriter.ERROR); freeConnections.removeAllElements(); pr
24、ivate String getStats() return "Total connections: " + (freeConnections.size() + checkedOut) + " Available: " + freeConnections.size() + " Checked-out: " + checkedOut; *文件結(jié)束*文件開始*ConnectionWrapper.java*package ConnectionPool;import java.sql.*;import java.util.*;/* * Thi
25、s class is a wrapper around a Connection, overriding the * close method to just inform the pool that it's 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 priv
26、ate 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 ConnectionPo
27、ol 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. */ pub
28、lic 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 c
29、reateStatement() 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.get
30、AutoCommit(); 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&q
31、uot;); 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 SQLExce
32、ption("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 (isCl
33、osed) 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 Prepared
34、Statement 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"); r
35、ealConn.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(&qu
36、ot;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 SQLE
37、xception 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.getHo
38、ldability(); 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(savepoin
39、t); 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 prepareSta
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)規(guī)范稅務(wù)管理制度
- 企業(yè)內(nèi)部節(jié)能管理制度
- 企業(yè)外協(xié)班組管理制度
- 企業(yè)供水設(shè)備管理制度
- 倉庫整排看板管理制度
- 書法教室臺賬管理制度
- 企業(yè)用電規(guī)范管理制度
- 會展公司規(guī)章管理制度
- 進出口商品質(zhì)量管理制度
- 嚴(yán)格單位資產(chǎn)管理制度
- 九師聯(lián)盟2024-2025學(xué)年高二下學(xué)期6月摸底聯(lián)考語文試題(含答案)
- 非遺文化掐絲琺瑯景泰藍(lán)
- 2025年幼兒園教師招聘考試試題及答案
- 電動葫蘆考試題及答案
- 2025廣東省勞動合同樣本
- 2025餐飲兼職合同樣本
- 農(nóng)資安全宣傳課件
- 2025年甘肅電投集團公司招聘筆試參考題庫含答案解析
- 國家開放大學(xué)《Web開發(fā)基礎(chǔ)》形考任務(wù)實驗1-5參考答案
- 中外美術(shù)評析與欣賞智慧樹知到期末考試答案章節(jié)答案2024年湖南大學(xué)
- 固體火箭發(fā)動機制造工藝
評論
0/150
提交評論