Modified: tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java?rev=1833794&r1=1833793&r2=1833794&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java (original) +++ tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java Tue Jun 19 09:04:08 2018 @@ -24,7 +24,6 @@ import java.sql.SQLException; import java.util.Collection; import javax.management.InstanceAlreadyExistsException; -import javax.management.InstanceNotFoundException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.NotCompliantMBeanException; @@ -33,19 +32,14 @@ import javax.management.ObjectName; import org.apache.tomcat.dbcp.pool2.ObjectPool; /** - * A delegating connection that, rather than closing the underlying - * connection, returns itself to an {@link ObjectPool} when - * closed. + * A delegating connection that, rather than closing the underlying connection, returns itself to an {@link ObjectPool} + * when closed. * - * @author Rodney Waldhoff - * @author Glenn L. Nielsen - * @author James House * @since 2.0 */ -public class PoolableConnection extends DelegatingConnection<Connection> - implements PoolableConnectionMXBean { +public class PoolableConnection extends DelegatingConnection<Connection> implements PoolableConnectionMXBean { - private static MBeanServer MBEAN_SERVER = null; + private static MBeanServer MBEAN_SERVER; static { try { @@ -56,70 +50,75 @@ public class PoolableConnection extends } /** The pool to which I should return. */ - private final ObjectPool<PoolableConnection> _pool; + private final ObjectPool<PoolableConnection> pool; - private final ObjectName _jmxName; + private final ObjectNameWrapper jmxObjectName; // Use a prepared statement for validation, retaining the last used SQL to // check if the validation query has changed. - private PreparedStatement validationPreparedStatement = null; - private String lastValidationSql = null; + private PreparedStatement validationPreparedStatement; + private String lastValidationSql; /** - * Indicate that unrecoverable SQLException was thrown when using this connection. - * Such a connection should be considered broken and not pass validation in the future. + * Indicate that unrecoverable SQLException was thrown when using this connection. Such a connection should be + * considered broken and not pass validation in the future. */ - private boolean _fatalSqlExceptionThrown = false; + private boolean fatalSqlExceptionThrown = false; /** - * SQL_STATE codes considered to signal fatal conditions. Overrides the - * defaults in {@link Utils#DISCONNECTION_SQL_CODES} (plus anything starting - * with {@link Utils#DISCONNECTION_SQL_CODE_PREFIX}). + * SQL_STATE codes considered to signal fatal conditions. Overrides the defaults in + * {@link Utils#DISCONNECTION_SQL_CODES} (plus anything starting with {@link Utils#DISCONNECTION_SQL_CODE_PREFIX}). */ - private final Collection<String> _disconnectionSqlCodes; + private final Collection<String> disconnectionSqlCodes; /** Whether or not to fast fail validation after fatal connection errors */ - private final boolean _fastFailValidation; + private final boolean fastFailValidation; /** * - * @param conn my underlying connection - * @param pool the pool to which I should return when closed - * @param jmxName JMX name - * @param disconnectSqlCodes SQL_STATE codes considered fatal disconnection errors - * @param fastFailValidation true means fatal disconnection errors cause subsequent - * validations to fail immediately (no attempt to run query or isValid) + * @param conn + * my underlying connection + * @param pool + * the pool to which I should return when closed + * @param jmxObjectName + * JMX name + * @param disconnectSqlCodes + * SQL_STATE codes considered fatal disconnection errors + * @param fastFailValidation + * true means fatal disconnection errors cause subsequent validations to fail immediately (no attempt to + * run query or isValid) */ - public PoolableConnection(final Connection conn, - final ObjectPool<PoolableConnection> pool, final ObjectName jmxName, final Collection<String> disconnectSqlCodes, + public PoolableConnection(final Connection conn, final ObjectPool<PoolableConnection> pool, + final ObjectName jmxObjectName, final Collection<String> disconnectSqlCodes, final boolean fastFailValidation) { super(conn); - _pool = pool; - _jmxName = jmxName; - _disconnectionSqlCodes = disconnectSqlCodes; - _fastFailValidation = fastFailValidation; + this.pool = pool; + this.jmxObjectName = ObjectNameWrapper.wrap(jmxObjectName); + this.disconnectionSqlCodes = disconnectSqlCodes; + this.fastFailValidation = fastFailValidation; - if (jmxName != null) { + if (jmxObjectName != null) { try { - MBEAN_SERVER.registerMBean(this, jmxName); - } catch (InstanceAlreadyExistsException | - MBeanRegistrationException | NotCompliantMBeanException e) { + MBEAN_SERVER.registerMBean(this, jmxObjectName); + } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) { // For now, simply skip registration } } } /** - * - * @param conn my underlying connection - * @param pool the pool to which I should return when closed - * @param jmxName JMX name - */ - public PoolableConnection(final Connection conn, - final ObjectPool<PoolableConnection> pool, final ObjectName jmxName) { - this(conn, pool, jmxName, null, false); - } - + * + * @param conn + * my underlying connection + * @param pool + * the pool to which I should return when closed + * @param jmxName + * JMX name + */ + public PoolableConnection(final Connection conn, final ObjectPool<PoolableConnection> pool, + final ObjectName jmxName) { + this(conn, pool, jmxName, null, false); + } @Override protected void passivate() throws SQLException { @@ -127,14 +126,12 @@ public class PoolableConnection extends setClosedInternal(true); } - /** * {@inheritDoc} * <p> - * This method should not be used by a client to determine whether or not a - * connection should be return to the connection pool (by calling - * {@link #close()}). Clients should always attempt to return a connection - * to the pool once it is no longer required. + * This method should not be used by a client to determine whether or not a connection should be return to the + * connection pool (by calling {@link #close()}). Clients should always attempt to return a connection to the pool + * once it is no longer required. */ @Override public boolean isClosed() throws SQLException { @@ -153,11 +150,10 @@ public class PoolableConnection extends return false; } - /** * Returns me to my pool. */ - @Override + @Override public synchronized void close() throws SQLException { if (isClosedInternal()) { // already closed @@ -169,8 +165,8 @@ public class PoolableConnection extends isUnderlyingConnectionClosed = getDelegateInternal().isClosed(); } catch (final SQLException e) { try { - _pool.invalidateObject(this); - } catch(final IllegalStateException ise) { + pool.invalidateObject(this); + } catch (final IllegalStateException ise) { // pool is closed, so close the connection passivate(); getInnermostDelegate().close(); @@ -180,18 +176,17 @@ public class PoolableConnection extends throw new SQLException("Cannot close connection (isClosed check failed)", e); } - /* Can't set close before this code block since the connection needs to - * be open when validation runs. Can't set close after this code block - * since by then the connection will have been returned to the pool and - * may have been borrowed by another thread. Therefore, the close flag - * is set in passivate(). + /* + * Can't set close before this code block since the connection needs to be open when validation runs. Can't set + * close after this code block since by then the connection will have been returned to the pool and may have + * been borrowed by another thread. Therefore, the close flag is set in passivate(). */ if (isUnderlyingConnectionClosed) { // Abnormal close: underlying connection closed unexpectedly, so we // must destroy this proxy try { - _pool.invalidateObject(this); - } catch(final IllegalStateException e) { + pool.invalidateObject(this); + } catch (final IllegalStateException e) { // pool is closed, so close the connection passivate(); getInnermostDelegate().close(); @@ -202,16 +197,16 @@ public class PoolableConnection extends // Normal close: underlying connection is still open, so we // simply need to return this proxy to the pool try { - _pool.returnObject(this); - } catch(final IllegalStateException e) { + pool.returnObject(this); + } catch (final IllegalStateException e) { // pool is closed, so close the connection passivate(); getInnermostDelegate().close(); - } catch(final SQLException e) { + } catch (final SQLException e) { throw e; - } catch(final RuntimeException e) { + } catch (final RuntimeException e) { throw e; - } catch(final Exception e) { + } catch (final Exception e) { throw new SQLException("Cannot close connection (return to pool failed)", e); } } @@ -222,15 +217,10 @@ public class PoolableConnection extends */ @Override public void reallyClose() throws SQLException { - if (_jmxName != null) { - try { - MBEAN_SERVER.unregisterMBean(_jmxName); - } catch (MBeanRegistrationException | InstanceNotFoundException e) { - // Ignore - } + if (jmxObjectName != null) { + jmxObjectName.unregisterMBean(); } - if (validationPreparedStatement != null) { try { validationPreparedStatement.close(); @@ -242,10 +232,8 @@ public class PoolableConnection extends super.closeInternal(); } - /** - * Expose the {@link #toString()} method via a bean getter so it can be read - * as a property via JMX. + * Expose the {@link #toString()} method via a bean getter so it can be read as a property via JMX. */ @Override public String getToString() { @@ -255,31 +243,31 @@ public class PoolableConnection extends /** * Validates the connection, using the following algorithm: * <ol> - * <li>If {@code fastFailValidation} (constructor argument) is {@code true} and - * this connection has previously thrown a fatal disconnection exception, - * a {@code SQLException} is thrown. </li> - * <li>If {@code sql} is null, the driver's - * #{@link Connection#isValid(int) isValid(timeout)} is called. - * If it returns {@code false}, {@code SQLException} is thrown; - * otherwise, this method returns successfully.</li> - * <li>If {@code sql} is not null, it is executed as a query and if the resulting - * {@code ResultSet} contains at least one row, this method returns - * successfully. If not, {@code SQLException} is thrown.</li> + * <li>If {@code fastFailValidation} (constructor argument) is {@code true} and this connection has previously + * thrown a fatal disconnection exception, a {@code SQLException} is thrown.</li> + * <li>If {@code sql} is null, the driver's #{@link Connection#isValid(int) isValid(timeout)} is called. If it + * returns {@code false}, {@code SQLException} is thrown; otherwise, this method returns successfully.</li> + * <li>If {@code sql} is not null, it is executed as a query and if the resulting {@code ResultSet} contains at + * least one row, this method returns successfully. If not, {@code SQLException} is thrown.</li> * </ol> - * @param sql validation query - * @param timeout validation timeout - * @throws SQLException if validation fails or an SQLException occurs during validation + * + * @param sql + * The validation SQL query. + * @param timeoutSeconds + * The validation timeout in seconds. + * @throws SQLException + * Thrown when validation fails or an SQLException occurs during validation */ - public void validate(final String sql, int timeout) throws SQLException { - if (_fastFailValidation && _fatalSqlExceptionThrown) { + public void validate(final String sql, int timeoutSeconds) throws SQLException { + if (fastFailValidation && fatalSqlExceptionThrown) { throw new SQLException(Utils.getMessage("poolableConnection.validate.fastFail")); } if (sql == null || sql.length() == 0) { - if (timeout < 0) { - timeout = 0; + if (timeoutSeconds < 0) { + timeoutSeconds = 0; } - if (!isValid(timeout)) { + if (!isValid(timeoutSeconds)) { throw new SQLException("isValid() returned false"); } return; @@ -289,16 +277,15 @@ public class PoolableConnection extends lastValidationSql = sql; // Has to be the innermost delegate else the prepared statement will // be closed when the pooled connection is passivated. - validationPreparedStatement = - getInnermostDelegateInternal().prepareStatement(sql); + validationPreparedStatement = getInnermostDelegateInternal().prepareStatement(sql); } - if (timeout > 0) { - validationPreparedStatement.setQueryTimeout(timeout); + if (timeoutSeconds > 0) { + validationPreparedStatement.setQueryTimeout(timeoutSeconds); } try (ResultSet rs = validationPreparedStatement.executeQuery()) { - if(!rs.next()) { + if (!rs.next()) { throw new SQLException("validationQuery didn't return a row"); } } catch (final SQLException sqle) { @@ -306,24 +293,27 @@ public class PoolableConnection extends } } - /* + /** * Checks the SQLState of the input exception and any nested SQLExceptions it wraps. + * <p> + * If {@link #getDisconnectSqlCodes() disconnectSQLCodes} has been set, sql states are compared to those in the + * configured list of fatal exception codes. If this property is not set, codes are compared against the default + * codes in #{@link Utils.DISCONNECTION_SQL_CODES} and in this case anything starting with #{link + * Utils.DISCONNECTION_SQL_CODE_PREFIX} is considered a disconnection. + * </p> * - * If _disconnectSQLCodes has been set, sql states - * are compared to those in the configured list of fatal exception codes. If this - * property is not set, codes are compared against the default codes in - * Utils.DISCONNECTION_SQL_CODES and in this case anything starting with - * Utils.DISCONNECTION_SQL_CODE_PREFIX is considered a disconnection.</p> - * - * @param e SQLException to be examined + * @param e + * SQLException to be examined * @return true if the exception signals a disconnection */ private boolean isDisconnectionSqlException(final SQLException e) { boolean fatalException = false; final String sqlState = e.getSQLState(); if (sqlState != null) { - fatalException = _disconnectionSqlCodes == null ? sqlState.startsWith(Utils.DISCONNECTION_SQL_CODE_PREFIX) - || Utils.DISCONNECTION_SQL_CODES.contains(sqlState) : _disconnectionSqlCodes.contains(sqlState); + fatalException = disconnectionSqlCodes == null + ? sqlState.startsWith(Utils.DISCONNECTION_SQL_CODE_PREFIX) + || Utils.DISCONNECTION_SQL_CODES.contains(sqlState) + : disconnectionSqlCodes.contains(sqlState); if (!fatalException) { final SQLException nextException = e.getNextException(); if (nextException != null && nextException != e) { @@ -336,8 +326,7 @@ public class PoolableConnection extends @Override protected void handleException(final SQLException e) throws SQLException { - _fatalSqlExceptionThrown |= isDisconnectionSqlException(e); + fatalSqlExceptionThrown |= isDisconnectionSqlException(e); super.handleException(e); } } -
Modified: tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java?rev=1833794&r1=1833793&r2=1833794&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java (original) +++ tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java Tue Jun 19 09:04:08 2018 @@ -21,6 +21,7 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.Collection; +import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; import javax.management.ObjectName; @@ -36,149 +37,167 @@ import org.apache.tomcat.dbcp.pool2.impl import org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPoolConfig; /** - * A {@link PooledObjectFactory} that creates - * {@link PoolableConnection}s. + * A {@link PooledObjectFactory} that creates {@link PoolableConnection}s. * - * @author Rodney Waldhoff - * @author Glenn L. Nielsen - * @author James House - * @author Dirk Verbeeck * @since 2.0 */ -public class PoolableConnectionFactory - implements PooledObjectFactory<PoolableConnection> { +public class PoolableConnectionFactory implements PooledObjectFactory<PoolableConnection> { - private static final Log log = - LogFactory.getLog(PoolableConnectionFactory.class); + private static final Log log = LogFactory.getLog(PoolableConnectionFactory.class); /** - * Create a new {@code PoolableConnectionFactory}. - * @param connFactory the {@link ConnectionFactory} from which to obtain - * @param dataSourceJmxName The JMX name - * base {@link Connection}s + * Creates a new {@code PoolableConnectionFactory}. + * + * @param connFactory + * the {@link ConnectionFactory} from which to obtain base {@link Connection}s + * @param dataSourceJmxObjectName + * The JMX object name, may be null. */ - public PoolableConnectionFactory(final ConnectionFactory connFactory, - final ObjectName dataSourceJmxName) { - _connFactory = connFactory; - this.dataSourceJmxName = dataSourceJmxName; + public PoolableConnectionFactory(final ConnectionFactory connFactory, final ObjectName dataSourceJmxObjectName) { + this.connectionFactory = connFactory; + this.dataSourceJmxObjectName = dataSourceJmxObjectName; } /** - * Sets the query I use to {@link #validateObject validate} {@link Connection}s. - * Should return at least one row. If not specified, - * {@link Connection#isValid(int)} will be used to validate connections. + * Sets the query I use to {@link #validateObject validate} {@link Connection}s. Should return at least one row. If + * not specified, {@link Connection#isValid(int)} will be used to validate connections. * - * @param validationQuery a query to use to {@link #validateObject validate} {@link Connection}s. + * @param validationQuery + * a query to use to {@link #validateObject validate} {@link Connection}s. */ public void setValidationQuery(final String validationQuery) { - _validationQuery = validationQuery; + this.validationQuery = validationQuery; } /** - * Sets the validation query timeout, the amount of time, in seconds, that - * connection validation will wait for a response from the database when - * executing a validation query. Use a value less than or equal to 0 for - * no timeout. + * Sets the validation query timeout, the amount of time, in seconds, that connection validation will wait for a + * response from the database when executing a validation query. Use a value less than or equal to 0 for no timeout. * - * @param timeout new validation query timeout value in seconds + * @param validationQueryTimeoutSeconds + * new validation query timeout value in seconds */ - public void setValidationQueryTimeout(final int timeout) { - _validationQueryTimeout = timeout; + public void setValidationQueryTimeout(final int validationQueryTimeoutSeconds) { + this.validationQueryTimeoutSeconds = validationQueryTimeoutSeconds; } /** - * Sets the SQL statements I use to initialize newly created {@link Connection}s. - * Using {@code null} turns off connection initialization. - * @param connectionInitSqls SQL statement to initialize {@link Connection}s. + * Sets the SQL statements I use to initialize newly created {@link Connection}s. Using {@code null} turns off + * connection initialization. + * + * @param connectionInitSqls + * SQL statement to initialize {@link Connection}s. */ public void setConnectionInitSql(final Collection<String> connectionInitSqls) { - _connectionInitSqls = connectionInitSqls; + this.connectionInitSqls = connectionInitSqls; } /** * Sets the {@link ObjectPool} in which to pool {@link Connection}s. - * @param pool the {@link ObjectPool} in which to pool those {@link Connection}s + * + * @param pool + * the {@link ObjectPool} in which to pool those {@link Connection}s */ public synchronized void setPool(final ObjectPool<PoolableConnection> pool) { - if(null != _pool && pool != _pool) { + if (null != this.pool && pool != this.pool) { try { - _pool.close(); - } catch(final Exception e) { + this.pool.close(); + } catch (final Exception e) { // ignored !?! } } - _pool = pool; + this.pool = pool; } /** * Returns the {@link ObjectPool} in which {@link Connection}s are pooled. + * * @return the connection pool */ public synchronized ObjectPool<PoolableConnection> getPool() { - return _pool; + return pool; } /** * Sets the default "read only" setting for borrowed {@link Connection}s - * @param defaultReadOnly the default "read only" setting for borrowed {@link Connection}s + * + * @param defaultReadOnly + * the default "read only" setting for borrowed {@link Connection}s */ public void setDefaultReadOnly(final Boolean defaultReadOnly) { - _defaultReadOnly = defaultReadOnly; + this.defaultReadOnly = defaultReadOnly; } /** * Sets the default "auto commit" setting for borrowed {@link Connection}s - * @param defaultAutoCommit the default "auto commit" setting for borrowed {@link Connection}s + * + * @param defaultAutoCommit + * the default "auto commit" setting for borrowed {@link Connection}s */ public void setDefaultAutoCommit(final Boolean defaultAutoCommit) { - _defaultAutoCommit = defaultAutoCommit; + this.defaultAutoCommit = defaultAutoCommit; } /** * Sets the default "Transaction Isolation" setting for borrowed {@link Connection}s - * @param defaultTransactionIsolation the default "Transaction Isolation" setting for returned {@link Connection}s + * + * @param defaultTransactionIsolation + * the default "Transaction Isolation" setting for returned {@link Connection}s */ public void setDefaultTransactionIsolation(final int defaultTransactionIsolation) { - _defaultTransactionIsolation = defaultTransactionIsolation; + this.defaultTransactionIsolation = defaultTransactionIsolation; } /** * Sets the default "catalog" setting for borrowed {@link Connection}s - * @param defaultCatalog the default "catalog" setting for borrowed {@link Connection}s + * + * @param defaultCatalog + * the default "catalog" setting for borrowed {@link Connection}s */ public void setDefaultCatalog(final String defaultCatalog) { - _defaultCatalog = defaultCatalog; + this.defaultCatalog = defaultCatalog; } public void setCacheState(final boolean cacheState) { - this._cacheState = cacheState; + this.cacheState = cacheState; } public void setPoolStatements(final boolean poolStatements) { this.poolStatements = poolStatements; } + /** + * Deprecated due to typo in method name. + * + * @param maxOpenPreparedStatements + * The maximum number of open prepared statements. + * @deprecated Use {@link #setMaxOpenPreparedStatements(int)}. + */ @Deprecated // Due to typo in method name. public void setMaxOpenPrepatedStatements(final int maxOpenPreparedStatements) { setMaxOpenPreparedStatements(maxOpenPreparedStatements); } + /** + * Sets the maximum number of open prepared statements. + * + * @param maxOpenPreparedStatements + * The maximum number of open prepared statements. + */ public void setMaxOpenPreparedStatements(final int maxOpenPreparedStatements) { this.maxOpenPreparedStatements = maxOpenPreparedStatements; } /** - * Sets the maximum lifetime in milliseconds of a connection after which the - * connection will always fail activation, passivation and validation. A - * value of zero or less indicates an infinite lifetime. The default value - * is -1. - * @param maxConnLifetimeMillis The maximum connection lifetime + * Sets the maximum lifetime in milliseconds of a connection after which the connection will always fail activation, + * passivation and validation. A value of zero or less indicates an infinite lifetime. The default value is -1. + * + * @param maxConnLifetimeMillis + * The maximum lifetime in milliseconds. */ public void setMaxConnLifetimeMillis(final long maxConnLifetimeMillis) { this.maxConnLifetimeMillis = maxConnLifetimeMillis; } - public boolean isEnableAutoCommitOnReturn() { return enableAutoCommitOnReturn; } @@ -187,7 +206,6 @@ public class PoolableConnectionFactory this.enableAutoCommitOnReturn = enableAutoCommitOnReturn; } - public boolean isRollbackOnReturn() { return rollbackOnReturn; } @@ -197,69 +215,68 @@ public class PoolableConnectionFactory } public Integer getDefaultQueryTimeout() { - return defaultQueryTimeout; + return defaultQueryTimeoutSeconds; } - public void setDefaultQueryTimeout(final Integer defaultQueryTimeout) { - this.defaultQueryTimeout = defaultQueryTimeout; + public void setDefaultQueryTimeout(final Integer defaultQueryTimeoutSeconds) { + this.defaultQueryTimeoutSeconds = defaultQueryTimeoutSeconds; } /** * SQL_STATE codes considered to signal fatal conditions. * <p> - * Overrides the defaults in {@link Utils#DISCONNECTION_SQL_CODES} - * (plus anything starting with {@link Utils#DISCONNECTION_SQL_CODE_PREFIX}). - * If this property is non-null and {@link #isFastFailValidation()} is - * {@code true}, whenever connections created by this factory generate exceptions - * with SQL_STATE codes in this list, they will be marked as "fatally disconnected" - * and subsequent validations will fail fast (no attempt at isValid or validation - * query).</p> + * Overrides the defaults in {@link Utils#DISCONNECTION_SQL_CODES} (plus anything starting with + * {@link Utils#DISCONNECTION_SQL_CODE_PREFIX}). If this property is non-null and {@link #isFastFailValidation()} is + * {@code true}, whenever connections created by this factory generate exceptions with SQL_STATE codes in this list, + * they will be marked as "fatally disconnected" and subsequent validations will fail fast (no attempt at isValid or + * validation query). + * </p> * <p> - * If {@link #isFastFailValidation()} is {@code false} setting this property has no - * effect.</p> + * If {@link #isFastFailValidation()} is {@code false} setting this property has no effect. + * </p> * * @return SQL_STATE codes overriding defaults * @since 2.1 */ public Collection<String> getDisconnectionSqlCodes() { - return _disconnectionSqlCodes; + return disconnectionSqlCodes; } /** + * @param disconnectionSqlCodes + * The disconnection SQL codes. * @see #getDisconnectionSqlCodes() - * @param disconnectionSqlCodes The disconnection codes * @since 2.1 */ public void setDisconnectionSqlCodes(final Collection<String> disconnectionSqlCodes) { - _disconnectionSqlCodes = disconnectionSqlCodes; + this.disconnectionSqlCodes = disconnectionSqlCodes; } /** - * True means that validation will fail immediately for connections that - * have previously thrown SQLExceptions with SQL_STATE indicating fatal - * disconnection errors. + * True means that validation will fail immediately for connections that have previously thrown SQLExceptions with + * SQL_STATE indicating fatal disconnection errors. * * @return true if connections created by this factory will fast fail validation. * @see #setDisconnectionSqlCodes(Collection) * @since 2.1 */ public boolean isFastFailValidation() { - return _fastFailValidation; + return fastFailValidation; } /** * @see #isFastFailValidation() - * @param fastFailValidation true means connections created by this factory will - * fast fail validation + * @param fastFailValidation + * true means connections created by this factory will fast fail validation * @since 2.1 */ public void setFastFailValidation(final boolean fastFailValidation) { - _fastFailValidation = fastFailValidation; + this.fastFailValidation = fastFailValidation; } @Override public PooledObject<PoolableConnection> makeObject() throws Exception { - Connection conn = _connFactory.createConnection(); + Connection conn = connectionFactory.createConnection(); if (conn == null) { throw new IllegalStateException("Connection factory returned null from createConnection"); } @@ -278,16 +295,16 @@ public class PoolableConnectionFactory final long connIndex = connectionIndex.getAndIncrement(); - if(poolStatements) { + if (poolStatements) { conn = new PoolingConnection(conn); - final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>(); + final GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig(); config.setMaxTotalPerKey(-1); config.setBlockWhenExhausted(false); config.setMaxWaitMillis(0); config.setMaxIdlePerKey(1); config.setMaxTotal(maxOpenPreparedStatements); - if (dataSourceJmxName != null) { - final StringBuilder base = new StringBuilder(dataSourceJmxName.toString()); + if (dataSourceJmxObjectName != null) { + final StringBuilder base = new StringBuilder(dataSourceJmxObjectName.toString()); base.append(Constants.JMX_CONNECTION_BASE_EXT); base.append(Long.toString(connIndex)); config.setJmxNameBase(base.toString()); @@ -295,40 +312,37 @@ public class PoolableConnectionFactory } else { config.setJmxEnabled(false); } - final KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> stmtPool = - new GenericKeyedObjectPool<>((PoolingConnection)conn, config); - ((PoolingConnection)conn).setStatementPool(stmtPool); - ((PoolingConnection) conn).setCacheState(_cacheState); + final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>( + (PoolingConnection) conn, config); + ((PoolingConnection) conn).setStatementPool(stmtPool); + ((PoolingConnection) conn).setCacheState(cacheState); } // Register this connection with JMX ObjectName connJmxName; - if (dataSourceJmxName == null) { + if (dataSourceJmxObjectName == null) { connJmxName = null; } else { - connJmxName = new ObjectName(dataSourceJmxName.toString() + - Constants.JMX_CONNECTION_BASE_EXT + connIndex); + connJmxName = new ObjectName( + dataSourceJmxObjectName.toString() + Constants.JMX_CONNECTION_BASE_EXT + connIndex); } - final PoolableConnection pc = new PoolableConnection(conn, _pool, connJmxName, - _disconnectionSqlCodes, _fastFailValidation); - pc.setCacheState(_cacheState); + final PoolableConnection pc = new PoolableConnection(conn, pool, connJmxName, disconnectionSqlCodes, + fastFailValidation); + pc.setCacheState(cacheState); return new DefaultPooledObject<>(pc); } protected void initializeConnection(final Connection conn) throws SQLException { - final Collection<String> sqls = _connectionInitSqls; - if(conn.isClosed()) { + final Collection<String> sqls = connectionInitSqls; + if (conn.isClosed()) { throw new SQLException("initializeConnection: connection closed"); } - if(null != sqls) { + if (null != sqls) { try (Statement stmt = conn.createStatement()) { for (final String sql : sqls) { - if (sql == null) { - throw new NullPointerException( - "null connectionInitSqls element"); - } + Objects.requireNonNull(sql, "null connectionInitSqls element"); stmt.execute(sql); } } @@ -336,8 +350,7 @@ public class PoolableConnectionFactory } @Override - public void destroyObject(final PooledObject<PoolableConnection> p) - throws Exception { + public void destroyObject(final PooledObject<PoolableConnection> p) throws Exception { p.getObject().reallyClose(); } @@ -350,23 +363,21 @@ public class PoolableConnectionFactory return true; } catch (final Exception e) { if (log.isDebugEnabled()) { - log.debug(Utils.getMessage( - "poolableConnectionFactory.validateObject.fail"), e); + log.debug(Utils.getMessage("poolableConnectionFactory.validateObject.fail"), e); } return false; } } public void validateConnection(final PoolableConnection conn) throws SQLException { - if(conn.isClosed()) { + if (conn.isClosed()) { throw new SQLException("validateConnection: connection closed"); } - conn.validate(_validationQuery, _validationQueryTimeout); + conn.validate(validationQuery, validationQueryTimeoutSeconds); } @Override - public void passivateObject(final PooledObject<PoolableConnection> p) - throws Exception { + public void passivateObject(final PooledObject<PoolableConnection> p) throws Exception { validateLifetime(p); @@ -374,7 +385,7 @@ public class PoolableConnectionFactory Boolean connAutoCommit = null; if (rollbackOnReturn) { connAutoCommit = Boolean.valueOf(conn.getAutoCommit()); - if(!connAutoCommit.booleanValue() && !conn.isReadOnly()) { + if (!connAutoCommit.booleanValue() && !conn.isReadOnly()) { conn.rollback(); } } @@ -387,7 +398,7 @@ public class PoolableConnectionFactory if (connAutoCommit == null) { connAutoCommit = Boolean.valueOf(conn.getAutoCommit()); } - if(!connAutoCommit.booleanValue()) { + if (!connAutoCommit.booleanValue()) { conn.setAutoCommit(true); } } @@ -396,48 +407,41 @@ public class PoolableConnectionFactory } @Override - public void activateObject(final PooledObject<PoolableConnection> p) - throws Exception { + public void activateObject(final PooledObject<PoolableConnection> p) throws Exception { validateLifetime(p); final PoolableConnection conn = p.getObject(); conn.activate(); - if (_defaultAutoCommit != null && - conn.getAutoCommit() != _defaultAutoCommit.booleanValue()) { - conn.setAutoCommit(_defaultAutoCommit.booleanValue()); + if (defaultAutoCommit != null && conn.getAutoCommit() != defaultAutoCommit.booleanValue()) { + conn.setAutoCommit(defaultAutoCommit.booleanValue()); } - if (_defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION && - conn.getTransactionIsolation() != _defaultTransactionIsolation) { - conn.setTransactionIsolation(_defaultTransactionIsolation); + if (defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION + && conn.getTransactionIsolation() != defaultTransactionIsolation) { + conn.setTransactionIsolation(defaultTransactionIsolation); } - if (_defaultReadOnly != null && - conn.isReadOnly() != _defaultReadOnly.booleanValue()) { - conn.setReadOnly(_defaultReadOnly.booleanValue()); + if (defaultReadOnly != null && conn.isReadOnly() != defaultReadOnly.booleanValue()) { + conn.setReadOnly(defaultReadOnly.booleanValue()); } - if (_defaultCatalog != null && - !_defaultCatalog.equals(conn.getCatalog())) { - conn.setCatalog(_defaultCatalog); + if (defaultCatalog != null && !defaultCatalog.equals(conn.getCatalog())) { + conn.setCatalog(defaultCatalog); } - conn.setDefaultQueryTimeout(defaultQueryTimeout); + conn.setDefaultQueryTimeout(defaultQueryTimeoutSeconds); } - private void validateLifetime(final PooledObject<PoolableConnection> p) - throws Exception { + private void validateLifetime(final PooledObject<PoolableConnection> p) throws Exception { if (maxConnLifetimeMillis > 0) { final long lifetime = System.currentTimeMillis() - p.getCreateTime(); if (lifetime > maxConnLifetimeMillis) { - throw new LifetimeExceededException(Utils.getMessage( - "connectionFactory.lifetimeExceeded", - Long.valueOf(lifetime), - Long.valueOf(maxConnLifetimeMillis))); + throw new LifetimeExceededException(Utils.getMessage("connectionFactory.lifetimeExceeded", + Long.valueOf(lifetime), Long.valueOf(maxConnLifetimeMillis))); } } } protected ConnectionFactory getConnectionFactory() { - return _connFactory; + return connectionFactory; } protected boolean getPoolStatements() { @@ -449,38 +453,37 @@ public class PoolableConnectionFactory } protected boolean getCacheState() { - return _cacheState; + return cacheState; } protected ObjectName getDataSourceJmxName() { - return dataSourceJmxName; + return dataSourceJmxObjectName; } protected AtomicLong getConnectionIndex() { return connectionIndex; } - private final ConnectionFactory _connFactory; - private final ObjectName dataSourceJmxName; - private volatile String _validationQuery = null; - private volatile int _validationQueryTimeout = -1; - private Collection<String> _connectionInitSqls = null; - private Collection<String> _disconnectionSqlCodes = null; - private boolean _fastFailValidation = false; - private volatile ObjectPool<PoolableConnection> _pool = null; - private Boolean _defaultReadOnly = null; - private Boolean _defaultAutoCommit = null; + private final ConnectionFactory connectionFactory; + private final ObjectName dataSourceJmxObjectName; + private volatile String validationQuery; + private volatile int validationQueryTimeoutSeconds = -1; + private Collection<String> connectionInitSqls; + private Collection<String> disconnectionSqlCodes; + private boolean fastFailValidation; + private volatile ObjectPool<PoolableConnection> pool; + private Boolean defaultReadOnly; + private Boolean defaultAutoCommit; private boolean enableAutoCommitOnReturn = true; private boolean rollbackOnReturn = true; - private int _defaultTransactionIsolation = UNKNOWN_TRANSACTIONISOLATION; - private String _defaultCatalog; - private boolean _cacheState; - private boolean poolStatements = false; - private int maxOpenPreparedStatements = - GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY; + private int defaultTransactionIsolation = UNKNOWN_TRANSACTIONISOLATION; + private String defaultCatalog; + private boolean cacheState; + private boolean poolStatements; + private int maxOpenPreparedStatements = GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY; private long maxConnLifetimeMillis = -1; private final AtomicLong connectionIndex = new AtomicLong(0); - private Integer defaultQueryTimeout = null; + private Integer defaultQueryTimeoutSeconds; /** * Internal constant to indicate the level is not set. Modified: tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java?rev=1833794&r1=1833793&r2=1833794&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java (original) +++ tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java Tue Jun 19 09:04:08 2018 @@ -19,41 +19,52 @@ package org.apache.tomcat.dbcp.dbcp2; import java.sql.SQLException; /** - * Defines the attributes and methods that will be exposed via JMX for - * {@link PoolableConnection} instances. + * Defines the attributes and methods that will be exposed via JMX for {@link PoolableConnection} instances. + * * @since 2.0 */ public interface PoolableConnectionMXBean { // Read-only properties boolean isClosed() throws SQLException; - //SQLWarning getWarnings() throws SQLException; + + // SQLWarning getWarnings() throws SQLException; String getToString(); // Read-write properties boolean getAutoCommit() throws SQLException; + void setAutoCommit(boolean autoCommit) throws SQLException; boolean getCacheState(); + void setCacheState(boolean cacheState); String getCatalog() throws SQLException; + void setCatalog(String catalog) throws SQLException; int getHoldability() throws SQLException; + void setHoldability(int holdability) throws SQLException; boolean isReadOnly() throws SQLException; + void setReadOnly(boolean readOnly) throws SQLException; String getSchema() throws SQLException; + void setSchema(String schema) throws SQLException; int getTransactionIsolation() throws SQLException; + void setTransactionIsolation(int level) throws SQLException; // Methods void clearCachedState(); + void clearWarnings() throws SQLException; + void close() throws SQLException; + void reallyClose() throws SQLException; } Modified: tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java?rev=1833794&r1=1833793&r2=1833794&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java (original) +++ tomcat/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java Tue Jun 19 09:04:08 2018 @@ -25,50 +25,51 @@ import java.util.List; import org.apache.tomcat.dbcp.pool2.KeyedObjectPool; /** - * A {@link DelegatingPreparedStatement} that cooperates with - * {@link PoolingConnection} to implement a pool of {@link PreparedStatement}s. + * A {@link DelegatingPreparedStatement} that cooperates with {@link PoolingConnection} to implement a pool of + * {@link PreparedStatement}s. * <p> * My {@link #close} method returns me to my containing pool. (See {@link PoolingConnection}.) * - * @param <K> the key type + * @param <K> + * the key type * * @see PoolingConnection - * @author Rodney Waldhoff - * @author Glenn L. Nielsen - * @author James House - * @author Dirk Verbeeck * @since 2.0 */ public class PoolablePreparedStatement<K> extends DelegatingPreparedStatement { /** * The {@link KeyedObjectPool} from which I was obtained. */ - private final KeyedObjectPool<K, PoolablePreparedStatement<K>> _pool; + private final KeyedObjectPool<K, PoolablePreparedStatement<K>> pool; /** * My "key" as used by {@link KeyedObjectPool}. */ - private final K _key; + private final K key; private volatile boolean batchAdded = false; /** * Constructor - * @param stmt my underlying {@link PreparedStatement} - * @param key my key" as used by {@link KeyedObjectPool} - * @param pool the {@link KeyedObjectPool} from which I was obtained. - * @param conn the {@link java.sql.Connection Connection} from which I was created + * + * @param stmt + * my underlying {@link PreparedStatement} + * @param key + * my key" as used by {@link KeyedObjectPool} + * @param pool + * the {@link KeyedObjectPool} from which I was obtained. + * @param conn + * the {@link java.sql.Connection Connection} from which I was created */ public PoolablePreparedStatement(final PreparedStatement stmt, final K key, - final KeyedObjectPool<K, PoolablePreparedStatement<K>> pool, - final DelegatingConnection<?> conn) { + final KeyedObjectPool<K, PoolablePreparedStatement<K>> pool, final DelegatingConnection<?> conn) { super(conn, stmt); - _pool = pool; - _key = key; + this.pool = pool; + this.key = key; // Remove from trace now because this statement will be // added by the activate method. - if(getConnectionInternal() != null) { + if (getConnectionInternal() != null) { getConnectionInternal().removeTrace(this); } } @@ -99,21 +100,21 @@ public class PoolablePreparedStatement<K // calling close twice should have no effect if (!isClosed()) { try { - _pool.returnObject(_key, this); - } catch(final SQLException e) { + pool.returnObject(key, this); + } catch (final SQLException e) { throw e; - } catch(final RuntimeException e) { + } catch (final RuntimeException e) { throw e; - } catch(final Exception e) { + } catch (final Exception e) { throw new SQLException("Cannot close preparedstatement (return to pool failed)", e); } } } @Override - public void activate() throws SQLException{ + public void activate() throws SQLException { setClosedInternal(false); - if(getConnectionInternal() != null) { + if (getConnectionInternal() != null) { getConnectionInternal().addTrace(this); } super.activate(); @@ -127,7 +128,7 @@ public class PoolablePreparedStatement<K clearBatch(); } setClosedInternal(true); - if(getConnectionInternal() != null) { + if (getConnectionInternal() != null) { getConnectionInternal().removeTrace(this); } @@ -136,7 +137,7 @@ public class PoolablePreparedStatement<K // FIXME The PreparedStatement we're wrapping should handle this for us. // See bug 17301 for what could happen when ResultSets are closed twice. final List<AbandonedTrace> resultSets = getTrace(); - if( resultSets != null) { + if (resultSets != null) { final ResultSet[] set = resultSets.toArray(new ResultSet[resultSets.size()]); for (final ResultSet element : set) { element.close(); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org