📄 pooledconnection.java
字号:
/*
* @(#)PooledConnection.java 1.41 06/08/21
*/
package mfkvit;
import java.sql.*;
import java.util.Map;
/**
* <p>Title: PooledConnection</p>
*
* <p>Description: This class contains a <code>java.sql.Connection</code>
* instance object. The class will do the function as same as
* <code>java.sql.Connection</code> class' using connection pool tech. Now the
* version for <code>java.sql.Connection</code> interface is JDK1.41 </p>
*
* @author Qian YANG
* @version 1.0
*/
public class PooledConnection implements Connection {
private static final boolean STATUS_FREE = true;
private static final boolean STATUS_IN_USE = false;
private static final String WARNING_MSG_CONNECTION_ALREADY_CLOSE =
"The connection is already been closed!";
/**
* Uses for ConnectionPool if the connection is free or not
*/
private boolean freeOrNot = STATUS_IN_USE;
/**
* real connection object connected to database
*/
private Connection conn = null;
/**
* current connection when to be in use
*/
private long startUsingTimeMillis = 0;
public PooledConnection(String driverName, String dbUrl, String user,
String password) throws
SQLException {
try {
Class.forName(driverName);
} catch(ClassNotFoundException cnfe) {
System.out.println("Naming space not found for " + driverName);
cnfe.printStackTrace();
}
this.conn = DriverManager.getConnection(dbUrl, user, password);
freeOrNot = STATUS_FREE;
}
/**
* Set the connection object's status. It will record the timemillis when
* the connection is being using automatically.
*/
public void setBeUsing() {
freeOrNot = STATUS_IN_USE;
startUsingTimeMillis = System.currentTimeMillis();
}
/**
* Returns the timemillis for current connection's using time.
*
* @return long
*/
public long getUsingTimeMillis() {
if (isFree()) {
return -1L;
}
return System.currentTimeMillis() - startUsingTimeMillis;
}
/**
* Retrieves whether this connection is free or being using.
*
* @return <code>true</code> if this <code>Connection</code> object is
* closed; <code>false</code> if it is still open
*/
private boolean isFree() {
return freeOrNot;
}
/**
* Set current connection free instead of releasing the real connection to
* database.
*/
public void close() throws SQLException {
freeOrNot = STATUS_FREE;
}
/**
* Retrieves whether this connection is free or being using.
*
* @return <code>true</code> if this <code>Connection</code> object is
* closed; <code>false</code> if it is still open
*/
public boolean isClosed() {
return isFree();
}
/**
* Release the connection.
*
* @throws SQLException
*/
public void releaseConnection() throws SQLException {
if (isFree()) {
conn.close();
}
}
public int getHoldability() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.getHoldability();
}
public int getTransactionIsolation() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.getTransactionIsolation();
}
public void clearWarnings() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.clearWarnings();
}
public void commit() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.commit();
}
public void rollback() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.rollback();
}
public boolean getAutoCommit() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.getAutoCommit();
}
public boolean isReadOnly() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.isReadOnly();
}
public void setHoldability(int holdability) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.setHoldability(holdability);
}
public void setTransactionIsolation(int level) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.setTransactionIsolation(level);
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.setAutoCommit(autoCommit);
}
public void setReadOnly(boolean readOnly) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.setReadOnly(readOnly);
}
public String getCatalog() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.getCatalog();
}
public void setCatalog(String catalog) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.setCatalog(catalog);
}
public DatabaseMetaData getMetaData() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.getMetaData();
}
public SQLWarning getWarnings() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.getWarnings();
}
public Savepoint setSavepoint() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.setSavepoint();
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.releaseSavepoint(savepoint);
}
public void rollback(Savepoint savepoint) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.rollback(savepoint);
}
public Statement createStatement() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.createStatement();
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.createStatement(resultSetType, resultSetConcurrency);
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.createStatement(resultSetType, resultSetConcurrency,
resultSetHoldability);
}
public Map getTypeMap() throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.getTypeMap();
}
public void setTypeMap(Map map) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
conn.setTypeMap(map);
}
public String nativeSQL(String sql) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.nativeSQL(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareCall(sql);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareCall(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareStatement(sql);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareStatement(sql, autoGeneratedKeys);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareStatement(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareStatement(sql, columnIndexes);
}
public Savepoint setSavepoint(String name) throws SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.setSavepoint(name);
}
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws
SQLException {
if (isFree()) {
throw new SQLWarning(WARNING_MSG_CONNECTION_ALREADY_CLOSE);
}
return conn.prepareStatement(sql, columnNames);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -