| Method from org.apache.commons.dbcp.datasources.KeyedCPDSConnectionFactory Detail: |
public void activateObject(Object key,
Object obj) {
}
|
public void connectionClosed(ConnectionEvent event) {
PooledConnection pc = (PooledConnection)event.getSource();
// if this event occured becase we were validating, ignore it
// otherwise return the connection to the pool.
if (!validatingMap.containsKey(pc)) {
PooledConnectionAndInfo info =
(PooledConnectionAndInfo) pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
try {
_pool.returnObject(info.getUserPassKey(), info);
} catch (Exception e) {
System.err.println("CLOSING DOWN CONNECTION AS IT COULD " +
"NOT BE RETURNED TO THE POOL");
try {
destroyObject(info.getUserPassKey(), info);
} catch (Exception e2) {
System.err.println("EXCEPTION WHILE DESTROYING OBJECT " +
info);
e2.printStackTrace();
}
}
}
}
This will be called if the Connection returned by the getConnection
method came from a PooledConnection, and the user calls the close()
method of this connection object. What we need to do here is to
release this PooledConnection from our pool... |
public void connectionErrorOccurred(ConnectionEvent event) {
PooledConnection pc = (PooledConnection)event.getSource();
try {
if (null != event.getSQLException()) {
System.err
.println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" +
event.getSQLException() + ")");
}
//remove this from the listener list because we are no more
//interested in errors since we are about to close this connection
pc.removeConnectionEventListener(this);
} catch (Exception ignore) {
// ignore
}
PooledConnectionAndInfo info = (PooledConnectionAndInfo) pcMap.get(pc);
if (info == null) {
throw new IllegalStateException(NO_KEY_MESSAGE);
}
try {
destroyObject(info.getUserPassKey(), info);
} catch (Exception e) {
System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + info);
e.printStackTrace();
}
}
If a fatal error occurs, close the underlying physical connection so as
not to be returned in the future |
public void destroyObject(Object key,
Object obj) throws Exception {
if (obj instanceof PooledConnectionAndInfo) {
PooledConnection pc = ((PooledConnectionAndInfo)obj).getPooledConnection();
pcMap.remove(pc);
pc.close();
}
}
|
public KeyedObjectPool getPool() {
return _pool;
}
|
public synchronized Object makeObject(Object key) throws Exception {
Object obj = null;
UserPassKey upkey = (UserPassKey)key;
PooledConnection pc = null;
String username = upkey.getUsername();
String password = upkey.getPassword();
if (username == null) {
pc = _cpds.getPooledConnection();
} else {
pc = _cpds.getPooledConnection(username, password);
}
// should we add this object as a listener or the pool.
// consider the validateObject method in decision
pc.addConnectionEventListener(this);
obj = new PooledConnectionAndInfo(pc, username, password);
pcMap.put(pc, obj);
return obj;
}
|
public void passivateObject(Object key,
Object obj) {
}
|
public synchronized void setCPDS(ConnectionPoolDataSource cpds) {
_cpds = cpds;
}
|
public synchronized void setPool(KeyedObjectPool pool) throws SQLException {
if (null != _pool && pool != _pool) {
try {
_pool.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new SQLNestedException("Cannot set the pool on this factory", e);
}
}
_pool = pool;
}
Sets the {*link ObjectPool} in which to pool {*link Connection}s. |
public synchronized void setRollbackAfterValidation(boolean rollbackAfterValidation) {
_rollbackAfterValidation = rollbackAfterValidation;
}
Sets whether a rollback should be issued after
{*link #validateObject validating}
{*link Connection}s. |
public synchronized void setValidationQuery(String validationQuery) {
_validationQuery = validationQuery;
}
Sets the query I use to {*link #validateObject validate} {*link Connection}s.
Should return at least one row.
May be null |
public boolean validateObject(Object key,
Object obj) {
boolean valid = false;
if (obj instanceof PooledConnectionAndInfo) {
PooledConnection pconn =
((PooledConnectionAndInfo)obj).getPooledConnection();
String query = _validationQuery;
if (null != query) {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
// logical Connection from the PooledConnection must be closed
// before another one can be requested and closing it will
// generate an event. Keep track so we know not to return
// the PooledConnection
validatingMap.put(pconn, null);
try {
conn = pconn.getConnection();
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
if (rset.next()) {
valid = true;
} else {
valid = false;
}
if (_rollbackAfterValidation) {
conn.rollback();
}
} catch(Exception e) {
valid = false;
} finally {
if (rset != null) {
try {
rset.close();
} catch (Throwable t) {
// ignore
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Throwable t) {
// ignore
}
}
if (conn != null) {
try {
conn.close();
} catch (Throwable t) {
// ignore
}
}
validatingMap.remove(pconn);
}
} else {
valid = true;
}
} else {
valid = false;
}
return valid;
}
|