📄 pooledconnection.java
字号:
//
//
package com.scxh.eei.sql;
import java.sql.*;
import java.util.*;
import javax.sql.ConnectionEvent;
import javax.sql.ConnectionEventListener;
import com.scxh.eei.sql.proxy.*;
/**
* implementation of the <code>PooledConnection</code> interface.
*
* @version $Id: PooledConnection.java,v 1.11 2005/02/01 23:52:50 Exp $
*/
public class PooledConnection implements javax.sql.PooledConnection {
private ArrayList listeners = new ArrayList();
protected Connection _connection;
protected ConnectionProxy connection;
public PooledConnection(Connection connection) {
this._connection=connection;
}
/**
* Adds the specified listener to the list.
*
* @see #fireConnectionEvent
* @see #removeConnectionEventListener
*/
public synchronized void addConnectionEventListener(ConnectionEventListener listener) {
// Clone the list of listeners to avoid concurrent modifications. See
// bug [1113040] Small bug in net.sourceforge.jtds.jdbcx.PooledConnection
// for a description of how these can occur. The method still needs to
// be synchronized to prevent race conditions.
listeners = (ArrayList) listeners.clone();
// Now add the listener to the new, cloned list
listeners.add(listener);
}
/**
* Closes the database connection.
*
* @throws SQLException if an error occurs
*/
public synchronized void close() throws SQLException {
connection.close();
connection = null; // 垃圾收集的连接
}
/**
* Fires a new connection event on all listeners.
*
* @param closed <code>true</code> if <code>close</code> has been called on the
* connection; <code>false</code> if the <code>sqlException</code> represents
* an error where the connection may not longer be used.
* @param sqlException the SQLException to pass to the listeners
*/
public synchronized void fireConnectionEvent(boolean closed, SQLException sqlException) {
if (listeners.size() > 0) {
ConnectionEvent connectionEvent = new ConnectionEvent(this, sqlException);
Iterator iterator = listeners.iterator();
while (iterator.hasNext()) {
ConnectionEventListener listener = (ConnectionEventListener) iterator.next();
if (closed) {
if(listener!=null)
listener.connectionClosed(connectionEvent);
} else {
try {
if (connection == null || connection.isClosed()) {
listener.connectionErrorOccurred(connectionEvent);
}
} catch (SQLException ex) {
// Will never occur
}
}
}
}
}
/**
* Returns a ConnectionProxy.
*
* @throws SQLException if an error occurs
*/
public synchronized Connection getConnection() throws SQLException {
if(connection!=null){
throw new SQLException("the PooledConnection is used");
}else{
this.connection = new ConnectionProxy(this, _connection);
return this.connection;
}
}
public synchronized void freeConnection() throws SQLException {
this.connection=null;
}
/**
* Removes the specified listener from the list.
*
* @see #addConnectionEventListener
* @see #fireConnectionEvent
*/
public synchronized void removeConnectionEventListener(ConnectionEventListener listener) {
// Clone the list of listeners to avoid concurrent modifications. See
// bug [1113040] Small bug in net.sourceforge.jtds.jdbcx.PooledConnection
// for a description of how these can occur. The method still needs to
// be synchronized to prevent race conditions.
listeners = (ArrayList) listeners.clone();
// Now remove the listener from the new, cloned list
listeners.remove(listener);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -