PooledConnectionAndInfo info = null;
try {
info = getPooledConnectionAndInfo(username, password);
} catch (NoSuchElementException e) {
closeDueToException(info);
throw new SQLNestedException("Cannot borrow connection from pool", e);
} catch (RuntimeException e) {
closeDueToException(info);
throw e;
} catch (SQLException e) {
closeDueToException(info);
throw e;
} catch (Exception e) {
closeDueToException(info);
throw new SQLNestedException("Cannot borrow connection from pool", e);
}
if (!(null == password ? null == info.getPassword()
: password.equals(info.getPassword()))) { // Password on PooledConnectionAndInfo does not match
try { // See if password has changed by attempting connection
testCPDS(username, password);
} catch (SQLException ex) {
// Password has not changed, so refuse client, but return connection to the pool
closeDueToException(info);
throw new SQLException("Given password did not match password used"
+ " to create the PooledConnection.");
} catch (javax.naming.NamingException ne) {
throw (SQLException) new SQLException(
"NamingException encountered connecting to database").initCause(ne);
}
/*
* Password must have changed -> destroy connection and keep retrying until we get a new, good one,
* destroying any idle connections with the old passowrd as we pull them from the pool.
*/
final UserPassKey upkey = info.getUserPassKey();
final PooledConnectionManager manager = getConnectionManager(upkey);
manager.invalidate(info.getPooledConnection()); // Destroy and remove from pool
manager.setPassword(upkey.getPassword()); // Reset the password on the factory if using CPDSConnectionFactory
info = null;
for (int i = 0; i < 10; i++) { // Bound the number of retries - only needed if bad instances return
try {
info = getPooledConnectionAndInfo(username, password);
} catch (NoSuchElementException e) {
closeDueToException(info);
throw new SQLNestedException("Cannot borrow connection from pool", e);
} catch (RuntimeException e) {
closeDueToException(info);
throw e;
} catch (SQLException e) {
closeDueToException(info);
throw e;
} catch (Exception e) {
closeDueToException(info);
throw new SQLNestedException("Cannot borrow connection from pool", e);
}
if (info != null && password.equals(info.getPassword())) {
break;
} else {
if (info != null) {