tomcat-jdbc-8.5.11-sources.jar!/org/apache/tomcat/jdbc/pool/ConnectionPool.javajava
/** * Validates and configures a previously idle connection * @param now - timestamp * @param con - the connection to validate and configure * @param username The user name to use for the connection * @param password The password for the connection * @return a connection * @throws SQLException if a validation error happens */ protected PooledConnection borrowConnection(long now, PooledConnection con, String username, String password) throws SQLException { //we have a connection, lets set it up //flag to see if we need to nullify boolean setToNull = false; try { con.lock(); if (con.isReleased()) { return null; } //evaluate username/password change as well as max age functionality boolean forceReconnect = con.shouldForceReconnect(username, password) || con.isMaxAgeExpired(); if (!con.isDiscarded() && !con.isInitialized()) { //here it states that the connection not discarded, but the connection is null //don't attempt a connect here. It will be done during the reconnect. forceReconnect = true; } if (!forceReconnect) { if ((!con.isDiscarded()) && con.validate(PooledConnection.VALIDATE_BORROW)) { //set the timestamp con.setTimestamp(now); if (getPoolProperties().isLogAbandoned()) { //set the stack trace for this pool con.setStackTrace(getThreadDump()); } if (!busy.offer(con)) { log.debug("Connection doesn't fit into busy array, connection will not be traceable."); } return con; } } //if we reached here, that means the connection //is either has another principal, is discarded or validation failed. //we will make one more attempt //in order to guarantee that the thread that just acquired //the connection shouldn't have to poll again. try { con.reconnect(); reconnectedCount.incrementAndGet(); int validationMode = getPoolProperties().isTestOnConnect() || getPoolProperties().getInitSQL()!=null ? PooledConnection.VALIDATE_INIT : PooledConnection.VALIDATE_BORROW; if (con.validate(validationMode)) { //set the timestamp con.setTimestamp(now); if (getPoolProperties().isLogAbandoned()) { //set the stack trace for this pool con.setStackTrace(getThreadDump()); } if (!busy.offer(con)) { log.debug("Connection doesn't fit into busy array, connection will not be traceable."); } return con; } else { //validation failed. throw new SQLException("Failed to validate a newly established connection."); } } catch (Exception x) { release(con); setToNull = true; if (x instanceof SQLException) { throw (SQLException)x; } else { SQLException ex = new SQLException(x.getMessage()); ex.initCause(x); throw ex; } } } finally { con.unlock(); if (setToNull) { con = null; } } }
tomcat-jdbc-8.5.11-sources.jar!/org/apache/tomcat/jdbc/pool/ConnectionPool.javaapache
/** * Returns a connection to the pool * If the pool is closed, the connection will be released * If the connection is not part of the busy queue, it will be released. * If {@link PoolProperties#testOnReturn} is set to true it will be validated * @param con PooledConnection to be returned to the pool */ protected void returnConnection(PooledConnection con) { if (isClosed()) { //if the connection pool is closed //close the connection instead of returning it release(con); return; } //end if if (con != null) { try { returnedCount.incrementAndGet(); con.lock(); if (con.isSuspect()) { if (poolProperties.isLogAbandoned() && log.isInfoEnabled()) { log.info("Connection(" + con + ") that has been marked suspect was returned." + " The processing time is " + (System.currentTimeMillis()-con.getTimestamp()) + " ms."); } if (jmxPool!=null) { jmxPool.notify(org.apache.tomcat.jdbc.pool.jmx.ConnectionPool.SUSPECT_RETURNED_NOTIFICATION, "Connection(" + con + ") that has been marked suspect was returned."); } } if (busy.remove(con)) { if (!shouldClose(con,PooledConnection.VALIDATE_RETURN)) { con.setStackTrace(null); con.setTimestamp(System.currentTimeMillis()); if (((idle.size()>=poolProperties.getMaxIdle()) && !poolProperties.isPoolSweeperEnabled()) || (!idle.offer(con))) { if (log.isDebugEnabled()) { log.debug("Connection ["+con+"] will be closed and not returned to the pool, idle["+idle.size()+"]>=maxIdle["+poolProperties.getMaxIdle()+"] idle.offer failed."); } release(con); } } else { if (log.isDebugEnabled()) { log.debug("Connection ["+con+"] will be closed and not returned to the pool."); } release(con); } //end if } else { if (log.isDebugEnabled()) { log.debug("Connection ["+con+"] will be closed and not returned to the pool, busy.remove failed."); } release(con); } } finally { con.unlock(); } } //end if } //checkIn
/** * Returns <code>true</code> if the connection pool is configured * to do validation for a certain action. * @param action The validation action */ private boolean doValidate(int action) { if (action == PooledConnection.VALIDATE_BORROW && poolProperties.isTestOnBorrow()) return true; else if (action == PooledConnection.VALIDATE_RETURN && poolProperties.isTestOnReturn()) return true; else if (action == PooledConnection.VALIDATE_IDLE && poolProperties.isTestWhileIdle()) return true; else if (action == PooledConnection.VALIDATE_INIT && poolProperties.isTestOnConnect()) return true; else if (action == PooledConnection.VALIDATE_INIT && poolProperties.getInitSQL()!=null) return true; else return false; }
想獲取最新內容,請關注微信公衆號
tomcat