Class PooledConnection
java.lang.Object
org.apache.tomcat.jdbc.pool.PooledConnection
- All Implemented Interfaces:
PooledConnectionMBean
Represents a pooled connection
and holds a reference to the
Connection object-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ConnectionPoolThe parentprotected PoolConfigurationThe properties for the connection poolstatic final StringAttribute key for the password.static final StringAttribute key for the username.static final intValidate when connection is borrowed flagstatic final intValidate when connection is idle flagstatic final intValidate when connection is initialized flagstatic final intValidate when connection is returned flagprotected XAConnectionIf using a XAConnection underneath. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanDeprecated.voidClears any warnings reported on this connection.voidconnect()Connects the underlying connection to the database.protected voidConnects using the configured DataSource.protected voidConnects using the configured JDBC Driver class.voidCreates and registers an MBean for this pooled connection with JMX.longReturns abandon timeout in millisecondsReturns the attributes map for this connection.booleanReturns the current auto-commit mode.Returns the current catalog name.Returns the underlying connectionlongReturns the connection version.Returns the first handler in the interceptor chainintReturns the current holdability for ResultSet objects.longReturns the timestamp of when the connection was last connected to the database. ie, a successful call toDriver.connect(String, java.util.Properties).longReturns the timestamp of the last successful validation query execution.Returns the JMX ObjectName for this pooled connection.Returns the configuration for this connection and poollongThe time limit for how long the object can remain unused before it is releasedReturns the current schema name.Returns the stack trace from when this connection was borrowed.longReturn the timestamps of last pool action.intReturns the current transaction isolation level.Returns the underlying XA connectionbooleanisClosed()Returns true if the connection has been closed.booleanReturns the discarded flag.booleanReturns true if the connection has been initialized.booleanReturns true if the connection has been connected more thanPoolConfiguration.getMaxAge()milliseconds. false otherwise.booleanReturns true if the connection is in read-only mode.booleanReturns true if this connection has been released and wont be reused.booleanReturns true if the connection is suspected to be invalid.voidlock()Locks the connection only if eitherPoolConfiguration.isPoolSweeperEnabled()orPoolConfiguration.getUseLock()return true.voidIssues a call todisconnect(boolean)with the argument false followed by a call toconnect()booleanrelease()This method is called if (Now - timeCheckedIn > getReleaseTime()) This method disconnects the connection, logs an error in debug mode if it happens then sets thereleasedflag to false.voidsetDiscarded(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.voidsetHandler(JdbcInterceptor handler) Sets the interceptor chain handler for this connection.voidsetLastValidated(long lastValidated) Set the timestamp the connection was last validated.voidsetPoolProperties(PoolConfiguration poolProperties) Sets the pool configuration for this connection and connection pool.voidsetStackTrace(String trace) The pool will set the stack trace when it is check out and checked invoidsetSuspect(boolean suspect) Sets the suspect flag for this connection.voidsetTimestamp(long timestamp) Sets a timestamp on this connection.booleanshouldForceReconnect(String username, String password) Returns true if we must force reconnect based on credentials passed in.toString()voidunlock()Unlocks the connection only if the sweeper is enabled Otherwise this is a noop for performancebooleanvalidate(int validateAction) Returnstrueif the object is still valid. if not the pool will call the getExpiredAction() and follow up with one of the four expired methodsbooleanValidates a connection.
-
Field Details
-
PROP_USER
-
PROP_PASSWORD
-
VALIDATE_BORROW
public static final int VALIDATE_BORROWValidate when connection is borrowed flag- See Also:
-
VALIDATE_RETURN
public static final int VALIDATE_RETURNValidate when connection is returned flag- See Also:
-
VALIDATE_IDLE
public static final int VALIDATE_IDLEValidate when connection is idle flag- See Also:
-
VALIDATE_INIT
public static final int VALIDATE_INITValidate when connection is initialized flag- See Also:
-
poolProperties
The properties for the connection pool -
xaConnection
If using a XAConnection underneath. -
parent
The parent
-
-
Constructor Details
-
PooledConnection
Constructor- Parameters:
prop- - pool propertiesparent- - the parent connection pool
-
-
Method Details
-
getConnectionVersion
public long getConnectionVersion()Description copied from interface:PooledConnectionMBeanReturns the connection version.- Specified by:
getConnectionVersionin interfacePooledConnectionMBean- Returns:
- the connection version
-
checkUser
Deprecated.useshouldForceReconnect(String, String)method kept since it was public, to avoid changing interface.- Parameters:
username- The user namepassword- The password- Returns:
trueif the pool does not need to reconnect
-
shouldForceReconnect
Returns true if we must force reconnect based on credentials passed in. Returns false ifPoolConfiguration.isAlternateUsernameAllowed()method returns false. Returns false if the username/password has not changed since this connection was connected- Parameters:
username- the username you wish to connect with, pass in null to accept the default username fromPoolConfiguration.getUsername()password- the password you wish to connect with, pass in null to accept the default username fromPoolConfiguration.getPassword()- Returns:
- true is the pool must reconnect
-
connect
Connects the underlying connection to the database.- Throws:
SQLException- if the methodrelease()has been called.SQLException- if driver instantiation failsSQLException- if a call toDriver.connect(String, java.util.Properties)fails.SQLException- if default properties are configured and a call toConnection.setAutoCommit(boolean),Connection.setCatalog(String),Connection.setTransactionIsolation(int)orConnection.setReadOnly(boolean)fails.
-
connectUsingDataSource
Connects using the configured DataSource.- Throws:
SQLException- if unable to obtain a connection from the DataSource
-
connectUsingDriver
Connects using the configured JDBC Driver class.- Throws:
SQLException- if unable to instantiate the driver or obtain a connection
-
isInitialized
public boolean isInitialized()Description copied from interface:PooledConnectionMBeanReturns true if the connection has been initialized.- Specified by:
isInitializedin interfacePooledConnectionMBean- Returns:
- true if connect() was called successfully and disconnect has not yet been called
-
isMaxAgeExpired
public boolean isMaxAgeExpired()Returns true if the connection has been connected more thanPoolConfiguration.getMaxAge()milliseconds. false otherwise.- Specified by:
isMaxAgeExpiredin interfacePooledConnectionMBean- Returns:
- Returns true if the connection has been connected more than
PoolConfiguration.getMaxAge()milliseconds. false otherwise.
-
reconnect
Issues a call todisconnect(boolean)with the argument false followed by a call toconnect()- Throws:
SQLException- if the call toconnect()fails.
-
getAbandonTimeout
public long getAbandonTimeout()Returns abandon timeout in milliseconds- Returns:
- abandon timeout in milliseconds
-
validate
public boolean validate(int validateAction) Returnstrueif 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- The value- Returns:
trueif the connection is valid
-
validate
Validates a connection.- Parameters:
validateAction- the action used. One ofVALIDATE_BORROW,VALIDATE_IDLE,VALIDATE_INITorVALIDATE_RETURNsql- the SQL to be used during validation. If thePoolConfiguration.setInitSQL(String)has been called with a non null value and the action isVALIDATE_INITthe init SQL will be used for validation.- 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.
-
getReleaseTime
public long getReleaseTime()The time limit for how long the object can remain unused before it is released -
release
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 thereleasedflag to false. Any attempts to connect this cached object again will fail perconnect()The connection pool uses the atomic return value to decrement the pool size counter.- Returns:
- true if this is the first time this method has been called. false if this method has been called before.
-
setStackTrace
The pool will set the stack trace when it is check out and checked in- Parameters:
trace- the stack trace for this connection
-
getStackTrace
Returns the stack trace from when this connection was borrowed. Can return null if no stack trace was set.- Returns:
- the stack trace or null of no trace was set
-
setTimestamp
public void setTimestamp(long timestamp) Sets a timestamp on this connection. A timestamp usually means that some operation performed successfully.- Parameters:
timestamp- the timestamp as defined bySystem.currentTimeMillis()
-
isSuspect
public boolean isSuspect()Description copied from interface:PooledConnectionMBeanReturns true if the connection is suspected to be invalid.- Specified by:
isSuspectin interfacePooledConnectionMBean- Returns:
- true if suspect
-
setSuspect
public void setSuspect(boolean suspect) Sets the suspect flag for this connection.- Parameters:
suspect- true if the connection is suspected to be invalid
-
setDiscarded
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- - only valid value is true- Throws:
IllegalStateException- if this method is called with the value false and the value true has already been set.
-
setLastValidated
public void setLastValidated(long lastValidated) Set the timestamp the connection was last validated. This flag is used to keep track when we are using avalidation-interval.- Parameters:
lastValidated- a timestamp as defined bySystem.currentTimeMillis()
-
setPoolProperties
Sets the pool configuration for this connection and connection pool. Object is shared with theConnectionPool- Parameters:
poolProperties- The pool properties
-
getTimestamp
public long 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 ofabandon-timeouts. This timestamp can also be reset by theResetAbandonedTimer.invoke(Object, java.lang.reflect.Method, Object[])- Specified by:
getTimestampin interfacePooledConnectionMBean- Returns:
- the timestamp of the last pool action as defined by
System.currentTimeMillis()
-
isDiscarded
public boolean isDiscarded()Returns the discarded flag.- Specified by:
isDiscardedin interfacePooledConnectionMBean- 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.
-
getLastValidated
public long getLastValidated()Returns the timestamp of the last successful validation query execution.- Specified by:
getLastValidatedin interfacePooledConnectionMBean- Returns:
- the timestamp of the last successful validation query execution as defined by
System.currentTimeMillis()
-
getPoolProperties
Returns the configuration for this connection and pool- Returns:
- the configuration for this connection and pool
-
lock
public void lock()Locks the connection only if eitherPoolConfiguration.isPoolSweeperEnabled()orPoolConfiguration.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 -
unlock
public void unlock()Unlocks the connection only if the sweeper is enabled Otherwise this is a noop for performance -
getConnection
Returns the underlying connection- Returns:
- the underlying JDBC connection as it was returned from the JDBC driver
- See Also:
-
getXAConnection
Returns the underlying XA connection- Returns:
- the underlying XA connection as it was returned from the Datasource
-
getLastConnected
public long getLastConnected()Returns the timestamp of when the connection was last connected to the database. ie, a successful call toDriver.connect(String, java.util.Properties).- Specified by:
getLastConnectedin interfacePooledConnectionMBean- Returns:
- the timestamp when this connection was created as defined by
System.currentTimeMillis()
-
getHandler
Returns the first handler in the interceptor chain- Returns:
- the first interceptor for this connection
-
setHandler
Sets the interceptor chain handler for this connection.- Parameters:
handler- the first interceptor in the chain
-
toString
-
isReleased
public boolean isReleased()Returns true if this connection has been released and wont be reused.- Specified by:
isReleasedin interfacePooledConnectionMBean- Returns:
- true if the method
release()has been called
-
getAttributes
-
createMBean
public void createMBean()Creates and registers an MBean for this pooled connection with JMX. -
getObjectName
Returns the JMX ObjectName for this pooled connection.- Returns:
- the JMX ObjectName or null if not registered
-
clearWarnings
public void clearWarnings()Description copied from interface:PooledConnectionMBeanClears any warnings reported on this connection.- Specified by:
clearWarningsin interfacePooledConnectionMBean
-
isClosed
Description copied from interface:PooledConnectionMBeanReturns true if the connection has been closed.- Specified by:
isClosedin interfacePooledConnectionMBean- Returns:
- true if closed
- Throws:
SQLException- if a database access error occurs
-
getAutoCommit
Description copied from interface:PooledConnectionMBeanReturns the current auto-commit mode.- Specified by:
getAutoCommitin interfacePooledConnectionMBean- Returns:
- the auto-commit mode
- Throws:
SQLException- if a database access error occurs
-
getCatalog
Description copied from interface:PooledConnectionMBeanReturns the current catalog name.- Specified by:
getCatalogin interfacePooledConnectionMBean- Returns:
- the catalog name
- Throws:
SQLException- if a database access error occurs
-
getHoldability
Description copied from interface:PooledConnectionMBeanReturns the current holdability for ResultSet objects.- Specified by:
getHoldabilityin interfacePooledConnectionMBean- Returns:
- the holdability
- Throws:
SQLException- if a database access error occurs
-
isReadOnly
Description copied from interface:PooledConnectionMBeanReturns true if the connection is in read-only mode.- Specified by:
isReadOnlyin interfacePooledConnectionMBean- Returns:
- true if read-only
- Throws:
SQLException- if a database access error occurs
-
getSchema
Description copied from interface:PooledConnectionMBeanReturns the current schema name.- Specified by:
getSchemain interfacePooledConnectionMBean- Returns:
- the schema name
- Throws:
SQLException- if a database access error occurs
-
getTransactionIsolation
Description copied from interface:PooledConnectionMBeanReturns the current transaction isolation level.- Specified by:
getTransactionIsolationin interfacePooledConnectionMBean- Returns:
- the transaction isolation level
- Throws:
SQLException- if a database access error occurs
-
shouldForceReconnect(String, String)method kept since it was public, to avoid changing interface.