/** * Validates a pooled connection. * * @param key ignored * @param p wrapped {@link PooledConnectionAndInfo} containing the connection to validate * @return true if validation succeeds */ @Override public boolean validateObject(UserPassKey key, PooledObject<PooledConnectionAndInfo> p) { try { validateLifetime(p); } catch (Exception e) { return false; } boolean valid = false; PooledConnection pconn = p.getObject().getPooledConnection(); if (null == _validationQuery) { int timeout = _validationQueryTimeout; if (timeout < 0) { timeout = 0; } try { valid = pconn.getConnection().isValid(timeout); } catch (SQLException e) { valid = false; } } else { 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 validatingSet.add(pconn); try { conn = pconn.getConnection(); stmt = conn.createStatement(); rset = stmt.executeQuery(_validationQuery); if (rset.next()) { valid = true; } else { valid = false; } if (_rollbackAfterValidation) { conn.rollback(); } } catch (Exception e) { valid = false; } finally { Utils.closeQuietly(rset); Utils.closeQuietly(stmt); Utils.closeQuietly(conn); validatingSet.remove(pconn); } } return valid; }
private void validateLifetime(PooledObject<PooledConnectionAndInfo> p) throws Exception { if (maxConnLifetimeMillis > 0) { long lifetime = System.currentTimeMillis() - p.getCreateTime(); if (lifetime > maxConnLifetimeMillis) { throw new Exception( Utils.getMessage( "connectionFactory.lifetimeExceeded", Long.valueOf(lifetime), Long.valueOf(maxConnLifetimeMillis))); } } }