📄 connectionwrapper.java
字号:
package cncgw.smdb;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author * @version 1.0 */import java.sql.*;import java.util.*;/** * 此类是数据连接类Connection的一个包装器,它实现了一般意义上的 * Connection接口,主要目的是为了当应用系统关闭一个数据连接的时 * 候不去关闭该数据连接,而只把连接归还给连接池。 */class ConnectionWrapper implements java.sql.Connection{ // realConn 本来可以使用private 修饰符号,我们在这里把它定义为包级别 // 的对象是为了在测试中去掉已经失效的数据连接 Connection realConn; private ConnectionPool pool; private boolean isClosed = false; public ConnectionWrapper(Connection realConn, ConnectionPool pool) { this.realConn = realConn; this.pool = pool; } /** * 这是该类最重要的一个方法,当关闭一个ConnectionWrapper数据连接 * 的时候使用数据连接池的关闭方法。 */ public void close() throws SQLException { isClosed = true; pool.wrapperClosed(realConn); } /** * 判断数据连接是否已经被关闭。 */ public boolean isClosed() throws SQLException { return isClosed; } /* * 以下的方法都是原来数据连接已经定义过的方法,一个ConnectionWrapper * 类只是传递该方法到Connection类中去。 */ 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"); realConn.commit(); } public Statement createStatement(int resultSetType,int resultSetConcurrency, int resultSetHoldability) throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); return realConn.createStatement(resultSetType,resultSetConcurrency, resultSetHoldability); } public Statement createStatement(int resultSetType,int resultSetConcurrency) throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); return realConn.createStatement(resultSetType,resultSetConcurrency); } 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 int getHoldability() throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); return realConn.getHoldability(); } 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 Map getTypeMap() throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); return realConn.getTypeMap() ; } 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,int resultSetType, int resultSetConcurrency,int resultSetHoldability) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } return realConn.prepareCall(sql,resultSetType, resultSetConcurrency,resultSetHoldability); } public CallableStatement prepareCall(String sql,int resultSetType, int resultSetConcurrency) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } return realConn.prepareCall(sql,resultSetType, resultSetConcurrency); } 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 PreparedStatement prepareStatement(String sql,String[] columnNames) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } return realConn.prepareStatement(sql,columnNames); } public PreparedStatement prepareStatement(String sql, int[] columnindexes) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } return realConn.prepareStatement(sql,columnindexes); } public PreparedStatement prepareStatement(String sql, int autoGenerateKeys) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } return realConn.prepareStatement(sql,autoGenerateKeys); } public PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency) throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); return realConn.prepareStatement(sql,resultSetType, resultSetConcurrency); } public PreparedStatement prepareStatement(String sql, int resultSetType,int resultSetConcurrency,int resultSetHoldability) throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); return realConn.prepareStatement(sql,resultSetType, resultSetConcurrency,resultSetHoldability); } public void releaseSavepoint(Savepoint savepoint) throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); realConn.releaseSavepoint(savepoint); } public void rollback() throws SQLException { if (isClosed) throw new SQLException("Pooled connection is closed"); realConn.rollback(); } public void rollback(Savepoint savepoint) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } realConn.rollback(savepoint); } 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 setHoldability(int holdAbility) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } realConn.setHoldability(holdAbility); } public void setReadOnly(boolean readOnly) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } realConn.setReadOnly(readOnly); } public Savepoint setSavepoint(String savepoint) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } return realConn.setSavepoint(savepoint) ; } public Savepoint setSavepoint() throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } return realConn.setSavepoint() ; } public void setTransactionIsolation(int level) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } realConn.setTransactionIsolation(level); } public void setTypeMap(Map map) throws SQLException { if (isClosed) { throw new SQLException("Pooled connection is closed"); } realConn.setTypeMap(map) ; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -