Class AOPool<C extends AutoCloseable,Ex extends Throwable,I extends Throwable>
- Type Parameters:
Ex- An arbitrary exception type that may be thrown
- All Implemented Interfaces:
Runnable
Two lists of connections are maintained. The first is the list of connections that are ready unused, and the second is the list of connections that are currently checked-out. By using this strategy, an available connection can be found without searching the entire list.
In addition to the global lists, a ThreadLocal list of connections
checked-out by the current thread is maintained. When getting a new connection,
this is used to check against maxConnections instead of checking
the global lists.
Idea: Add some sort of thread-connection affinity, where the same connection slot will be used by the same thread when it is available. This should help cache locality.
Idea: Automatically connect ahead of time in the background. This could hide connection latency on first-use.
- Author:
- AO Industries, Inc.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final longstatic final intstatic final intprotected final LoggerAll warnings are sent here if available, otherwise will be written toSystem.err.static final longFields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()Shuts down the pool, exceptions during close will be logged as a warning and not thrown.protected abstract voidCloses the underlying connection.final intGets the number of connections that are currently busy.Gets either an available connection or creates a new connection, warning when a connection is already used by this thread.getConnection(int maxConnections) Gets either an available connection or creates a new connection.final intGets the number of connections currently connected.protected abstract CCreates a new connection.final longGets the total number of connects for the entire pool.final Loggerfinal intGets the maximum number of connections that have been busy at once.final longGets the maximum age for connections.final intGets the maximum number of connections the pool will create at once.final longfinal longprotected abstract booleanDetermine if the underlying connection is closed.protected voidlogConnection(C conn) Perform any connection logging beforeresetConnection(java.lang.AutoCloseable)and/orclose(java.lang.AutoCloseable).protected abstract ExnewException(String message, Throwable cause) protected abstract InewInterruptedException(String message, Throwable cause) Throws an exception when interrupted, must either throwInterruptedExceptionor re-interrupt the current thread.protected voidprintConnectionStats(Appendable out, boolean isXhtml) Prints additional connection pool details.final voidprintStatisticsHtml(Appendable out, boolean isXhtml) Prints complete statistics about connection pool use.final voidDeprecated.Please specify if is HTML or XHTMLfinal voidprintStatisticsHTML(Appendable out, boolean isXhtml) Deprecated.Please useprintStatisticsHtml(java.lang.Appendable, boolean)instead.protected voidReleases the databaseConnectionto theConnectionpool.final voidreleaseConnection(C connection) Deprecated.Please release to the pool by closing the connection, preferably via try-with-resources.protected abstract voidresetConnection(C conn) Resets the given connection for release back to the pool.final voidrun()The RefreshConnection thread polls every connection in the connection pool.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Field Details
-
DEFAULT_DELAY_TIME
public static final int DEFAULT_DELAY_TIME- See Also:
-
DEFAULT_MAX_IDLE_TIME
public static final int DEFAULT_MAX_IDLE_TIME- See Also:
-
UNLIMITED_MAX_CONNECTION_AGE
public static final long UNLIMITED_MAX_CONNECTION_AGE- See Also:
-
DEFAULT_MAX_CONNECTION_AGE
public static final long DEFAULT_MAX_CONNECTION_AGE- See Also:
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT- See Also:
-
DEFAULT_SOCKET_SO_LINGER
public static final int DEFAULT_SOCKET_SO_LINGER- See Also:
-
logger
All warnings are sent here if available, otherwise will be written toSystem.err.
-
-
Constructor Details
-
AOPool
-
AOPool
-
-
Method Details
-
close
Closes the underlying connection. The connection may or may not already have been reset. The connection may or may not already be closed.Please note, this is distinct from the implementation of
AutoCloseablefor use in try-with-resources.- Throws:
Ex
-
close
public final void close()Shuts down the pool, exceptions during close will be logged as a warning and not thrown. -
getConcurrency
public final int getConcurrency()Gets the number of connections that are currently busy. -
getConnectionCount
public final int getConnectionCount()Gets the number of connections currently connected. -
getConnection
Gets either an available connection or creates a new connection, warning when a connection is already used by this thread.If all the connections in the pool are busy and the pool is at capacity, waits until a connection becomes available.
- Returns:
- Either a reused or new connection
- Throws:
I- when interruptedEx- when an error occurs, or when a thread attempts to allocate more than half the pool- See Also:
-
getConnection
Gets either an available connection or creates a new connection.If all the connections in the pool are busy and the pool is at capacity, waits until a connection becomes available.
- Parameters:
maxConnections- The maximum number of connections expected to be used by the current thread. This should normally be one to avoid potential deadlock.The connection will continue to be considered used by the allocating thread until released (via
AutoCloseable.close(), even if the connection is shared by another thread.- Returns:
- Either a reused or new connection
- Throws:
I- when interruptedEx- when an error occurs, or when a thread attempts to allocate more than half the pool- See Also:
-
getConnectionObject
Creates a new connection.The returned connection must call
release(java.lang.AutoCloseable)onAutoCloseable.close(). This is to support use via try-with-resources, and is distinct fromisClosed(java.lang.AutoCloseable)andclose(java.lang.AutoCloseable), which must both work with the underlying connection.- Throws:
I- when interruptedEx- when error
-
getConnects
public final long getConnects()Gets the total number of connects for the entire pool. -
getMaxConnectionAge
public final long getMaxConnectionAge()Gets the maximum age for connections. -
getMaxConcurrency
public final int getMaxConcurrency()Gets the maximum number of connections that have been busy at once. -
getPoolSize
public final int getPoolSize()Gets the maximum number of connections the pool will create at once. -
getTotalTime
public final long getTotalTime() -
getTransactionCount
public final long getTransactionCount() -
isClosed
Determine if the underlying connection is closed.Please note, this is distinct from the implementation of
AutoCloseablefor use in try-with-resources.- Throws:
Ex
-
printConnectionStats
Prints additional connection pool details. Must have opened the<tbody>.- Throws:
IOException
-
printStatisticsHtml
Prints complete statistics about connection pool use.- Throws:
IOExceptionEx
-
printStatisticsHTML
@Deprecated public final void printStatisticsHTML(Appendable out, boolean isXhtml) throws IOException, Ex Deprecated.Please useprintStatisticsHtml(java.lang.Appendable, boolean)instead.Prints complete statistics about connection pool use.- Throws:
IOExceptionEx
-
printStatisticsHTML
Deprecated.Please specify if is HTML or XHTMLPrints complete statistics about connection pool use in XHTML.- Throws:
IOExceptionEx
-
releaseConnection
Deprecated.Please release to the pool by closing the connection, preferably via try-with-resources.- Throws:
Ex- See Also:
-
release
Releases the databaseConnectionto theConnectionpool.It is safe to call this method more than once, but only the first call will have any affect.
If the connection is not from this pool, no action is taken.
- Throws:
Ex- See Also:
-
logConnection
Perform any connection logging beforeresetConnection(java.lang.AutoCloseable)and/orclose(java.lang.AutoCloseable). This is only called on connections that are not closed.- Throws:
Ex
-
resetConnection
Resets the given connection for release back to the pool.- Throws:
IEx
-
run
public final void run()The RefreshConnection thread polls every connection in the connection pool. If it detects a connection is idle for more than the pre-defined MAX_IDLE_TIME, it closes the connection. It will stop when the pool is flagged as closed. -
newException
-
newInterruptedException
Throws an exception when interrupted, must either throwInterruptedExceptionor re-interrupt the current thread. -
getLogger
-
