Top Description Fields Constructors Methods
org.apache.tomcat.jdbc.pool

public Class PooledConnection

extends Object
implements PooledConnectionMBean
Class Inheritance
All Implemented Interfaces
org.apache.tomcat.jdbc.pool.PooledConnectionMBean
Imports
java.sql.DriverManager, .SQLException, .Statement, java.util.HashMap, .Properties, java.util.concurrent.atomic.AtomicBoolean, .AtomicLong, java.util.concurrent.locks.ReentrantReadWriteLock, javax.management.ObjectName, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.jdbc.pool.interceptor.ConnectionState, org.apache.tomcat.jdbc.pool.jmx.JmxUtil

Represents a pooled connection and holds a reference to the java.sql.Connection object

Field Summary

Modifier and TypeField and Description
private String
abandonTrace

When we track abandon traces, this string holds the thread dump

private HashMap<Object, Object>
private volatile Connection
connection

The underlying database connection

private static final AtomicLong
private volatile long
private volatile boolean
discarded

Set to true if this connection has been discarded by the pool

private Driver
private volatile JdbcInterceptor
handler

Weak reference to cache the list of interceptors for this connection so that we don't create a new list of interceptors each time we borrow the connection

private volatile long
lastConnected

The Timestamp when the last time the connect() method was called successfully

private volatile long
lastValidated

timestamp to keep track of validation intervals

private final ReentrantReadWriteLock
lock

Lock for this connection only

private static final Log
log

Logger

private ObjectName
protected ConnectionPool
parent

The parent

protected PoolConfiguration
poolProperties

The properties for the connection pool

public static final String
public static final String
private AtomicBoolean
private volatile boolean
private volatile long
timestamp

Timestamp the connection was last 'touched' by the pool

public static final int
VALIDATE_BORROW

Validate when connection is borrowed flag

public static final int
VALIDATE_IDLE

Validate when connection is idle flag

public static final int
VALIDATE_INIT

Validate when connection is initialized flag

public static final int
VALIDATE_RETURN

Validate when connection is returned flag

protected volatile XAConnection
xaConnection

If using a XAConnection underneath.

Constructor Summary

AccessConstructor and Description
public
PooledConnection(PoolConfiguration
- pool properties
prop
,
ConnectionPool
- the parent connection pool
parent
)

Constructor

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

trueif the pool does not need to reconnect
checkUser
(String
The user name
username
,
String
The password
password
)
Deprecated use shouldForceReconnect(String, String) method kept since it was public, to avoid changing interface.

public void
public void
connect()

Connects the underlying connection to the database.

protected void
protected void
public void
private void
disconnect(boolean
if set to true, a call to ConnectionPool#finalize(PooledConnection) is called.
finalize
)

Disconnects the connection.

private boolean
doValidate(int
The validation action
action
)

Returns true if the connection pool is configured to do validation for a certain action.

public long

Returns:

abandon timeout in milliseconds
getAbandonTimeout
()

Returns abandon timeout in milliseconds

public HashMap<Object, Object>
public boolean
public String
public Connection

Returns:

the underlying JDBC connection as it was returned from the JDBC driver
getConnection
()

Returns the underlying connection

public long
public JdbcInterceptor

Returns:

the first interceptor for this connection
getHandler
()

Returns the first handler in the interceptor chain

public int
public long

Returns:

the timestamp when this connection was created as defined by System#currentTimeMillis()
getLastConnected
()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getLastConnected.

Returns the timestamp of when the connection was last connected to the database.

public long

Returns:

the timestamp of the last successful validation query execution as defined by System#currentTimeMillis()
getLastValidated
()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getLastValidated.

Returns the timestamp of the last successful validation query execution.

public ObjectName
public PoolConfiguration

Returns:

the configuration for this connection and pool
getPoolProperties
()

Returns the configuration for this connection and pool

public long
getReleaseTime()

The time limit for how long the object can remain unused before it is released

public String
public String

Returns:

the stack trace or null of no trace was set
getStackTrace
()

Returns the stack trace from when this connection was borrowed.

public long

Returns:

the timestamp of the last pool action as defined by System#currentTimeMillis()
getTimestamp
()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getTimestamp.

Return the timestamps of last pool action.

public int
public XAConnection

Returns:

the underlying XA connection as it was returned from the Datasource
getXAConnection
()

Returns the underlying XA connection

public boolean
public boolean

Returns:

the discarded flag. If the value is true, either disconnect(boolean) has been called or it will be called when the connection is returned to the pool.
isDiscarded
()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isDiscarded.

Returns the discarded flag.

public boolean

Returns:

true if connect() was called successfully and disconnect has not yet been called
isInitialized
()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isInitialized.

public boolean

Returns:

Returns true if the connection has been connected more than PoolConfiguration#getMaxAge() milliseconds. false otherwise.
isMaxAgeExpired
()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isMaxAgeExpired.

Returns true if the connection has been connected more than PoolConfiguration#getMaxAge() milliseconds.

public boolean
public boolean

Returns:

true if the method release() has been called
isReleased
()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isReleased.

Returns true if this connection has been released and wont be reused.

public boolean
public void
lock()

Locks the connection only if either PoolConfiguration#isPoolSweeperEnabled() or PoolConfiguration#getUseLock() return true.

public void
reconnect()

Issues a call to disconnect(boolean) with the argument false followed by a call to connect()

public boolean

Returns:

true if this is the first time this method has been called. false if this method has been called before.
release
()

This method is called if (Now - timeCheckedIn > getReleaseTime()) This method disconnects the connection, logs an error in debug mode if it happens then sets the released flag to false.

public void
setDiscarded(boolean
- only valid value is true
discarded
)

An interceptor can call this method with the value true, and the connection will be closed when it is returned to the pool.

public void
public void
setLastValidated(long
a timestamp as defined by System#currentTimeMillis()
lastValidated
)

Set the timestamp the connection was last validated.

public void
setPoolProperties(PoolConfiguration
The pool properties
poolProperties
)

Sets the pool configuration for this connection and connection pool.

public void
setStackTrace(String
the stack trace for this connection
trace
)

The pool will set the stack trace when it is check out and checked in

public void
setSuspect(boolean suspect)

public void
setTimestamp(long
the timestamp as defined by System#currentTimeMillis()
timestamp
)

Sets a timestamp on this connection.

public boolean

Returns:

true is the pool must reconnect
shouldForceReconnect
(String
the username you wish to connect with, pass in null to accept the default username from PoolConfiguration#getUsername()
username
,
String
the password you wish to connect with, pass in null to accept the default username from org.apache.tomcat.jdbc.pool.PoolConfiguration#getPassword()
password
)

Returns true if we must force reconnect based on credentials passed in. Returns false if PoolConfiguration#isAlternateUsernameAllowed() method returns false.

private boolean
private boolean
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
unlock()

Unlocks the connection only if the sweeper is enabled Otherwise this is a noop for performance

public boolean

Returns:

true if the connection is valid
validate
(int
The value
validateAction
)

Returns true if the object is still valid.

public boolean

Returns:

true if the connection was validated successfully. It returns true even if validation was not performed, such as when PoolConfiguration#setValidationInterval(long) has been called with a positive value.

false if the validation failed. The caller should close the connection if false is returned since a session could have been left in an unknown state during initialization.

validate
(int validateAction, String
the SQL to be used during validation. If the PoolConfiguration#setInitSQL(String) has been called with a non null value and the action is VALIDATE_INIT the init SQL will be used for validation.
sql
)

Validates a connection.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

abandonTraceback to summary
private String abandonTrace

When we track abandon traces, this string holds the thread dump

attributesback to summary
private HashMap<Object, Object> attributes
connectionback to summary
private volatile Connection connection

The underlying database connection

connectionIndexback to summary
private static final AtomicLong connectionIndex
connectionVersionback to summary
private volatile long connectionVersion
discardedback to summary
private volatile boolean discarded

Set to true if this connection has been discarded by the pool

driverback to summary
private Driver driver
handlerback to summary
private volatile JdbcInterceptor handler

Weak reference to cache the list of interceptors for this connection so that we don't create a new list of interceptors each time we borrow the connection

lastConnectedback to summary
private volatile long lastConnected

The Timestamp when the last time the connect() method was called successfully

lastValidatedback to summary
private volatile long lastValidated

timestamp to keep track of validation intervals

lockback to summary
private final ReentrantReadWriteLock lock

Lock for this connection only

logback to summary
private static final Log log

Logger

onameback to summary
private ObjectName oname
parentback to summary
protected ConnectionPool parent

The parent

poolPropertiesback to summary
protected PoolConfiguration poolProperties

The properties for the connection pool

PROP_PASSWORDback to summary
public static final String PROP_PASSWORD
PROP_USERback to summary
public static final String PROP_USER
releasedback to summary
private AtomicBoolean released
suspectback to summary
private volatile boolean suspect
timestampback to summary
private volatile long timestamp

Timestamp the connection was last 'touched' by the pool

VALIDATE_BORROWback to summary
public static final int VALIDATE_BORROW

Validate when connection is borrowed flag

VALIDATE_IDLEback to summary
public static final int VALIDATE_IDLE

Validate when connection is idle flag

VALIDATE_INITback to summary
public static final int VALIDATE_INIT

Validate when connection is initialized flag

VALIDATE_RETURNback to summary
public static final int VALIDATE_RETURN

Validate when connection is returned flag

xaConnectionback to summary
protected volatile XAConnection xaConnection

If using a XAConnection underneath.

Constructor Detail

PooledConnectionback to summary
public PooledConnection(PoolConfiguration prop, ConnectionPool parent)

Constructor

Parameters
prop:PoolConfiguration

- pool properties

parent:ConnectionPool

- the parent connection pool

Method Detail

checkUserback to summary
public boolean checkUser(String username, String password)

Deprecated

use shouldForceReconnect(String, String) method kept since it was public, to avoid changing interface.

Parameters
username:String

The user name

password:String

The password

Returns:boolean

trueif the pool does not need to reconnect

Annotations
@Deprecated
clearWarningsback to summary
public void clearWarnings()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.clearWarnings.

Annotations
@Override
connectback to summary
public void connect() throws SQLException

Connects the underlying connection to the database.

Exceptions
SQLException:
connectUsingDataSourceback to summary
protected void connectUsingDataSource() throws SQLException
connectUsingDriverback to summary
protected void connectUsingDriver() throws SQLException
createMBeanback to summary
public void createMBean()
disconnectback to summary
private void disconnect(boolean finalize)

Disconnects the connection. All exceptions are logged using debug level.

Parameters
finalize:boolean

if set to true, a call to ConnectionPool#finalize(PooledConnection) is called.

doValidateback to summary
private boolean doValidate(int action)

Returns true if the connection pool is configured to do validation for a certain action.

Parameters
action:int

The validation action

getAbandonTimeoutback to summary
public long getAbandonTimeout()

Returns abandon timeout in milliseconds

Returns:long

abandon timeout in milliseconds

getAttributesback to summary
public HashMap<Object, Object> getAttributes()
getAutoCommitback to summary
public boolean getAutoCommit() throws SQLException

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getAutoCommit.

Annotations
@Override
getCatalogback to summary
public String getCatalog() throws SQLException

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getCatalog.

Annotations
@Override
getConnectionback to summary
public Connection getConnection()

Returns the underlying connection

Returns:Connection

the underlying JDBC connection as it was returned from the JDBC driver

See Also
javax.sql.PooledConnection#getConnection()
getConnectionVersionback to summary
public long getConnectionVersion()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getConnectionVersion.

Annotations
@Override
getHandlerback to summary
public JdbcInterceptor getHandler()

Returns the first handler in the interceptor chain

Returns:JdbcInterceptor

the first interceptor for this connection

getHoldabilityback to summary
public int getHoldability() throws SQLException

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getHoldability.

Annotations
@Override
getLastConnectedback to summary
public long getLastConnected()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getLastConnected.

Returns the timestamp of when the connection was last connected to the database. ie, a successful call to java.sql.Driver#connect(String, java.util.Properties).

Returns:long

the timestamp when this connection was created as defined by System#currentTimeMillis()

Annotations
@Override
getLastValidatedback to summary
public long getLastValidated()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getLastValidated.

Returns the timestamp of the last successful validation query execution.

Returns:long

the timestamp of the last successful validation query execution as defined by System#currentTimeMillis()

Annotations
@Override
getObjectNameback to summary
public ObjectName getObjectName()
getPoolPropertiesback to summary
public PoolConfiguration getPoolProperties()

Returns the configuration for this connection and pool

Returns:PoolConfiguration

the configuration for this connection and pool

getReleaseTimeback to summary
public long getReleaseTime()

The time limit for how long the object can remain unused before it is released

Returns:long

PoolConfiguration#getMinEvictableIdleTimeMillis()

getSchemaback to summary
public String getSchema() throws SQLException

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getSchema.

Annotations
@Override
getStackTraceback to summary
public String getStackTrace()

Returns the stack trace from when this connection was borrowed. Can return null if no stack trace was set.

Returns:String

the stack trace or null of no trace was set

getTimestampback to summary
public long getTimestamp()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getTimestamp.

Return the timestamps of last pool action. Timestamps are typically set when connections are borrowed from the pool. It is used to keep track of abandon-timeouts. This timestamp can also be reset by the org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer#invoke(Object, java.lang.reflect.Method, Object[])

Returns:long

the timestamp of the last pool action as defined by System#currentTimeMillis()

Annotations
@Override
getTransactionIsolationback to summary
public int getTransactionIsolation() throws SQLException

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.getTransactionIsolation.

Annotations
@Override
getXAConnectionback to summary
public XAConnection getXAConnection()

Returns the underlying XA connection

Returns:XAConnection

the underlying XA connection as it was returned from the Datasource

isClosedback to summary
public boolean isClosed() throws SQLException

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isClosed.

Annotations
@Override
isDiscardedback to summary
public boolean isDiscarded()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isDiscarded.

Returns the discarded flag.

Returns:boolean

the discarded flag. If the value is true, either disconnect(boolean) has been called or it will be called when the connection is returned to the pool.

Annotations
@Override
isInitializedback to summary
public boolean isInitialized()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isInitialized.

Returns:boolean

true if connect() was called successfully and disconnect has not yet been called

Annotations
@Override
isMaxAgeExpiredback to summary
public boolean isMaxAgeExpired()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isMaxAgeExpired.

Returns true if the connection has been connected more than PoolConfiguration#getMaxAge() milliseconds. false otherwise.

Returns:boolean

Returns true if the connection has been connected more than PoolConfiguration#getMaxAge() milliseconds. false otherwise.

Annotations
@Override
isReadOnlyback to summary
public boolean isReadOnly() throws SQLException

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isReadOnly.

Annotations
@Override
isReleasedback to summary
public boolean isReleased()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isReleased.

Returns true if this connection has been released and wont be reused.

Returns:boolean

true if the method release() has been called

Annotations
@Override
isSuspectback to summary
public boolean isSuspect()

Implements org.apache.tomcat.jdbc.pool.PooledConnectionMBean.isSuspect.

Annotations
@Override
lockback to summary
public void lock()

Locks the connection only if either PoolConfiguration#isPoolSweeperEnabled() or PoolConfiguration#getUseLock() return true. The per connection lock ensures thread safety is multiple threads are performing operations on the connection. Otherwise this is a noop for performance

reconnectback to summary
public void reconnect() throws SQLException

Issues a call to disconnect(boolean) with the argument false followed by a call to connect()

Exceptions
SQLException:
if the call to connect() fails.
releaseback to summary
public boolean release()

This method is called if (Now - timeCheckedIn > getReleaseTime()) This method disconnects the connection, logs an error in debug mode if it happens then sets the released flag to false. Any attempts to connect this cached object again will fail per connect() The connection pool uses the atomic return value to decrement the pool size counter.

Returns:boolean

true if this is the first time this method has been called. false if this method has been called before.

setDiscardedback to summary
public void setDiscarded(boolean discarded)

An interceptor can call this method with the value true, and the connection will be closed when it is returned to the pool.

Parameters
discarded:boolean

- only valid value is true

Exceptions
IllegalStateException:
if this method is called with the value false and the value true has already been set.
setHandlerback to summary
public void setHandler(JdbcInterceptor handler)
setLastValidatedback to summary
public void setLastValidated(long lastValidated)

Set the timestamp the connection was last validated. This flag is used to keep track when we are using a validation-interval.

Parameters
lastValidated:long

a timestamp as defined by System#currentTimeMillis()

setPoolPropertiesback to summary
public void setPoolProperties(PoolConfiguration poolProperties)

Sets the pool configuration for this connection and connection pool. Object is shared with the ConnectionPool

Parameters
poolProperties:PoolConfiguration

The pool properties

setStackTraceback to summary
public void setStackTrace(String trace)

The pool will set the stack trace when it is check out and checked in

Parameters
trace:String

the stack trace for this connection

setSuspectback to summary
public void setSuspect(boolean suspect)
setTimestampback to summary
public void setTimestamp(long timestamp)

Sets a timestamp on this connection. A timestamp usually means that some operation performed successfully.

Parameters
timestamp:long

the timestamp as defined by System#currentTimeMillis()

shouldForceReconnectback to summary
public boolean shouldForceReconnect(String username, String password)

Returns true if we must force reconnect based on credentials passed in. Returns false if PoolConfiguration#isAlternateUsernameAllowed() method returns false. Returns false if the username/password has not changed since this connection was connected

Parameters
username:String

the username you wish to connect with, pass in null to accept the default username from PoolConfiguration#getUsername()

password:String

the password you wish to connect with, pass in null to accept the default username from org.apache.tomcat.jdbc.pool.PoolConfiguration#getPassword()

Returns:boolean

true is the pool must reconnect

silentlyCommitTransactionIfNeededback to summary
private boolean silentlyCommitTransactionIfNeeded()
silentlyRollbackTransactionIfNeededback to summary
private boolean silentlyRollbackTransactionIfNeeded()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
unlockback to summary
public void unlock()

Unlocks the connection only if the sweeper is enabled Otherwise this is a noop for performance

validateback to summary
public boolean validate(int validateAction)

Returns true if the object is still valid. if not the pool will call the getExpiredAction() and follow up with one of the four expired methods

Parameters
validateAction:int

The value

Returns:boolean

true if the connection is valid

validateback to summary
public boolean validate(int validateAction, String sql)

Validates a connection.

Parameters
validateAction:int

the action used. One of VALIDATE_BORROW, VALIDATE_IDLE, VALIDATE_INIT or VALIDATE_RETURN

sql:String

the SQL to be used during validation. If the PoolConfiguration#setInitSQL(String) has been called with a non null value and the action is VALIDATE_INIT the init SQL will be used for validation.

Returns:boolean

true if the connection was validated successfully. It returns true even if validation was not performed, such as when PoolConfiguration#setValidationInterval(long) has been called with a positive value.

false if the validation failed. The caller should close the connection if false is returned since a session could have been left in an unknown state during initialization.