You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2018/06/19 11:21:13 UTC
svn commit: r1833816 [14/16] - in
/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2: ./ cpdsadapter/
datasources/
Modified: tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/datasources/KeyedCPDSConnectionFactory.java
URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/datasources/KeyedCPDSConnectionFactory.java?rev=1833816&r1=1833815&r2=1833816&view=diff
==============================================================================
--- tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/datasources/KeyedCPDSConnectionFactory.java (original)
+++ tomcat/tc8.0.x/trunk/java/org/apache/tomcat/dbcp/dbcp2/datasources/KeyedCPDSConnectionFactory.java Tue Jun 19 11:21:13 2018
@@ -38,64 +38,59 @@ import org.apache.tomcat.dbcp.pool2.Pool
import org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject;
/**
- * A {@link KeyedPooledObjectFactory} that creates
- * {@link org.apache.tomcat.dbcp.dbcp2.PoolableConnection PoolableConnection}s.
+ * A {@link KeyedPooledObjectFactory} that creates {@link org.apache.tomcat.dbcp.dbcp2.PoolableConnection
+ * PoolableConnection}s.
*
- * @author John D. McNally
* @since 2.0
*/
-class KeyedCPDSConnectionFactory
- implements KeyedPooledObjectFactory<UserPassKey,PooledConnectionAndInfo>,
- ConnectionEventListener, PooledConnectionManager {
+class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey, PooledConnectionAndInfo>,
+ ConnectionEventListener, PooledConnectionManager {
- private static final String NO_KEY_MESSAGE
- = "close() was called on a Connection, but "
+ private static final String NO_KEY_MESSAGE = "close() was called on a Connection, but "
+ "I have no record of the underlying PooledConnection.";
- private final ConnectionPoolDataSource _cpds;
- private final String _validationQuery;
- private final int _validationQueryTimeout;
- private final boolean _rollbackAfterValidation;
- private KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> _pool;
+ private final ConnectionPoolDataSource cpds;
+ private final String validationQuery;
+ private final int validationQueryTimeoutSeconds;
+ private final boolean rollbackAfterValidation;
+ private KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool;
private long maxConnLifetimeMillis = -1;
/**
- * Map of PooledConnections for which close events are ignored.
- * Connections are muted when they are being validated.
+ * Map of PooledConnections for which close events are ignored. Connections are muted when they are being validated.
*/
- private final Set<PooledConnection> validatingSet =
- Collections.newSetFromMap(new ConcurrentHashMap<PooledConnection,Boolean>());
+ private final Set<PooledConnection> validatingSet = Collections
+ .newSetFromMap(new ConcurrentHashMap<PooledConnection, Boolean>());
/**
* Map of PooledConnectionAndInfo instances
*/
- private final Map<PooledConnection, PooledConnectionAndInfo> pcMap =
- new ConcurrentHashMap<>();
-
+ private final Map<PooledConnection, PooledConnectionAndInfo> pcMap = new ConcurrentHashMap<>();
/**
* Create a new {@code KeyedPoolableConnectionFactory}.
- * @param cpds the ConnectionPoolDataSource from which to obtain
- * PooledConnections
- * @param validationQuery a query to use to {@link #validateObject validate}
- * {@link Connection}s. Should return at least one row. May be
- * {@code null} in which case3 {@link Connection#isValid(int)} will be used
- * to validate connections.
- * @param rollbackAfterValidation whether a rollback should be issued after
- * {@link #validateObject validating} {@link Connection}s.
- */
- public KeyedCPDSConnectionFactory(final ConnectionPoolDataSource cpds,
- final String validationQuery,
- final int validationQueryTimeout,
- final boolean rollbackAfterValidation) {
- _cpds = cpds;
- _validationQuery = validationQuery;
- _validationQueryTimeout = validationQueryTimeout;
- _rollbackAfterValidation = rollbackAfterValidation;
+ *
+ * @param cpds
+ * the ConnectionPoolDataSource from which to obtain PooledConnections
+ * @param validationQuery
+ * a query to use to {@link #validateObject validate} {@link Connection}s. Should return at least one
+ * row. May be {@code null} in which case3 {@link Connection#isValid(int)} will be used to validate
+ * connections.
+ * @param validationQueryTimeoutSeconds
+ * The time, in seconds, to allow for the validation query to complete
+ * @param rollbackAfterValidation
+ * whether a rollback should be issued after {@link #validateObject validating} {@link Connection}s.
+ */
+ public KeyedCPDSConnectionFactory(final ConnectionPoolDataSource cpds, final String validationQuery,
+ final int validationQueryTimeoutSeconds, final boolean rollbackAfterValidation) {
+ this.cpds = cpds;
+ this.validationQuery = validationQuery;
+ this.validationQueryTimeoutSeconds = validationQueryTimeoutSeconds;
+ this.rollbackAfterValidation = rollbackAfterValidation;
}
- public void setPool(final KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> pool) {
- this._pool = pool;
+ public void setPool(final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool) {
+ this.pool = pool;
}
/**
@@ -103,29 +98,30 @@ class KeyedCPDSConnectionFactory
*
* @return KeyedObjectPool managing pooled connections
*/
- public KeyedObjectPool<UserPassKey,PooledConnectionAndInfo> getPool() {
- return _pool;
+ public KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> getPool() {
+ return pool;
}
/**
* Creates a new {@link PooledConnectionAndInfo} from the given {@link UserPassKey}.
*
- * @param upkey {@link UserPassKey} containing user credentials
- * @throws SQLException if the connection could not be created.
+ * @param upkey
+ * {@link UserPassKey} containing user credentials
+ * @throws SQLException
+ * if the connection could not be created.
* @see org.apache.tomcat.dbcp.pool2.KeyedPooledObjectFactory#makeObject(java.lang.Object)
*/
@Override
- public synchronized PooledObject<PooledConnectionAndInfo> makeObject(final UserPassKey upkey)
- throws Exception {
+ public synchronized PooledObject<PooledConnectionAndInfo> makeObject(final UserPassKey upkey) throws Exception {
PooledConnectionAndInfo pci = null;
PooledConnection pc = null;
- final String username = upkey.getUsername();
+ final String userName = upkey.getUsername();
final String password = upkey.getPassword();
- if (username == null) {
- pc = _cpds.getPooledConnection();
+ if (userName == null) {
+ pc = cpds.getPooledConnection();
} else {
- pc = _cpds.getPooledConnection(username, password);
+ pc = cpds.getPooledConnection(userName, password);
}
if (pc == null) {
@@ -135,7 +131,7 @@ class KeyedCPDSConnectionFactory
// should we add this object as a listener or the pool.
// consider the validateObject method in decision
pc.addConnectionEventListener(this);
- pci = new PooledConnectionAndInfo(pc, username, password);
+ pci = new PooledConnectionAndInfo(pc, userName, upkey.getPasswordCharArray());
pcMap.put(pc, pci);
return new DefaultPooledObject<>(pci);
@@ -145,8 +141,7 @@ class KeyedCPDSConnectionFactory
* Closes the PooledConnection and stops listening for events from it.
*/
@Override
- public void destroyObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p)
- throws Exception {
+ public void destroyObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws Exception {
final PooledConnection pc = p.getObject().getPooledConnection();
pc.removeConnectionEventListener(this);
pcMap.remove(pc);
@@ -156,31 +151,31 @@ class KeyedCPDSConnectionFactory
/**
* Validates a pooled connection.
*
- * @param key ignored
- * @param p wrapped {@link PooledConnectionAndInfo} containing the
- * connection to validate
+ * @param key
+ * ignored
+ * @param pooledObject
+ * wrapped {@link PooledConnectionAndInfo} containing the connection to validate
* @return true if validation succeeds
*/
@Override
- public boolean validateObject(final UserPassKey key,
- final PooledObject<PooledConnectionAndInfo> p) {
+ public boolean validateObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> pooledObject) {
try {
- validateLifetime(p);
+ validateLifetime(pooledObject);
} catch (final Exception e) {
return false;
}
boolean valid = false;
- final PooledConnection pconn = p.getObject().getPooledConnection();
+ final PooledConnection pconn = pooledObject.getObject().getPooledConnection();
Connection conn = null;
validatingSet.add(pconn);
- if (null == _validationQuery) {
- int timeout = _validationQueryTimeout;
- if (timeout < 0) {
- timeout = 0;
+ if (null == validationQuery) {
+ int timeoutSeconds = validationQueryTimeoutSeconds;
+ if (timeoutSeconds < 0) {
+ timeoutSeconds = 0;
}
try {
conn = pconn.getConnection();
- valid = conn.isValid(timeout);
+ valid = conn.isValid(timeoutSeconds);
} catch (final SQLException e) {
valid = false;
} finally {
@@ -198,16 +193,16 @@ class KeyedCPDSConnectionFactory
try {
conn = pconn.getConnection();
stmt = conn.createStatement();
- rset = stmt.executeQuery(_validationQuery);
+ rset = stmt.executeQuery(validationQuery);
if (rset.next()) {
valid = true;
} else {
valid = false;
}
- if (_rollbackAfterValidation) {
+ if (rollbackAfterValidation) {
conn.rollback();
}
- } catch(final Exception e) {
+ } catch (final Exception e) {
valid = false;
} finally {
Utils.closeQuietly(rset);
@@ -220,14 +215,12 @@ class KeyedCPDSConnectionFactory
}
@Override
- public void passivateObject(final UserPassKey key,
- final PooledObject<PooledConnectionAndInfo> p) throws Exception {
+ public void passivateObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws Exception {
validateLifetime(p);
}
@Override
- public void activateObject(final UserPassKey key,
- final PooledObject<PooledConnectionAndInfo> p) throws Exception {
+ public void activateObject(final UserPassKey key, final PooledObject<PooledConnectionAndInfo> p) throws Exception {
validateLifetime(p);
}
@@ -236,14 +229,13 @@ class KeyedCPDSConnectionFactory
// ***********************************************************************
/**
- * This will be called if the Connection returned by the getConnection
- * method came from a PooledConnection, and the user calls the close()
- * method of this connection object. What we need to do here is to
- * release this PooledConnection from our pool...
+ * This will be called if the Connection returned by the getConnection method came from a PooledConnection, and the
+ * user calls the close() method of this connection object. What we need to do here is to release this
+ * PooledConnection from our pool...
*/
@Override
public void connectionClosed(final ConnectionEvent event) {
- final PooledConnection pc = (PooledConnection)event.getSource();
+ final PooledConnection pc = (PooledConnection) event.getSource();
// if this event occurred because we were validating, or if this
// connection has been marked for removal, ignore it
// otherwise return the connection to the pool.
@@ -253,16 +245,14 @@ class KeyedCPDSConnectionFactory
throw new IllegalStateException(NO_KEY_MESSAGE);
}
try {
- _pool.returnObject(pci.getUserPassKey(), pci);
+ pool.returnObject(pci.getUserPassKey(), pci);
} catch (final Exception e) {
- System.err.println("CLOSING DOWN CONNECTION AS IT COULD " +
- "NOT BE RETURNED TO THE POOL");
+ System.err.println("CLOSING DOWN CONNECTION AS IT COULD " + "NOT BE RETURNED TO THE POOL");
pc.removeConnectionEventListener(this);
try {
- _pool.invalidateObject(pci.getUserPassKey(), pci);
+ pool.invalidateObject(pci.getUserPassKey(), pci);
} catch (final Exception e3) {
- System.err.println("EXCEPTION WHILE DESTROYING OBJECT " +
- pci);
+ System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + pci);
e3.printStackTrace();
}
}
@@ -270,16 +260,13 @@ class KeyedCPDSConnectionFactory
}
/**
- * If a fatal error occurs, close the underlying physical connection so as
- * not to be returned in the future
+ * If a fatal error occurs, close the underlying physical connection so as not to be returned in the future
*/
@Override
public void connectionErrorOccurred(final ConnectionEvent event) {
- final PooledConnection pc = (PooledConnection)event.getSource();
+ final PooledConnection pc = (PooledConnection) event.getSource();
if (null != event.getSQLException()) {
- System.err
- .println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" +
- event.getSQLException() + ")");
+ System.err.println("CLOSING DOWN CONNECTION DUE TO INTERNAL ERROR (" + event.getSQLException() + ")");
}
pc.removeConnectionEventListener(this);
@@ -288,7 +275,7 @@ class KeyedCPDSConnectionFactory
throw new IllegalStateException(NO_KEY_MESSAGE);
}
try {
- _pool.invalidateObject(info.getUserPassKey(), info);
+ pool.invalidateObject(info.getUserPassKey(), info);
} catch (final Exception e) {
System.err.println("EXCEPTION WHILE DESTROYING OBJECT " + info);
e.printStackTrace();
@@ -300,11 +287,10 @@ class KeyedCPDSConnectionFactory
// ***********************************************************************
/**
- * Invalidates the PooledConnection in the pool. The KeyedCPDSConnectionFactory
- * closes the connection and pool counters are updated appropriately.
- * Also clears any idle instances associated with the username that was used
- * to create the PooledConnection. Connections associated with this user
- * are not affected and they will not be automatically closed on return to the pool.
+ * Invalidates the PooledConnection in the pool. The KeyedCPDSConnectionFactory closes the connection and pool
+ * counters are updated appropriately. Also clears any idle instances associated with the user name that was used to
+ * create the PooledConnection. Connections associated with this user are not affected and they will not be
+ * automatically closed on return to the pool.
*/
@Override
public void invalidate(final PooledConnection pc) throws SQLException {
@@ -314,52 +300,50 @@ class KeyedCPDSConnectionFactory
}
final UserPassKey key = info.getUserPassKey();
try {
- _pool.invalidateObject(key, info); // Destroy and update pool counters
- _pool.clear(key); // Remove any idle instances with this key
+ pool.invalidateObject(key, info); // Destroy and update pool counters
+ pool.clear(key); // Remove any idle instances with this key
} catch (final Exception ex) {
throw new SQLException("Error invalidating connection", ex);
}
}
/**
- * Does nothing. This factory does not cache user credentials.
+ * Does nothing. This factory does not cache user credentials.
*/
@Override
public void setPassword(final String password) {
+ // Does nothing. This factory does not cache user credentials.
}
/**
- * 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.
+ *
+ * @param maxConnLifetimeMillis
+ * A value of zero or less indicates an infinite lifetime. The default value is -1.
*/
public void setMaxConnLifetimeMillis(final long maxConnLifetimeMillis) {
this.maxConnLifetimeMillis = maxConnLifetimeMillis;
}
/**
- * This implementation does not fully close the KeyedObjectPool, as
- * this would affect all users. Instead, it clears the pool associated
- * with the given user. This method is not currently used.
+ * This implementation does not fully close the KeyedObjectPool, as this would affect all users. Instead, it clears
+ * the pool associated with the given user. This method is not currently used.
*/
@Override
- public void closePool(final String username) throws SQLException {
+ public void closePool(final String userName) throws SQLException {
try {
- _pool.clear(new UserPassKey(username, null));
+ pool.clear(new UserPassKey(userName));
} catch (final Exception ex) {
throw new SQLException("Error closing connection pool", ex);
}
}
- private void validateLifetime(final PooledObject<PooledConnectionAndInfo> p)
- throws Exception {
+ private void validateLifetime(final PooledObject<PooledConnectionAndInfo> p) throws Exception {
if (maxConnLifetimeMillis > 0) {
final long lifetime = System.currentTimeMillis() - p.getCreateTime();
if (lifetime > maxConnLifetimeMillis) {
- throw new Exception(Utils.getMessage(
- "connectionFactory.lifetimeExceeded",
- Long.valueOf(lifetime),
+ throw new Exception(Utils.getMessage("connectionFactory.lifetimeExceeded", Long.valueOf(lifetime),
Long.valueOf(maxConnLifetimeMillis)));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org