Package com.aoindustries.io
Class AOPool<C,E extends Exception,I extends Exception>
- java.lang.Object
-
- java.lang.Thread
-
- com.aoindustries.io.AOPool<C,E,I>
-
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
AOConnectionPool
public abstract class AOPool<C,E extends Exception,I extends Exception> extends Thread
Reusable generic connection pooling with dynamic flaming tiger feature. 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, aThreadLocallist of connections checked-out by the current thread is maintained. When getting a new connection, this is used to check againmaxConnectionsinstead 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: When connections are closed due to maxConnectionAge, automatically reconnect in the background. This would avoid the latency of the reconnect for the connection-using thread.- Author:
- AO Industries, Inc.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_CONNECT_TIMEOUTstatic intDEFAULT_DELAY_TIMEstatic longDEFAULT_MAX_CONNECTION_AGEstatic intDEFAULT_MAX_IDLE_TIMEstatic intDEFAULT_SOCKET_SO_LINGERprotected LoggerloggerAll warnings are sent here if available, otherwise will be written toSystem.err.static longUNLIMITED_MAX_CONNECTION_AGE-
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()Shuts down the pool, exceptions during close will be logged as a warning and not thrown.protected abstract voidclose(C conn)intgetConcurrency()Gets the number of connections that are currently busy.CgetConnection()Gets a connection, warning of a connection is already used by this thread.CgetConnection(int maxConnections)Gets either an available connection or creates a new connection.intgetConnectionCount()Gets the number of connections currently connected.protected abstract CgetConnectionObject()Creates a new connection.longgetConnects()Gets the total number of connects for the entire pool.LoggergetLogger()intgetMaxConcurrency()longgetMaxConnectionAge()Gets the maximum age for connections.intgetPoolSize()longgetTotalTime()longgetTransactionCount()protected abstract booleanisClosed(C conn)protected abstract EnewException(String message, Throwable cause)protected abstract InewInterruptedException(String message, Throwable cause)protected voidprintConnectionStats(Appendable out, boolean isXhtml)Prints additional connection pool details.voidprintStatisticsHTML(Appendable out)Deprecated.Please specify if is HTML or XHTMLvoidprintStatisticsHTML(Appendable out, boolean isXhtml)Prints complete statistics about connection pool use.voidreleaseConnection(C connection)Releases the databaseConnectionto theConnectionpool.protected abstract voidresetConnection(C conn)voidrun()The RefreshConnection thread polls every connection in the connection pool.-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
DEFAULT_DELAY_TIME
public static final int DEFAULT_DELAY_TIME
- See Also:
- Constant Field Values
-
DEFAULT_MAX_IDLE_TIME
public static final int DEFAULT_MAX_IDLE_TIME
- See Also:
- Constant Field Values
-
UNLIMITED_MAX_CONNECTION_AGE
public static final long UNLIMITED_MAX_CONNECTION_AGE
- See Also:
- Constant Field Values
-
DEFAULT_MAX_CONNECTION_AGE
public static final long DEFAULT_MAX_CONNECTION_AGE
- See Also:
- Constant Field Values
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT
- See Also:
- Constant Field Values
-
DEFAULT_SOCKET_SO_LINGER
public static final int DEFAULT_SOCKET_SO_LINGER
- See Also:
- Constant Field Values
-
logger
protected final Logger logger
All warnings are sent here if available, otherwise will be written toSystem.err.
-
-
Method Detail
-
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
public C getConnection() throws I extends Exception, E extends Exception
Gets a connection, warning of a connection is already used by this thread.- Throws:
I- when interruptedE- when errorI extends Exception- See Also:
getConnection(int)
-
getConnection
public C getConnection(int maxConnections) throws I extends Exception, E extends Exception
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. Due to internalThreadLocaloptimizations, the connection returned must be released by the current thread, it should not be passed off to another thread before release.
-
getConnectionObject
protected abstract C getConnectionObject() throws I extends Exception, E extends Exception
Creates a new connection.
-
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()
-
getPoolSize
public final int getPoolSize()
-
getTotalTime
public final long getTotalTime()
-
getTransactionCount
public final long getTransactionCount()
-
printConnectionStats
protected void printConnectionStats(Appendable out, boolean isXhtml) throws IOException
Prints additional connection pool details. Must have opened the<tbody>.- Throws:
IOException
-
printStatisticsHTML
public final void printStatisticsHTML(Appendable out, boolean isXhtml) throws IOException, E extends Exception
Prints complete statistics about connection pool use.- Throws:
IOExceptionE extends Exception
-
printStatisticsHTML
@Deprecated public final void printStatisticsHTML(Appendable out) throws IOException, E extends Exception
Deprecated.Please specify if is HTML or XHTMLPrints complete statistics about connection pool use in XHTML.- Throws:
IOExceptionE extends Exception
-
releaseConnection
public final void releaseConnection(C connection) throws E extends Exception
Releases the databaseConnectionto theConnectionpool. It is safe to call this method more than once, but only the first call will have any affect and the second release will log a warning.
-
resetConnection
protected abstract void resetConnection(C conn) throws I extends Exception, E extends Exception
-
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.
-
newInterruptedException
protected abstract I newInterruptedException(String message, Throwable cause)
-
getLogger
public final Logger getLogger()
-
-