Modified: tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java?rev=1833816&r1=1833815&r2=1833816&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java (original) +++ tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnection.java Tue Jun 19 11:21:13 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) { @@ -309,21 +296,24 @@ 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 {@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> * - * @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/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java?rev=1833816&r1=1833815&r2=1833816&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java (original) +++ tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionFactory.java Tue Jun 19 11:21:13 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,147 +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 - * 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. + * 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; } @@ -185,7 +206,6 @@ public class PoolableConnectionFactory this.enableAutoCommitOnReturn = enableAutoCommitOnReturn; } - public boolean isRollbackOnReturn() { return rollbackOnReturn; } @@ -195,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; } /** - * @see #getDisconnectionSqlCodes() * @param disconnectionSqlCodes + * The disconnection SQL codes. + * @see #getDisconnectionSqlCodes() * @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"); } @@ -276,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()); @@ -293,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); } } @@ -334,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(); } @@ -348,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); @@ -372,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(); } } @@ -385,7 +398,7 @@ public class PoolableConnectionFactory if (connAutoCommit == null) { connAutoCommit = Boolean.valueOf(conn.getAutoCommit()); } - if(!connAutoCommit.booleanValue()) { + if (!connAutoCommit.booleanValue()) { conn.setAutoCommit(true); } } @@ -394,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() { @@ -447,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/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java?rev=1833816&r1=1833815&r2=1833816&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java (original) +++ tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolableConnectionMXBean.java Tue Jun 19 11:21:13 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/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java?rev=1833816&r1=1833815&r2=1833816&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java (original) +++ tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolablePreparedStatement.java Tue Jun 19 11:21:13 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(); Modified: tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolingConnection.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolingConnection.java?rev=1833816&r1=1833815&r2=1833816&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolingConnection.java (original) +++ tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/PoolingConnection.java Tue Jun 19 11:21:13 2018 @@ -31,56 +31,79 @@ import org.apache.tomcat.dbcp.pool2.impl /** * A {@link DelegatingConnection} that pools {@link PreparedStatement}s. * <p> - * The {@link #prepareStatement} and {@link #prepareCall} methods, rather than - * creating a new PreparedStatement each time, may actually pull the statement - * from a pool of unused statements. - * The {@link PreparedStatement#close} method of the returned statement doesn't - * actually close the statement, but rather returns it to the pool. - * (See {@link PoolablePreparedStatement}, {@link PoolableCallableStatement}.) - * + * The {@link #prepareStatement} and {@link #prepareCall} methods, rather than creating a new PreparedStatement each + * time, may actually pull the statement from a pool of unused statements. The {@link PreparedStatement#close} method of + * the returned statement doesn't actually close the statement, but rather returns it to the pool. (See + * {@link PoolablePreparedStatement}, {@link PoolableCallableStatement}.) + * </p> * * @see PoolablePreparedStatement - * @author Rodney Waldhoff - * @author Dirk Verbeeck * @since 2.0 */ public class PoolingConnection extends DelegatingConnection<Connection> - implements KeyedPooledObjectFactory<PStmtKey,DelegatingPreparedStatement> { + implements KeyedPooledObjectFactory<PStmtKey, DelegatingPreparedStatement> { + + /** + * Statement types. + * + * @since 2.0 protected enum. + * @since 2.4.0 public enum. + */ + public enum StatementType { + + /** + * Callable statement. + */ + CALLABLE_STATEMENT, + + /** + * Prepared statement. + */ + PREPARED_STATEMENT + } /** Pool of {@link PreparedStatement}s. and {@link CallableStatement}s */ - private KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> _pstmtPool = null; + private KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pstmtPool; /** * Constructor. - * @param c the underlying {@link Connection}. + * + * @param connection + * the underlying {@link Connection}. */ - public PoolingConnection(final Connection c) { - super(c); + public PoolingConnection(final Connection connection) { + super(connection); } - - public void setStatementPool( - final KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> pool) { - _pstmtPool = pool; + /** + * {@link KeyedPooledObjectFactory} method for activating pooled statements. + * + * @param key + * ignored + * @param pooledObject + * wrapped pooled statement to be activated + */ + @Override + public void activateObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject) + throws Exception { + pooledObject.getObject().activate(); } - /** - * Close and free all {@link PreparedStatement}s or - * {@link CallableStatement}s from the pool, and close the underlying - * connection. + * Closes and frees all {@link PreparedStatement}s or {@link CallableStatement}s from the pool, and close the + * underlying connection. */ @Override public synchronized void close() throws SQLException { try { - if (null != _pstmtPool) { - final KeyedObjectPool<PStmtKey,DelegatingPreparedStatement> oldpool = _pstmtPool; - _pstmtPool = null; + if (null != pstmtPool) { + final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> oldpool = pstmtPool; + pstmtPool = null; try { oldpool.close(); - } catch(final RuntimeException e) { + } catch (final RuntimeException e) { throw e; - } catch(final Exception e) { + } catch (final Exception e) { throw new SQLException("Cannot close connection", e); } } @@ -94,343 +117,166 @@ public class PoolingConnection extends D } /** - * Create or obtain a {@link PreparedStatement} from the pool. - * @param sql the sql string used to define the PreparedStatement - * @return a {@link PoolablePreparedStatement} - */ - @Override - public PreparedStatement prepareStatement(final String sql) throws SQLException { - if (null == _pstmtPool) { - throw new SQLException( - "Statement pool is null - closed or invalid PoolingConnection."); - } - try { - return _pstmtPool.borrowObject(createKey(sql)); - } catch(final NoSuchElementException e) { - throw new SQLException("MaxOpenPreparedStatements limit reached", e); - } catch(final RuntimeException e) { - throw e; - } catch(final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } - } - - @Override - public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException { - if (null == _pstmtPool) { - throw new SQLException( - "Statement pool is null - closed or invalid PoolingConnection."); - } - try { - return _pstmtPool.borrowObject(createKey(sql, autoGeneratedKeys)); - } - catch (final NoSuchElementException e) { - throw new SQLException("MaxOpenPreparedStatements limit reached", e); - } - catch (final RuntimeException e) { - throw e; - } - catch (final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } - } - - /** - * Create or obtain a {@link PreparedStatement} from the pool. - * @param sql the sql string used to define the PreparedStatement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - * @return a {@link PoolablePreparedStatement} - */ - @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { - if (null == _pstmtPool) { - throw new SQLException( - "Statement pool is null - closed or invalid PoolingConnection."); - } - try { - return _pstmtPool.borrowObject(createKey(sql,resultSetType,resultSetConcurrency)); - } catch(final NoSuchElementException e) { - throw new SQLException("MaxOpenPreparedStatements limit reached", e); - } catch(final RuntimeException e) { - throw e; - } catch(final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } - } - - /** - * Create or obtain a {@link CallableStatement} from the pool. - * @param sql the sql string used to define the CallableStatement - * @return a {@link PoolableCallableStatement} - * @throws SQLException - */ - @Override - public CallableStatement prepareCall(final String sql) throws SQLException { - try { - return (CallableStatement) _pstmtPool.borrowObject(createKey(sql, StatementType.CALLABLE_STATEMENT)); - } catch (final NoSuchElementException e) { - throw new SQLException("MaxOpenCallableStatements limit reached", e); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow callableStatement from pool failed", e); - } - } - - /** - * Create or obtain a {@link CallableStatement} from the pool. - * @param sql the sql string used to define the CallableStatement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - * @return a {@link PoolableCallableStatement} - * @throws SQLException - */ - @Override - public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { - try { - return (CallableStatement) _pstmtPool.borrowObject(createKey(sql, resultSetType, - resultSetConcurrency, StatementType.CALLABLE_STATEMENT)); - } catch (final NoSuchElementException e) { - throw new SQLException("MaxOpenCallableStatements limit reached", e); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow callableStatement from pool failed", e); - } - } - - /** - * Create or obtain a {@link PreparedStatement} from the pool. - * @param sql the sql string used to define the PreparedStatement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - * @param resultSetHoldability result set holdability - * @return a {@link PoolablePreparedStatement} - */ - @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, - final int resultSetConcurrency, final int resultSetHoldability) throws SQLException { - if (null == _pstmtPool) { - throw new SQLException( - "Statement pool is null - closed or invalid PoolingConnection."); - } - try { - return _pstmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); - } catch(final NoSuchElementException e) { - throw new SQLException("MaxOpenPreparedStatements limit reached", e); - } catch(final RuntimeException e) { - throw e; - } catch(final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } - } - - /** - * Create or obtain a {@link PreparedStatement} from the pool. - * @param sql the sql string used to define the PreparedStatement - * @param columnIndexes column indexes - * @return a {@link PoolablePreparedStatement} - */ - @Override - public PreparedStatement prepareStatement(final String sql, final int columnIndexes[]) - throws SQLException { - if (null == _pstmtPool) { - throw new SQLException( - "Statement pool is null - closed or invalid PoolingConnection."); - } - try { - return _pstmtPool.borrowObject(createKey(sql, columnIndexes)); - } catch(final NoSuchElementException e) { - throw new SQLException("MaxOpenPreparedStatements limit reached", e); - } catch(final RuntimeException e) { - throw e; - } catch(final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } - } - - /** - * Create or obtain a {@link PreparedStatement} from the pool. - * @param sql the sql string used to define the PreparedStatement - * @param columnNames column names - * @return a {@link PoolablePreparedStatement} - */ - @Override - public PreparedStatement prepareStatement(final String sql, final String columnNames[]) - throws SQLException { - if (null == _pstmtPool) { - throw new SQLException( - "Statement pool is null - closed or invalid PoolingConnection."); - } - try { - return _pstmtPool.borrowObject(createKey(sql, columnNames)); - } catch(final NoSuchElementException e) { - throw new SQLException("MaxOpenPreparedStatements limit reached", e); - } catch(final RuntimeException e) { - throw e; - } catch(final Exception e) { - throw new SQLException("Borrow prepareStatement from pool failed", e); - } - } - - /** - * Create or obtain a {@link CallableStatement} from the pool. - * @param sql the sql string used to define the CallableStatement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - * @param resultSetHoldability result set holdability - * @return a {@link PoolableCallableStatement} - * @throws SQLException if a {@link CallableStatement} cannot be obtained - * from the pool + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement */ - @Override - public CallableStatement prepareCall(final String sql, final int resultSetType, - final int resultSetConcurrency, final int resultSetHoldability) throws SQLException { - try { - return (CallableStatement) _pstmtPool.borrowObject(createKey(sql, resultSetType, - resultSetConcurrency, resultSetHoldability, StatementType.CALLABLE_STATEMENT)); - } catch (final NoSuchElementException e) { - throw new SQLException("MaxOpenCallableStatements limit reached", e); - } catch (final RuntimeException e) { - throw e; - } catch (final Exception e) { - throw new SQLException("Borrow callableStatement from pool failed", e); - } - } - - protected PStmtKey createKey(final String sql, final int autoGeneratedKeys) { + protected PStmtKey createKey(final String sql) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog, autoGeneratedKeys); + return new PStmtKey(normalizeSQL(sql), catalog); } - /** - * Create a PStmtKey for the given arguments. - * @param sql the sql string used to define the statement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - */ - protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency) { + protected PStmtKey createKey(final String sql, final int autoGeneratedKeys) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency); + return new PStmtKey(normalizeSQL(sql), catalog, autoGeneratedKeys); } /** - * Create a PStmtKey for the given arguments. - * @param sql the sql string used to define the statement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - * @param stmtType statement type + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement + * @param columnIndexes + * column indexes */ - protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency, final StatementType stmtType) { + protected PStmtKey createKey(final String sql, final int columnIndexes[]) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, stmtType); + return new PStmtKey(normalizeSQL(sql), catalog, columnIndexes); } /** - * Create a PStmtKey for the given arguments. - * @param sql the sql string used to define the statement + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency */ - protected PStmtKey createKey(final String sql) { + protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog); + return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency); } /** - * Create a PStmtKey for the given arguments. - * @param sql the SQL string used to define the statement - * @param stmtType statement type + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency + * @param resultSetHoldability + * result set holdability */ - protected PStmtKey createKey(final String sql, final StatementType stmtType) { + protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog, stmtType, null); + return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, resultSetHoldability); } /** - * Create a PStmtKey for the given arguments. - * @param sql the sql string used to define the statement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - * @param resultSetHoldability result set holdability - * @return a newly created key for the given arguments + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency + * @param resultSetHoldability + * result set holdability + * @param stmtType + * statement type */ protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability) { + final int resultSetHoldability, final StatementType stmtType) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, resultSetHoldability); + return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, resultSetHoldability, + stmtType); } /** - * Create a PStmtKey for the given arguments. - * @param sql the sql string used to define the statement - * @param resultSetType result set type - * @param resultSetConcurrency result set concurrency - * @param resultSetHoldability result set holdability - * @param stmtType statement type - * @return a newly created key for the given arguments + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency + * @param stmtType + * statement type */ protected PStmtKey createKey(final String sql, final int resultSetType, final int resultSetConcurrency, - final int resultSetHoldability, final StatementType stmtType) { + final StatementType stmtType) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, resultSetHoldability, stmtType); + return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency, stmtType); } /** - * Create a PStmtKey for the given arguments. - * @param sql the sql string used to define the statement - * @param columnIndexes column indexes - * @return a newly created key for the given arguments + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement + * @param stmtType + * statement type */ - protected PStmtKey createKey(final String sql, final int columnIndexes[]) { + protected PStmtKey createKey(final String sql, final StatementType stmtType) { String catalog = null; try { catalog = getCatalog(); } catch (final SQLException e) { // Ignored } - return new PStmtKey(normalizeSQL(sql), catalog, columnIndexes); + return new PStmtKey(normalizeSQL(sql), catalog, stmtType, null); } /** - * Create a PStmtKey for the given arguments. - * @param sql the sql string used to define the statement - * @param columnNames column names - * @return a newly created key for the given arguments + * Creates a PStmtKey for the given arguments. + * + * @param sql + * the SQL string used to define the statement + * @param columnNames + * column names */ protected PStmtKey createKey(final String sql, final String columnNames[]) { String catalog = null; @@ -443,112 +289,327 @@ public class PoolingConnection extends D } /** - * Normalize the given SQL statement, producing a - * canonical form that is semantically equivalent to the original. + * {@link KeyedPooledObjectFactory} method for destroying PoolablePreparedStatements and PoolableCallableStatements. + * Closes the underlying statement. + * + * @param key + * ignored + * @param pooledObject + * the wrapped pooled statement to be destroyed. */ - protected String normalizeSQL(final String sql) { - return sql.trim(); + @Override + public void destroyObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject) + throws Exception { + pooledObject.getObject().getInnermostDelegate().close(); } /** - * {@link KeyedPooledObjectFactory} method for creating - * {@link PoolablePreparedStatement}s or {@link PoolableCallableStatement}s. - * The <code>stmtType</code> field in the key determines whether - * a PoolablePreparedStatement or PoolableCallableStatement is created. + * {@link KeyedPooledObjectFactory} method for creating {@link PoolablePreparedStatement}s or + * {@link PoolableCallableStatement}s. The <code>stmtType</code> field in the key determines whether a + * PoolablePreparedStatement or PoolableCallableStatement is created. * - * @param key the key for the {@link PreparedStatement} to be created + * @param key + * the key for the {@link PreparedStatement} to be created * @see #createKey(String, int, int, StatementType) */ + @SuppressWarnings("resource") @Override - public PooledObject<DelegatingPreparedStatement> makeObject(final PStmtKey key) - throws Exception { - if(null == key) { + public PooledObject<DelegatingPreparedStatement> makeObject(final PStmtKey key) throws Exception { + if (null == key) { throw new IllegalArgumentException("Prepared statement key is null or invalid."); } - if (key.getStmtType() == StatementType.PREPARED_STATEMENT ) { + if (key.getStmtType() == StatementType.PREPARED_STATEMENT) { final PreparedStatement statement = (PreparedStatement) key.createStatement(getDelegate()); - @SuppressWarnings({"rawtypes", "unchecked"}) // Unable to find way to avoid this - final - PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, _pstmtPool, this); + @SuppressWarnings({"rawtypes", "unchecked" }) // Unable to find way to avoid this + final PoolablePreparedStatement pps = new PoolablePreparedStatement(statement, key, pstmtPool, this); return new DefaultPooledObject<DelegatingPreparedStatement>(pps); } final CallableStatement statement = (CallableStatement) key.createStatement(getDelegate()); - final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, _pstmtPool, this); + final PoolableCallableStatement pcs = new PoolableCallableStatement(statement, key, pstmtPool, this); return new DefaultPooledObject<DelegatingPreparedStatement>(pcs); } /** - * {@link KeyedPooledObjectFactory} method for destroying - * PoolablePreparedStatements and PoolableCallableStatements. - * Closes the underlying statement. + * Normalizes the given SQL statement, producing a canonical form that is semantically equivalent to the original. + */ + protected String normalizeSQL(final String sql) { + return sql.trim(); + } + + /** + * {@link KeyedPooledObjectFactory} method for passivating {@link PreparedStatement}s or {@link CallableStatement}s. + * Invokes {@link PreparedStatement#clearParameters}. * - * @param key ignored - * @param p the wrapped pooled statement to be destroyed. + * @param key + * ignored + * @param pooledObject + * a wrapped {@link PreparedStatement} */ @Override - public void destroyObject(final PStmtKey key, - final PooledObject<DelegatingPreparedStatement> p) + public void passivateObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject) throws Exception { - p.getObject().getInnermostDelegate().close(); + @SuppressWarnings("resource") + final DelegatingPreparedStatement dps = pooledObject.getObject(); + dps.clearParameters(); + dps.passivate(); } /** - * {@link KeyedPooledObjectFactory} method for validating - * pooled statements. Currently always returns true. + * Creates or obtains a {@link CallableStatement} from the pool. * - * @param key ignored - * @param p ignored - * @return {@code true} + * @param sql + * the SQL string used to define the CallableStatement + * @return a {@link PoolableCallableStatement} + * @throws SQLException + * Wraps an underlying exception. */ @Override - public boolean validateObject(final PStmtKey key, - final PooledObject<DelegatingPreparedStatement> p) { - return true; + public CallableStatement prepareCall(final String sql) throws SQLException { + try { + return (CallableStatement) pstmtPool.borrowObject(createKey(sql, StatementType.CALLABLE_STATEMENT)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenCallableStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow callableStatement from pool failed", e); + } + } + + /** + * Creates or obtains a {@link CallableStatement} from the pool. + * + * @param sql + * the SQL string used to define the CallableStatement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency + * @return a {@link PoolableCallableStatement} + * @throws SQLException + * Wraps an underlying exception. + */ + @Override + public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) + throws SQLException { + try { + return (CallableStatement) pstmtPool.borrowObject( + createKey(sql, resultSetType, resultSetConcurrency, StatementType.CALLABLE_STATEMENT)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenCallableStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow callableStatement from pool failed", e); + } } /** - * {@link KeyedPooledObjectFactory} method for activating - * pooled statements. + * Creates or obtains a {@link CallableStatement} from the pool. * - * @param key ignored - * @param p wrapped pooled statement to be activated + * @param sql + * the SQL string used to define the CallableStatement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency + * @param resultSetHoldability + * result set holdability + * @return a {@link PoolableCallableStatement} + * @throws SQLException + * Wraps an underlying exception. */ @Override - public void activateObject(final PStmtKey key, - final PooledObject<DelegatingPreparedStatement> p) throws Exception { - p.getObject().activate(); + public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { + try { + return (CallableStatement) pstmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency, + resultSetHoldability, StatementType.CALLABLE_STATEMENT)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenCallableStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow callableStatement from pool failed", e); + } } /** - * {@link KeyedPooledObjectFactory} method for passivating - * {@link PreparedStatement}s or {@link CallableStatement}s. - * Invokes {@link PreparedStatement#clearParameters}. + * Creates or obtains a {@link PreparedStatement} from the pool. * - * @param key ignored - * @param p a wrapped {@link PreparedStatement} + * @param sql + * the SQL string used to define the PreparedStatement + * @return a {@link PoolablePreparedStatement} */ @Override - public void passivateObject(final PStmtKey key, - final PooledObject<DelegatingPreparedStatement> p) throws Exception { - final DelegatingPreparedStatement dps = p.getObject(); - dps.clearParameters(); - dps.passivate(); + public PreparedStatement prepareStatement(final String sql) throws SQLException { + if (null == pstmtPool) { + throw new SQLException("Statement pool is null - closed or invalid PoolingConnection."); + } + try { + return pstmtPool.borrowObject(createKey(sql)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenPreparedStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow prepareStatement from pool failed", e); + } + } + + @Override + public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException { + if (null == pstmtPool) { + throw new SQLException("Statement pool is null - closed or invalid PoolingConnection."); + } + try { + return pstmtPool.borrowObject(createKey(sql, autoGeneratedKeys)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenPreparedStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow prepareStatement from pool failed", e); + } + } + + /** + * Creates or obtains a {@link PreparedStatement} from the pool. + * + * @param sql + * the SQL string used to define the PreparedStatement + * @param columnIndexes + * column indexes + * @return a {@link PoolablePreparedStatement} + */ + @Override + public PreparedStatement prepareStatement(final String sql, final int columnIndexes[]) throws SQLException { + if (null == pstmtPool) { + throw new SQLException("Statement pool is null - closed or invalid PoolingConnection."); + } + try { + return pstmtPool.borrowObject(createKey(sql, columnIndexes)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenPreparedStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow prepareStatement from pool failed", e); + } + } + + /** + * Creates or obtains a {@link PreparedStatement} from the pool. + * + * @param sql + * the SQL string used to define the PreparedStatement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency + * @return a {@link PoolablePreparedStatement} + */ + @Override + public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) + throws SQLException { + if (null == pstmtPool) { + throw new SQLException("Statement pool is null - closed or invalid PoolingConnection."); + } + try { + return pstmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenPreparedStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow prepareStatement from pool failed", e); + } + } + + /** + * Creates or obtains a {@link PreparedStatement} from the pool. + * + * @param sql + * the SQL string used to define the PreparedStatement + * @param resultSetType + * result set type + * @param resultSetConcurrency + * result set concurrency + * @param resultSetHoldability + * result set holdability + * @return a {@link PoolablePreparedStatement} + */ + @Override + public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { + if (null == pstmtPool) { + throw new SQLException("Statement pool is null - closed or invalid PoolingConnection."); + } + try { + return pstmtPool.borrowObject(createKey(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenPreparedStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow prepareStatement from pool failed", e); + } + } + + /** + * Creates or obtains a {@link PreparedStatement} from the pool. + * + * @param sql + * the SQL string used to define the PreparedStatement + * @param columnNames + * column names + * @return a {@link PoolablePreparedStatement} + */ + @Override + public PreparedStatement prepareStatement(final String sql, final String columnNames[]) throws SQLException { + if (null == pstmtPool) { + throw new SQLException("Statement pool is null - closed or invalid PoolingConnection."); + } + try { + return pstmtPool.borrowObject(createKey(sql, columnNames)); + } catch (final NoSuchElementException e) { + throw new SQLException("MaxOpenPreparedStatements limit reached", e); + } catch (final RuntimeException e) { + throw e; + } catch (final Exception e) { + throw new SQLException("Borrow prepareStatement from pool failed", e); + } + } + + /** + * Sets the prepared statement pool. + * + * @param pool + * the prepared statement pool. + */ + public void setStatementPool(final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> pool) { + pstmtPool = pool; } @Override public String toString() { - if (_pstmtPool != null ) { - return "PoolingConnection: " + _pstmtPool.toString(); + if (pstmtPool != null) { + return "PoolingConnection: " + pstmtPool.toString(); } return "PoolingConnection: null"; } /** - * The possible statement types. - * @since 2.0 + * {@link KeyedPooledObjectFactory} method for validating pooled statements. Currently always returns true. + * + * @param key + * ignored + * @param pooledObject + * ignored + * @return {@code true} */ - protected enum StatementType { - CALLABLE_STATEMENT, - PREPARED_STATEMENT + @Override + public boolean validateObject(final PStmtKey key, final PooledObject<DelegatingPreparedStatement> pooledObject) { + return true; } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org