Class Pool

  • All Implemented Interfaces:
    java.lang.AutoCloseable, PoolMBean

    public class Pool
    extends java.lang.Object
    implements java.lang.AutoCloseable, PoolMBean
    • Field Detail

      • logger

        private static final Logger logger
      • poolState

        private final java.util.concurrent.atomic.AtomicInteger poolState
      • urlParser

        private final UrlParser urlParser
      • options

        private final Options options
      • pendingRequestNumber

        private final java.util.concurrent.atomic.AtomicInteger pendingRequestNumber
      • totalConnection

        private final java.util.concurrent.atomic.AtomicInteger totalConnection
      • 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
      • maxIdleTime

        private int maxIdleTime
      • timeToConnectNanos

        private long timeToConnectNanos
      • connectionTime

        private long connectionTime
    • Constructor Detail

      • Pool

        public Pool​(UrlParser urlParser,
                    int poolIndex,
                    java.util.concurrent.ScheduledThreadPoolExecutor poolExecutor)
        Create pool from configuration.
        Parameters:
        urlParser - configuration parser
        poolIndex - pool index to permit distinction of thread name
        poolExecutor - 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 MariaDbPooledConnection getIdleConnection()
                                                   throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • getIdleConnection

        private MariaDbPooledConnection 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
      • getConnection

        public MariaDbConnection getConnection()
                                        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)
      • getConnection

        public MariaDbConnection getConnection​(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 - username
        password - password
        Returns:
        connection
        Throws:
        java.sql.SQLException - if any error occur during connection
      • generatePoolTag

        private java.lang.String generatePoolTag​(int poolIndex)
      • getUrlParser

        public UrlParser getUrlParser()
      • close

        public void close()
                   throws java.lang.InterruptedException
        Close pool and underlying connections.
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.InterruptedException - if interrupted
      • closeAll

        private void closeAll​(java.util.concurrent.ExecutorService connectionRemover,
                              java.util.Collection<MariaDbPooledConnection> collection)
      • initializePoolGlobalState

        private void initializePoolGlobalState​(MariaDbConnection connection)
                                        throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getPoolTag

        public java.lang.String getPoolTag()
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • 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
      • resetStaticGlobal

        public void resetStaticGlobal()
        JMX method to remove state (will be reinitialized on next connection creation).
        Specified by:
        resetStaticGlobal in interface PoolMBean