Package org.mariadb.jdbc.pool
Class Pool
- java.lang.Object
-
- org.mariadb.jdbc.pool.Pool
-
-
Field Summary
Fields Modifier and Type Field Description private Configuration
conf
private java.util.concurrent.ThreadPoolExecutor
connectionAppender
private java.util.concurrent.BlockingQueue<java.lang.Runnable>
connectionAppenderQueue
private java.util.concurrent.LinkedBlockingDeque<MariaDbInnerPoolConnection>
idleConnections
private static Logger
logger
private java.util.concurrent.atomic.AtomicInteger
pendingRequestNumber
private static int
POOL_STATE_CLOSING
private static int
POOL_STATE_OK
private java.util.concurrent.ScheduledThreadPoolExecutor
poolExecutor
private java.util.concurrent.atomic.AtomicInteger
poolState
private java.lang.String
poolTag
private java.util.concurrent.ScheduledFuture<?>
scheduledFuture
private java.util.concurrent.atomic.AtomicInteger
totalConnection
private int
waitTimeout
-
Constructor Summary
Constructors Constructor Description Pool(Configuration conf, int poolIndex, java.util.concurrent.ScheduledThreadPoolExecutor poolExecutor)
Create pool from configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addConnection()
Create new connection.private void
addConnectionRequest()
Add new connection if needed.void
close()
Close pool and underlying connections.private void
closeAll(java.util.Collection<MariaDbInnerPoolConnection> collection)
private java.lang.String
generatePoolTag(int poolIndex)
long
getActiveConnections()
get pool active connection numberConfiguration
getConf()
Get current configurationlong
getConnectionRequests()
get connection waiting request numberprivate MariaDbInnerPoolConnection
getIdleConnection(long timeout, java.util.concurrent.TimeUnit timeUnit)
Get an existing idle connection in pool.long
getIdleConnections()
get idle connection numberMariaDbInnerPoolConnection
getPoolConnection()
Retrieve new connection.MariaDbInnerPoolConnection
getPoolConnection(java.lang.String username, java.lang.String password)
Get new connection from pool if user and password correspond to pool.java.lang.String
getPoolTag()
return pool taglong
getTotalConnections()
get pool total connectionprivate void
registerJmx()
private void
removeIdleTimeoutConnection()
Removing idle connection.private void
silentAbortConnection(Connection con)
private void
silentCloseConnection(Connection con)
java.util.List<java.lang.Long>
testGetConnectionIdleThreadIds()
For testing purpose only.private void
unRegisterJmx()
-
-
-
Field Detail
-
logger
private static final Logger logger
-
POOL_STATE_OK
private static final int POOL_STATE_OK
- See Also:
- Constant Field Values
-
POOL_STATE_CLOSING
private static final int POOL_STATE_CLOSING
- See Also:
- Constant Field Values
-
poolState
private final java.util.concurrent.atomic.AtomicInteger poolState
-
conf
private final Configuration conf
-
pendingRequestNumber
private final java.util.concurrent.atomic.AtomicInteger pendingRequestNumber
-
totalConnection
private final java.util.concurrent.atomic.AtomicInteger totalConnection
-
idleConnections
private final java.util.concurrent.LinkedBlockingDeque<MariaDbInnerPoolConnection> idleConnections
-
connectionAppender
private final java.util.concurrent.ThreadPoolExecutor connectionAppender
-
connectionAppenderQueue
private final java.util.concurrent.BlockingQueue<java.lang.Runnable> connectionAppenderQueue
-
poolTag
private final java.lang.String poolTag
-
poolExecutor
private final java.util.concurrent.ScheduledThreadPoolExecutor poolExecutor
-
scheduledFuture
private final java.util.concurrent.ScheduledFuture<?> scheduledFuture
-
waitTimeout
private int waitTimeout
-
-
Constructor Detail
-
Pool
public Pool(Configuration conf, int poolIndex, java.util.concurrent.ScheduledThreadPoolExecutor poolExecutor)
Create pool from configuration.- Parameters:
conf
- configuration parserpoolIndex
- pool index to permit distinction of thread namepoolExecutor
- pools common executor
-
-
Method Detail
-
addConnectionRequest
private void addConnectionRequest()
Add new connection if needed. Only one thread create new connection, so new connection request will wait to newly created connection or for a released connection.
-
removeIdleTimeoutConnection
private void removeIdleTimeoutConnection()
Removing idle connection. Close them and recreate connection to reach minimal number of connection.
-
addConnection
private void addConnection() throws java.sql.SQLException
Create new connection.- Throws:
java.sql.SQLException
- if connection creation failed
-
getIdleConnection
private MariaDbInnerPoolConnection getIdleConnection(long timeout, java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedException
Get an existing idle connection in pool.- Returns:
- an IDLE connection.
- Throws:
java.lang.InterruptedException
-
silentCloseConnection
private void silentCloseConnection(Connection con)
-
silentAbortConnection
private void silentAbortConnection(Connection con)
-
getPoolConnection
public MariaDbInnerPoolConnection getPoolConnection() throws java.sql.SQLException
Retrieve new connection. If possible return idle connection, if not, stack connection query, ask for a connection creation, and loop until a connection become idle / a new connection is created.- Returns:
- a connection object
- Throws:
java.sql.SQLException
- if no connection is created when reaching timeout (connectTimeout option)
-
getPoolConnection
public MariaDbInnerPoolConnection getPoolConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException
Get new connection from pool if user and password correspond to pool. If username and password are different from pool, will return a dedicated connection.- Parameters:
username
- usernamepassword
- password- Returns:
- connection
- Throws:
java.sql.SQLException
- if any error occur during connection
-
generatePoolTag
private java.lang.String generatePoolTag(int poolIndex)
-
getConf
public Configuration getConf()
Get current configuration- Returns:
- configuration
-
close
public void close()
Close pool and underlying connections.- Specified by:
close
in interfacejava.lang.AutoCloseable
-
closeAll
private void closeAll(java.util.Collection<MariaDbInnerPoolConnection> collection)
-
getPoolTag
public java.lang.String getPoolTag()
return pool tag- Returns:
- pool tag
-
getActiveConnections
public long getActiveConnections()
Description copied from interface:PoolMBean
get pool active connection number- Specified by:
getActiveConnections
in interfacePoolMBean
- Returns:
- pool active connection number
-
getTotalConnections
public long getTotalConnections()
Description copied from interface:PoolMBean
get pool total connection- Specified by:
getTotalConnections
in interfacePoolMBean
- Returns:
- pool total connection number
-
getIdleConnections
public long getIdleConnections()
Description copied from interface:PoolMBean
get idle connection number- Specified by:
getIdleConnections
in interfacePoolMBean
- Returns:
- idle connection number
-
getConnectionRequests
public long getConnectionRequests()
Description copied from interface:PoolMBean
get connection waiting request number- Specified by:
getConnectionRequests
in interfacePoolMBean
- Returns:
- request number
-
registerJmx
private void registerJmx() throws java.lang.Exception
- Throws:
java.lang.Exception
-
unRegisterJmx
private void unRegisterJmx() throws java.lang.Exception
- Throws:
java.lang.Exception
-
testGetConnectionIdleThreadIds
public java.util.List<java.lang.Long> testGetConnectionIdleThreadIds()
For testing purpose only.- Returns:
- current thread id's
-
-