|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.modeshape.graph.connector.RepositoryConnectionPool
@ThreadSafe public class RepositoryConnectionPool
A reusable implementation of a managed pool of connections that is optimized for safe concurrent operations.
Nested Class Summary | |
---|---|
protected class |
RepositoryConnectionPool.ConnectionWrapper
|
Field Summary | |
---|---|
static int |
DEFAULT_CORE_POOL_SIZE
The core pool size for default-constructed pools is 1 . |
static long |
DEFAULT_KEEP_ALIVE_TIME_IN_SECONDS
The keep-alive time for connections in default-constructed pools is * * * * 30L seconds. |
static int |
DEFAULT_MAXIMUM_POOL_SIZE
The maximum pool size for default-constructed pools is 10 . |
static long |
DEFAULT_TIME_TO_WAIT
The default time to wait for ping when called from a client. |
static TimeUnit |
DEFAULT_TIME_UNIT
The default TimeUnit to wait for ping when called from a client. |
Constructor Summary | |
---|---|
RepositoryConnectionPool(RepositorySource source)
Create the pool to use the supplied connection factory, which is typically a RepositorySource . |
|
RepositoryConnectionPool(RepositorySource source,
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit)
Create the pool to use the supplied connection factory, which is typically a RepositorySource . |
Method Summary | |
---|---|
protected boolean |
addConnectionIfUnderCorePoolSize()
|
protected int |
addConnectionsIfUnderCorePoolSize()
|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
Method that can be called after shutdown() or shutdownNow() to wait until all connections in use at the
time those methods were called have been closed normally. |
protected void |
closeConnection(RepositoryConnectionPool.ConnectionWrapper wrapper)
Close a connection that is in the pool but no longer in the available connections . |
protected int |
drainUnusedConnections(int count)
|
protected void |
finalize()
Invokes shutdown when this pool is no longer referenced. |
RepositoryConnection |
getConnection()
Get a connection from the pool. |
int |
getCorePoolSize()
Returns the core number of connections. |
int |
getInUseCount()
Returns the approximate number of connections that are currently checked out from the pool. |
long |
getKeepAliveTime()
Returns the connection keep-alive time, which is the amount of time which connections in excess of the core pool size may remain idle before being closed. |
long |
getKeepAliveTime(TimeUnit unit)
Returns the connection keep-alive time, which is the amount of time which connections in excess of the core pool size may remain idle before being closed. |
int |
getMaxFailedAttemptsBeforeError()
|
int |
getMaximumPoolSize()
|
long |
getPingTimeout()
The ping timeout, in seconds. |
long |
getPingTimeoutInNanos()
|
int |
getPoolSize()
Returns the current number of connections in the pool, including those that are checked out (in use) and those that are not being used. |
RepositorySource |
getRepositorySource()
Get the RepositorySource that's used by this pool. |
protected String |
getSourceName()
Get the name of this pool, which delegates to the connection factory. |
long |
getTotalConnectionsCreated()
Get the total number of connections that have been created by this pool. |
long |
getTotalConnectionsUsed()
Get the total number of times connections have been getConnection() used. |
boolean |
getValidateConnectionBeforeUse()
|
boolean |
isRunning()
Return whether this connection pool is running and is able to provide connections . |
boolean |
isShutdown()
Return whether this connection pool is in the process of shutting down or has already been shut down. |
boolean |
isTerminated()
Return true if this pool has completed its termination and no longer has any open connections. |
boolean |
isTerminating()
Returns true if this pool is in the process of terminating after shutdown() or shutdownNow() has been
called but has not completely terminated. |
protected RepositoryConnectionPool.ConnectionWrapper |
newWrappedConnection()
Obtain a new connection wrapped in a RepositoryConnectionPool.ConnectionWrapper . |
boolean |
ping()
Used to get and test a connection in the pool with default wait and time unit values |
boolean |
ping(long time,
TimeUnit unit)
Used to get and test a connection in the pool |
int |
prestartAllCoreConnections()
Starts all core connections, causing them to idly wait for use. |
boolean |
prestartCoreConnection()
Starts a core connection, causing it to idly wait for use. |
protected void |
returnConnection(RepositoryConnectionPool.ConnectionWrapper wrapper)
This method is automatically called by the RepositoryConnectionPool.ConnectionWrapper when it is closed . |
void |
setCorePoolSize(int corePoolSize)
Sets the core number of connections. |
void |
setKeepAliveTime(long time)
Sets the time limit for which connections may remain idle before being closed. |
void |
setKeepAliveTime(long time,
TimeUnit unit)
Sets the time limit for which connections may remain idle before being closed. |
void |
setMaxFailedAttemptsBeforeError(int maxFailedAttemptsBeforeError)
|
void |
setMaximumPoolSize(int maximumPoolSize)
Sets the maximum allowed number of connections. |
void |
setPingTimeout(long pingTimeoutInSeconds)
Sets the ping timeout, in seconds. |
void |
setPingTimeout(long pingTimeout,
TimeUnit unit)
|
void |
setValidateConnectionBeforeUse(boolean validateConnectionBeforeUse)
|
void |
shutdown()
Initiates an orderly shutdown of the pool in which connections that are currently in use are allowed to be used and closed as normal, but no new connections will be created. |
void |
shutdownNow()
Attempts to close all connections in the pool, including those connections currently in use, and prevent the use of other connections. |
protected void |
terminated()
Method invoked when the pool has terminated. |
protected RepositoryConnectionPool.ConnectionWrapper |
validateConnection(RepositoryConnectionPool.ConnectionWrapper connection)
Validate the supplied connection, returning the connection if valid or null if the connection is not valid. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final long DEFAULT_TIME_TO_WAIT
public static final TimeUnit DEFAULT_TIME_UNIT
public static final int DEFAULT_CORE_POOL_SIZE
public static final int DEFAULT_MAXIMUM_POOL_SIZE
public static final long DEFAULT_KEEP_ALIVE_TIME_IN_SECONDS
Constructor Detail |
---|
public RepositoryConnectionPool(RepositorySource source)
RepositorySource
. This constructor
uses the default core pool size
, default maximum pool
size
, and default keep-alive time (in seconds)
.
source
- the source for connections
IllegalArgumentException
- if the connection factory is null or any of the supplied arguments are invalidpublic RepositoryConnectionPool(RepositorySource source, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit)
RepositorySource
.
source
- the source for connectionscorePoolSize
- the number of connections to keep in the pool, even if they are idle.maximumPoolSize
- the maximum number of connections to allow in the pool.keepAliveTime
- when the number of connection is greater than the core, this is the maximum time that excess idle
connections will be kept before terminating.unit
- the time unit for the keepAliveTime argument.
IllegalArgumentException
- if the connection factory is null or any of the supplied arguments are invalidMethod Detail |
---|
public boolean ping(long time, TimeUnit unit) throws InterruptedException
time
- unit
-
InterruptedException
public boolean ping() throws InterruptedException
InterruptedException
public final RepositorySource getRepositorySource()
RepositorySource
that's used by this pool.
protected String getSourceName()
public boolean getValidateConnectionBeforeUse()
public void setValidateConnectionBeforeUse(boolean validateConnectionBeforeUse)
validateConnectionBeforeUse
- Sets validateConnectionBeforeUse to the specified value.public long getPingTimeoutInNanos()
public void setPingTimeout(long pingTimeout, TimeUnit unit)
pingTimeout
- the time to wait for a ping to completeunit
- the time unit of the time argumentpublic long getPingTimeout()
public void setPingTimeout(long pingTimeoutInSeconds)
pingTimeoutInSeconds
- the time to wait for a ping to completepublic int getMaxFailedAttemptsBeforeError()
public void setMaxFailedAttemptsBeforeError(int maxFailedAttemptsBeforeError)
maxFailedAttemptsBeforeError
- Sets maxFailedAttemptsBeforeError to the specified value.public void setKeepAliveTime(long time, TimeUnit unit)
time
- the time to wait. A time value of zero will cause excess connections to terminate immediately after being
returned.unit
- the time unit of the time argument
IllegalArgumentException
- if time less than zerogetKeepAliveTime(java.util.concurrent.TimeUnit)
public long getKeepAliveTime(TimeUnit unit)
unit
- the desired time unit of the result
setKeepAliveTime(long, java.util.concurrent.TimeUnit)
public long getKeepAliveTime()
setKeepAliveTime(long, java.util.concurrent.TimeUnit)
public void setKeepAliveTime(long time)
time
- the time to wait, in seconds. A time value of zero will cause excess connections to terminate immediately after
being returned.
IllegalArgumentException
- if time less than zerogetKeepAliveTime(java.util.concurrent.TimeUnit)
public int getMaximumPoolSize()
public void setMaximumPoolSize(int maximumPoolSize)
maximumPoolSize
- the new maximum
IllegalArgumentException
- if maximumPoolSize less than zero or the core pool size
getMaximumPoolSize()
public int getCorePoolSize()
setCorePoolSize(int)
public void setCorePoolSize(int corePoolSize) throws RepositorySourceException, InterruptedException
corePoolSize
- the new core size
RepositorySourceException
- if there was an error obtaining the new connection
InterruptedException
- if the thread was interrupted during the operation
IllegalArgumentException
- if corePoolSize less than zerogetCorePoolSize()
public int getPoolSize()
public int getInUseCount()
public long getTotalConnectionsCreated()
public long getTotalConnectionsUsed()
getConnection()
used.
public boolean prestartCoreConnection() throws RepositorySourceException, InterruptedException
needed
. This method will return false if all core connections have
already been started.
RepositorySourceException
- if there was an error obtaining the new connection
InterruptedException
- if the thread was interrupted during the operationpublic int prestartAllCoreConnections() throws RepositorySourceException, InterruptedException
needed
.
RepositorySourceException
- if there was an error obtaining the new connection
InterruptedException
- if the thread was interrupted during the operationpublic void shutdown()
Once the pool has been shutdown, it may not be used to get connections
.
SecurityException
- if a security manager exists and shutting down this pool may manipulate threads that the caller
is not permitted to modify because it does not hold RuntimePermission
("modifyThread"),
or the security manager's checkAccess method denies access.shutdownNow()
public void shutdownNow()
SecurityException
- if a security manager exists and shutting down this pool may manipulate threads that the caller
is not permitted to modify because it does not hold RuntimePermission
("modifyThread"),
or the security manager's checkAccess method denies access.shutdown()
public boolean isRunning()
provide connections
. Note that this
method is effectively !isShutdown()
.
isShutdown()
,
isTerminated()
,
isTerminating()
public boolean isShutdown()
true
signals that the pool may no longer be used. Note that this method is effectively
!isRunning()
.
isShutdown()
,
isTerminated()
,
isTerminating()
public boolean isTerminating()
shutdown()
or shutdownNow()
has been
called but has not completely terminated. This method may be useful for debugging. A return of true reported a
sufficient period after shutdown may indicate that submitted tasks have ignored or suppressed interruption, causing this
executor not to properly terminate.
isTerminated()
public boolean isTerminated()
isTerminating()
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
shutdown()
or shutdownNow()
to wait until all connections in use at the
time those methods were called have been closed normally. This method accepts a maximum time duration, after which it will
return even if all connections have not been closed.
timeout
- the maximum time to wait for all connections to be closed and returned to the poolunit
- the time unit for timeout
InterruptedException
- if the thread was interruptedprotected void terminated()
protected void finalize()
finalize
in class Object
public RepositoryConnection getConnection() throws RepositorySourceException
RepositorySourceException
- if there is a problem obtaining a connection
IllegalStateException
- if the factory is not in a state to create or return connectionsprotected void returnConnection(RepositoryConnectionPool.ConnectionWrapper wrapper)
RepositoryConnectionPool.ConnectionWrapper
when it is closed
.
wrapper
- the wrapper to the connection that is being returned to the poolprotected RepositoryConnectionPool.ConnectionWrapper validateConnection(RepositoryConnectionPool.ConnectionWrapper connection) throws InterruptedException
connection
- the connection to be validated; may not be null
InterruptedException
- if the thread is interrupted while validating the connectionprotected RepositoryConnectionPool.ConnectionWrapper newWrappedConnection() throws RepositorySourceException
RepositoryConnectionPool.ConnectionWrapper
. This method does not check whether creating the new
connection would violate the maximum pool size
nor does it add the new connection to the
available connections
(as the caller may want it immediately), but it does increment the
pool size
.
RepositorySourceException
- if there was an error obtaining the new connectionprotected void closeConnection(RepositoryConnectionPool.ConnectionWrapper wrapper)
available connections
. This
method does decrement the pool size
.
wrapper
- the wrapper for the connection to be closedprotected int drainUnusedConnections(int count)
protected boolean addConnectionIfUnderCorePoolSize() throws RepositorySourceException
RepositorySourceException
protected int addConnectionsIfUnderCorePoolSize() throws RepositorySourceException
RepositorySourceException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |