Class DefaultPooledObject<T>
- java.lang.Object
-
- org.apache.tomcat.dbcp.pool2.impl.DefaultPooledObject<T>
-
- Type Parameters:
T
- the type of object in the pool
- All Implemented Interfaces:
java.lang.Comparable<PooledObject<T>>
,PooledObject<T>
- Direct Known Subclasses:
PooledSoftReference
public class DefaultPooledObject<T> extends java.lang.Object implements PooledObject<T>
This wrapper is used to track the additional information, such as state, for the pooled objects.This class is intended to be thread-safe.
- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description DefaultPooledObject(T object)
Creates a new instance that wraps the provided object so that the pool can track the state of the pooled object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allocate()
Allocates the object.int
compareTo(PooledObject<T> other)
Orders instances based on idle time - i.e. the length of time since the instance was returned to the pool.boolean
deallocate()
boolean
endEvictionTest(java.util.Deque<PooledObject<T>> idleQueue)
Notifies the object that the eviction test has ended.long
getActiveTimeMillis()
Gets the amount of time in milliseconds this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).long
getBorrowedCount()
Gets the number of times this object has been borrowed.java.time.Instant
getCreateInstant()
Gets the time (using the same basis asInstant.now()
) that this object was created.long
getCreateTime()
Gets the time (using the same basis asSystem.currentTimeMillis()
) that this object was created.java.time.Duration
getIdleDuration()
Gets the amount of time that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).java.time.Duration
getIdleTime()
Gets the amount of time that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).long
getIdleTimeMillis()
Gets the amount of time in milliseconds that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).java.time.Instant
getLastBorrowInstant()
Gets the time the wrapped object was last borrowed.long
getLastBorrowTime()
Gets the time the wrapped object was last borrowed.java.time.Instant
getLastReturnInstant()
Gets the time the wrapped object was last borrowed.long
getLastReturnTime()
Gets the time the wrapped object was last returned.java.time.Instant
getLastUsedInstant()
Gets an estimate of the last time this object was used.long
getLastUsedTime()
Gets an estimate of the last time this object was used.T
getObject()
Gets the underlying object that is wrapped by this instance ofPooledObject
.PooledObjectState
getState()
Gets the state of this object.void
invalidate()
Sets the state toINVALID
.void
markAbandoned()
Marks the pooled object asABANDONED
.void
markReturning()
Marks the pooled object asRETURNING
.void
printStackTrace(java.io.PrintWriter writer)
Prints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.void
setLogAbandoned(boolean logAbandoned)
Sets whether to use abandoned object tracking.void
setRequireFullStackTrace(boolean requireFullStackTrace)
Configures the stack trace generation strategy based on whether or not fully detailed stack traces are required.boolean
startEvictionTest()
Attempts to place the pooled object in thePooledObjectState.EVICTION
state.java.lang.String
toString()
Gets a String form of the wrapper for debug purposes.void
use()
Records the current stack trace as the last time the object was used.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.tomcat.dbcp.pool2.PooledObject
equals, getActiveDuration, getActiveTime, getFullDuration, hashCode
-
-
-
-
Constructor Detail
-
DefaultPooledObject
public DefaultPooledObject(T object)
Creates a new instance that wraps the provided object so that the pool can track the state of the pooled object.- Parameters:
object
- The object to wrap
-
-
Method Detail
-
allocate
public boolean allocate()
Allocates the object.- Specified by:
allocate
in interfacePooledObject<T>
- Returns:
true
if the original state wasIDLE
-
compareTo
public int compareTo(PooledObject<T> other)
Description copied from interface:PooledObject
Orders instances based on idle time - i.e. the length of time since the instance was returned to the pool. Used by the GKOP idle object evictor.Note: This class has a natural ordering that is inconsistent with equals if distinct objects have the same identity hash code.
- Specified by:
compareTo
in interfacejava.lang.Comparable<T>
- Specified by:
compareTo
in interfacePooledObject<T>
-
deallocate
public boolean deallocate()
- Specified by:
deallocate
in interfacePooledObject<T>
- Returns:
true
if the state wasALLOCATED
orRETURNING
.
-
endEvictionTest
public boolean endEvictionTest(java.util.Deque<PooledObject<T>> idleQueue)
Description copied from interface:PooledObject
Notifies the object that the eviction test has ended.- Specified by:
endEvictionTest
in interfacePooledObject<T>
- Parameters:
idleQueue
- The queue of idle objects to which the object should be returned.- Returns:
- Currently not used.
-
getActiveTimeMillis
public long getActiveTimeMillis()
Description copied from interface:PooledObject
Gets the amount of time in milliseconds this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).- Specified by:
getActiveTimeMillis
in interfacePooledObject<T>
- Returns:
- The time in milliseconds last spent in the active state.
-
getBorrowedCount
public long getBorrowedCount()
Gets the number of times this object has been borrowed.- Specified by:
getBorrowedCount
in interfacePooledObject<T>
- Returns:
- The number of times this object has been borrowed.
- Since:
- 2.1
-
getCreateInstant
public java.time.Instant getCreateInstant()
Description copied from interface:PooledObject
Gets the time (using the same basis asInstant.now()
) that this object was created.- Specified by:
getCreateInstant
in interfacePooledObject<T>
- Returns:
- The creation time for the wrapped object.
-
getCreateTime
public long getCreateTime()
Description copied from interface:PooledObject
Gets the time (using the same basis asSystem.currentTimeMillis()
) that this object was created.- Specified by:
getCreateTime
in interfacePooledObject<T>
- Returns:
- The creation time for the wrapped object.
-
getIdleDuration
public java.time.Duration getIdleDuration()
Description copied from interface:PooledObject
Gets the amount of time that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).- Specified by:
getIdleDuration
in interfacePooledObject<T>
- Returns:
- The amount of time in last spent in the idle state.
-
getIdleTime
public java.time.Duration getIdleTime()
Description copied from interface:PooledObject
Gets the amount of time that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).- Specified by:
getIdleTime
in interfacePooledObject<T>
- Returns:
- The amount of time in last spent in the idle state.
-
getIdleTimeMillis
public long getIdleTimeMillis()
Description copied from interface:PooledObject
Gets the amount of time in milliseconds that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).- Specified by:
getIdleTimeMillis
in interfacePooledObject<T>
- Returns:
- The time in milliseconds last spent in the idle state.
-
getLastBorrowInstant
public java.time.Instant getLastBorrowInstant()
Description copied from interface:PooledObject
Gets the time the wrapped object was last borrowed.- Specified by:
getLastBorrowInstant
in interfacePooledObject<T>
- Returns:
- The time the object was last borrowed.
-
getLastBorrowTime
public long getLastBorrowTime()
Description copied from interface:PooledObject
Gets the time the wrapped object was last borrowed.- Specified by:
getLastBorrowTime
in interfacePooledObject<T>
- Returns:
- The time the object was last borrowed.
-
getLastReturnInstant
public java.time.Instant getLastReturnInstant()
Description copied from interface:PooledObject
Gets the time the wrapped object was last borrowed.- Specified by:
getLastReturnInstant
in interfacePooledObject<T>
- Returns:
- The time the object was last borrowed.
-
getLastReturnTime
public long getLastReturnTime()
Description copied from interface:PooledObject
Gets the time the wrapped object was last returned.- Specified by:
getLastReturnTime
in interfacePooledObject<T>
- Returns:
- The time the object was last returned.
-
getLastUsedInstant
public java.time.Instant getLastUsedInstant()
Gets an estimate of the last time this object was used. If the class of the pooled object implementsTrackedUse
, what is returned is the maximum ofTrackedUse.getLastUsedInstant()
andgetLastBorrowTime()
; otherwise this method gives the same value asgetLastBorrowTime()
.- Specified by:
getLastUsedInstant
in interfacePooledObject<T>
- Returns:
- the last Instant this object was used.
-
getLastUsedTime
public long getLastUsedTime()
Gets an estimate of the last time this object was used. If the class of the pooled object implementsTrackedUse
, what is returned is the maximum ofTrackedUse.getLastUsedInstant()
andgetLastBorrowTime()
; otherwise this method gives the same value asgetLastBorrowTime()
.- Specified by:
getLastUsedTime
in interfacePooledObject<T>
- Returns:
- the last time this object was used
-
getObject
public T getObject()
Description copied from interface:PooledObject
Gets the underlying object that is wrapped by this instance ofPooledObject
.- Specified by:
getObject
in interfacePooledObject<T>
- Returns:
- The wrapped object.
-
getState
public PooledObjectState getState()
Gets the state of this object.- Specified by:
getState
in interfacePooledObject<T>
- Returns:
- state
-
invalidate
public void invalidate()
Sets the state toINVALID
.- Specified by:
invalidate
in interfacePooledObject<T>
-
markAbandoned
public void markAbandoned()
Marks the pooled object asABANDONED
.- Specified by:
markAbandoned
in interfacePooledObject<T>
-
markReturning
public void markReturning()
Marks the pooled object asRETURNING
.- Specified by:
markReturning
in interfacePooledObject<T>
-
printStackTrace
public void printStackTrace(java.io.PrintWriter writer)
Description copied from interface:PooledObject
Prints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.- Specified by:
printStackTrace
in interfacePooledObject<T>
- Parameters:
writer
- The destination for the debug output.
-
setLogAbandoned
public void setLogAbandoned(boolean logAbandoned)
Description copied from interface:PooledObject
Sets whether to use abandoned object tracking. If this is true the implementation will need to record the stack trace of the last caller to borrow this object.- Specified by:
setLogAbandoned
in interfacePooledObject<T>
- Parameters:
logAbandoned
- The new configuration setting for abandoned object tracking.
-
setRequireFullStackTrace
public void setRequireFullStackTrace(boolean requireFullStackTrace)
Configures the stack trace generation strategy based on whether or not fully detailed stack traces are required. When set to false, abandoned logs may only include caller class information rather than method names, line numbers, and other normal metadata available in a full stack trace.- Specified by:
setRequireFullStackTrace
in interfacePooledObject<T>
- Parameters:
requireFullStackTrace
- the new configuration setting for abandoned object logging- Since:
- 2.5
-
startEvictionTest
public boolean startEvictionTest()
Description copied from interface:PooledObject
Attempts to place the pooled object in thePooledObjectState.EVICTION
state.- Specified by:
startEvictionTest
in interfacePooledObject<T>
- Returns:
true
if the object was placed in thePooledObjectState.EVICTION
state otherwisefalse
.
-
toString
public java.lang.String toString()
Description copied from interface:PooledObject
Gets a String form of the wrapper for debug purposes. The format is not fixed and may change at any time.- Specified by:
toString
in interfacePooledObject<T>
- Overrides:
toString
in classjava.lang.Object
-
use
public void use()
Description copied from interface:PooledObject
Records the current stack trace as the last time the object was used.- Specified by:
use
in interfacePooledObject<T>
-
-