版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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ū)動(dòng)程序3. org :MySql JDBC驅(qū)動(dòng)程序4. net :MSSqlserver JTDS 驅(qū)動(dòng)程序5. javax :Orac
2、le JDBC 驅(qū)動(dòng)程序6. oracle :Oracle JDBC 驅(qū)動(dòng)程序制作流程:1. 新建一個(gè)工程命名為Pool,在工程內(nèi)建立一個(gè)war模塊命名為Poolwar;2. 在工程內(nèi)新建class文件,文件的包起名為ConnectionPool;3. 依次新建下列文件:ConnectionPool.java、ConnectionWrapper.java、LogFile.java、LogWriter.java、MakeDateTime.java、PoolManager.java4. 將Oracle for JDBC驅(qū)動(dòng)classes12.jar文件用winrar解壓縮,得到j(luò)avax和orac
3、le兩個(gè)文件夾,將這兩個(gè)文件夾復(fù)制到Pool工程內(nèi)的classes目錄下;5. 將MS-Sqlserver for JDBC驅(qū)動(dòng)jtds-1.2.jar文件用winrar解壓縮,得到net這個(gè)文件夾,將這個(gè)文件夾復(fù)制到Pool工程內(nèi)的classes目錄下;6. 將MySql for JDBC驅(qū)動(dòng)mysql-connector-java-3.0.16-ga-bin.jar文件用winrar解壓縮,得到com和org兩個(gè)文件夾,將這兩個(gè)文件夾復(fù)制到Pool工程內(nèi)的classes目錄下;7. 如果需要添加其他驅(qū)動(dòng)程序,可參照4-6進(jìn)行;8. 選擇JBuilder工具條上Wizards-àA
4、rchive Builder ;9.Archive Builder Step 1 of 5 : Archive Type 選擇Basic(具體類型含義參考JBuilerX使用手冊(cè));10. Archive Builder Step 2 of 5 : 將Name 和 File 命名為需要的名字,這里同意將其命名為ConnectionPool,其他的選項(xiàng)均不做更改;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)右鍵點(diǎn) ConnectionPool 結(jié)構(gòu),然后選擇make 生成結(jié)構(gòu)文件;使用方法:1. 將該jar文件添加到 ToolsàConfigure Librarise 中;2. 新加工程,在工程 Required Librarise 中添加該庫文件;3. 在工程中新建一個(gè)“連接池初始化類”負(fù)責(zé)建立數(shù)據(jù)連接;4. 在工程中新建一屬性文件-perties,負(fù)責(zé)描述數(shù)據(jù)庫驅(qū)動(dòng)及用戶信息等,該屬性文件存放
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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年城市綠地養(yǎng)護(hù)保潔服務(wù)合同3篇
- 溫州肯恩大學(xué)《AM技術(shù)及應(yīng)用》2023-2024學(xué)年第一學(xué)期期末試卷
- 二零二五年度跨境電商供應(yīng)鏈融資擔(dān)保協(xié)議書3篇
- 二零二五版廢鐵貿(mào)易結(jié)算與倉(cāng)儲(chǔ)服務(wù)合同3篇
- 二零二五年金融租賃擔(dān)保協(xié)議與保證合同規(guī)范2篇
- 2025年度特色小吃街加盟經(jīng)營(yíng)合同范本3篇
- 2025年度電影項(xiàng)目投資與回報(bào)分成協(xié)議3篇
- 2024文化藝術(shù)品交易平臺(tái)建設(shè)與運(yùn)營(yíng)協(xié)議
- 2024版保安勞動(dòng)合同書范本
- 2025年度化學(xué)原料藥廢棄物處理與資源化利用合同3篇
- 2024年計(jì)算機(jī)二級(jí)WPS考試題庫(共380題含答案)
- 《湖南省房屋建筑和市政工程消防質(zhì)量控制技術(shù)標(biāo)準(zhǔn)》
- 中建集團(tuán)面試自我介紹
- 《工業(yè)園區(qū)節(jié)水管理規(guī)范》
- 警校生職業(yè)生涯規(guī)劃
- 意識(shí)障礙患者的護(hù)理診斷及措施
- 2024版《53天天練單元?dú)w類復(fù)習(xí)》3年級(jí)語文下冊(cè)(統(tǒng)編RJ)附參考答案
- 2025企業(yè)年會(huì)盛典
- 215kWh工商業(yè)液冷儲(chǔ)能電池一體柜用戶手冊(cè)
- 場(chǎng)地平整施工組織設(shè)計(jì)-(3)模板
- 交通設(shè)施設(shè)備供貨及技術(shù)支持方案
評(píng)論
0/150
提交評(píng)論