Class MariaDbPoolDataSource

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.sql.Wrapper, javax.sql.CommonDataSource, javax.sql.DataSource, javax.sql.XADataSource

    public class MariaDbPoolDataSource
    extends java.lang.Object
    implements javax.sql.DataSource, javax.sql.XADataSource, java.io.Closeable, java.lang.AutoCloseable
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkNotInitialized()  
      void close()
      Close datasource.
      java.sql.Connection getConnection()
      Attempts to establish a connection with the data source that this DataSource object represents.
      java.sql.Connection getConnection​(java.lang.String username, java.lang.String password)
      Attempts to establish a connection with the data source that this DataSource object represents.
      java.lang.String getDatabaseName()
      Gets the name of the database.
      int getLoginTimeout()
      Gets the maximum time in seconds that this data source can wait while attempting to connect to a database.
      java.io.PrintWriter getLogWriter()
      Retrieves the log writer for this DataSource object.
      int getMaxIdleTime()
      Max time a connection can be idle.
      int getMaxPoolSize()
      Pool maximum connection size.
      int getMinPoolSize()
      Get minimum pool size (pool will grow at creation untile reaching this size).
      java.util.logging.Logger getParentLogger()  
      java.lang.String getPoolName()  
      java.lang.Integer getPoolValidMinDelay()
      If connection has been used in less time than poolValidMinDelay, then no connection validation will be done (0=mean validation every time).
      int getPort()
      Returns the port number.
      int getPortNumber()
      Returns the port number.
      java.lang.String getServerName()
      Returns the name of the database server.
      java.lang.Boolean getStaticGlobal()  
      protected UrlParser getUrlParser()
      For testing purpose only.
      java.lang.String getUser()
      Gets the username.
      javax.sql.XAConnection getXAConnection()  
      javax.sql.XAConnection getXAConnection​(java.lang.String user, java.lang.String password)  
      void initialize()
      Initialize pool.
      private void initializeUrlParser()  
      boolean isWrapperFor​(java.lang.Class<?> interfaceOrWrapper)
      Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.
      void setDatabaseName​(java.lang.String database)
      Sets the database name.
      void setLoginTimeout​(int seconds)
      Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.
      void setLogWriter​(java.io.PrintWriter out)
      Sets the log writer for this DataSource object to the given java.io.PrintWriter object.
      void setMaxIdleTime​(int maxIdleTime)  
      void setMaxPoolSize​(int maxPoolSize)  
      void setMinPoolSize​(int minPoolSize)  
      void setPassword​(java.lang.String password)
      Sets the password.
      void setPoolName​(java.lang.String poolName)  
      void setPoolValidMinDelay​(java.lang.Integer poolValidMinDelay)  
      void setPort​(int port)
      Sets the database port.
      void setPortNumber​(int port)
      Sets the port number.
      void setServerName​(java.lang.String serverName)
      Sets the server name.
      void setStaticGlobal​(java.lang.Boolean staticGlobal)  
      void setUrl​(java.lang.String url)
      Sets the connection string URL.
      void setUser​(java.lang.String user)
      Sets the username.
      void testForceMaxIdleTime​(int maxIdleTime)
      Permit to create test that doesn't wait for maxIdleTime minimum value of 60 seconds.
      java.util.List<java.lang.Long> testGetConnectionIdleThreadIds()
      Get current idle threads.
      Pool testGetPool()
      Get pool.
      <T> T unwrap​(java.lang.Class<T> iface)
      Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface javax.sql.CommonDataSource

        createShardingKeyBuilder
      • Methods inherited from interface javax.sql.DataSource

        createConnectionBuilder
      • Methods inherited from interface javax.sql.XADataSource

        createXAConnectionBuilder
    • Field Detail

      • pool

        private Pool pool
      • hostname

        private java.lang.String hostname
      • port

        private java.lang.Integer port
      • connectTimeout

        private java.lang.Integer connectTimeout
      • database

        private java.lang.String database
      • url

        private java.lang.String url
      • user

        private java.lang.String user
      • password

        private java.lang.String password
      • poolName

        private java.lang.String poolName
      • maxPoolSize

        private java.lang.Integer maxPoolSize
      • minPoolSize

        private java.lang.Integer minPoolSize
      • maxIdleTime

        private java.lang.Integer maxIdleTime
      • staticGlobal

        private java.lang.Boolean staticGlobal
      • poolValidMinDelay

        private java.lang.Integer poolValidMinDelay
    • Constructor Detail

      • MariaDbPoolDataSource

        public MariaDbPoolDataSource​(java.lang.String hostname,
                                     int port,
                                     java.lang.String database)
        Constructor.
        Parameters:
        hostname - hostname (ipv4, ipv6, dns name)
        port - server port
        database - database name
      • MariaDbPoolDataSource

        public MariaDbPoolDataSource​(java.lang.String url)
      • MariaDbPoolDataSource

        public MariaDbPoolDataSource()
        Default constructor. hostname will be localhost, port 3306.
    • Method Detail

      • getDatabaseName

        public java.lang.String getDatabaseName()
        Gets the name of the database.
        Returns:
        the name of the database for this data source
      • setDatabaseName

        public void setDatabaseName​(java.lang.String database)
                             throws java.sql.SQLException
        Sets the database name.
        Parameters:
        database - the name of the database
        Throws:
        java.sql.SQLException - if error in URL
      • checkNotInitialized

        private void checkNotInitialized()
                                  throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getUser

        public java.lang.String getUser()
        Gets the username.
        Returns:
        the username to use when connecting to the database
      • setUser

        public void setUser​(java.lang.String user)
                     throws java.sql.SQLException
        Sets the username.
        Parameters:
        user - the username
        Throws:
        java.sql.SQLException - if error in URL
      • setPassword

        public void setPassword​(java.lang.String password)
                         throws java.sql.SQLException
        Sets the password.
        Parameters:
        password - the password
        Throws:
        java.sql.SQLException - if error in URL
      • getPort

        public int getPort()
        Returns the port number.
        Returns:
        the port number
      • setPort

        public void setPort​(int port)
                     throws java.sql.SQLException
        Sets the database port.
        Parameters:
        port - the port
        Throws:
        java.sql.SQLException - if error in URL
      • getPortNumber

        public int getPortNumber()
        Returns the port number.
        Returns:
        the port number
      • setPortNumber

        public void setPortNumber​(int port)
                           throws java.sql.SQLException
        Sets the port number.
        Parameters:
        port - the port
        Throws:
        java.sql.SQLException - if error in URL
        See Also:
        setPort(int)
      • setUrl

        public void setUrl​(java.lang.String url)
                    throws java.sql.SQLException
        Sets the connection string URL.
        Parameters:
        url - the connection string
        Throws:
        java.sql.SQLException - if error in URL
      • getServerName

        public java.lang.String getServerName()
        Returns the name of the database server.
        Returns:
        the name of the database server
      • setServerName

        public void setServerName​(java.lang.String serverName)
                           throws java.sql.SQLException
        Sets the server name.
        Parameters:
        serverName - the server name
        Throws:
        java.sql.SQLException - if error in URL
      • getConnection

        public java.sql.Connection getConnection()
                                          throws java.sql.SQLException
        Attempts to establish a connection with the data source that this DataSource object represents.
        Specified by:
        getConnection in interface javax.sql.DataSource
        Returns:
        a connection to the data source
        Throws:
        java.sql.SQLException - if a database access error occurs
      • getConnection

        public java.sql.Connection getConnection​(java.lang.String username,
                                                 java.lang.String password)
                                          throws java.sql.SQLException
        Attempts to establish a connection with the data source that this DataSource object represents.
        Specified by:
        getConnection in interface javax.sql.DataSource
        Parameters:
        username - the database user on whose behalf the connection is being made
        password - the user's password
        Returns:
        a connection to the data source
        Throws:
        java.sql.SQLException - if a database access error occurs
      • getLogWriter

        public java.io.PrintWriter getLogWriter()
        Retrieves the log writer for this DataSource object.

        The log writer is a character output stream to which all logging and tracing messages for this data source will be printed. This includes messages printed by the methods of this object, messages printed by methods of other objects manufactured by this object, and so on. Messages printed to a data source specific log writer are not printed to the log writer associated with the java.sql.DriverManager class.

        When a DataSource object is created, the log writer is initially null; in other words, the default is for logging to be disabled.

        Specified by:
        getLogWriter in interface javax.sql.CommonDataSource
        Specified by:
        getLogWriter in interface javax.sql.DataSource
        Specified by:
        getLogWriter in interface javax.sql.XADataSource
        Returns:
        the log writer for this data source or null if logging is disabled
        See Also:
        setLogWriter(java.io.PrintWriter)
      • setLogWriter

        public void setLogWriter​(java.io.PrintWriter out)
        Sets the log writer for this DataSource object to the given java.io.PrintWriter object.

        The log writer is a character output stream to which all logging and tracing messages for this data source will be printed. This includes messages printed by the methods of this object, messages printed by methods of other objects manufactured by this object, and so on. Messages printed to a data source- specific log writer are not printed to the log writer associated with the java.sql.DriverManager class. When a DataSource object is created the log writer is initially null; in other words, the default is for logging to be disabled.

        Specified by:
        setLogWriter in interface javax.sql.CommonDataSource
        Specified by:
        setLogWriter in interface javax.sql.DataSource
        Specified by:
        setLogWriter in interface javax.sql.XADataSource
        Parameters:
        out - the new log writer; to disable logging, set to null
        Since:
        1.4
        See Also:
        getLogWriter()
      • getLoginTimeout

        public int getLoginTimeout()
        Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. A value of zero means that the timeout is the default system timeout if there is one; otherwise, it means that there is no timeout. When a DataSource object is created, the login timeout is initially zero.
        Specified by:
        getLoginTimeout in interface javax.sql.CommonDataSource
        Specified by:
        getLoginTimeout in interface javax.sql.DataSource
        Specified by:
        getLoginTimeout in interface javax.sql.XADataSource
        Returns:
        the data source login time limit
        Since:
        1.4
        See Also:
        setLoginTimeout(int)
      • setLoginTimeout

        public void setLoginTimeout​(int seconds)
                             throws java.sql.SQLException
        Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. A value of zero specifies that the timeout is the default system timeout if there is one; otherwise, it specifies that there is no timeout. When a DataSource object is created, the login timeout is initially zero.
        Specified by:
        setLoginTimeout in interface javax.sql.CommonDataSource
        Specified by:
        setLoginTimeout in interface javax.sql.DataSource
        Specified by:
        setLoginTimeout in interface javax.sql.XADataSource
        Parameters:
        seconds - the data source login time limit
        Throws:
        java.sql.SQLException - if a database access error occurs.
        Since:
        1.4
        See Also:
        getLoginTimeout()
      • unwrap

        public <T> T unwrap​(java.lang.Class<T> iface)
                     throws java.sql.SQLException
        Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

        If the receiver implements the interface then the result is the receiver or a proxy for the receiver. If the receiver is a wrapper and the wrapped object implements the interface then the result is the wrapped object or a proxy for the wrapped object. Otherwise return the the result of calling unwrap recursively on the wrapped object or a proxy for that result. If the receiver is not a wrapper and does not implement the interface, then an SQLException is thrown.

        Specified by:
        unwrap in interface java.sql.Wrapper
        Parameters:
        iface - A Class defining an interface that the result must implement.
        Returns:
        an object that implements the interface. May be a proxy for the actual implementing object.
        Throws:
        java.sql.SQLException - If no object found that implements the interface
        Since:
        1.6
      • isWrapperFor

        public boolean isWrapperFor​(java.lang.Class<?> interfaceOrWrapper)
                             throws java.sql.SQLException
        Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. Returns false otherwise. If this implements the interface then return true, else if this is a wrapper then return the result of recursively calling isWrapperFor on the wrapped object. If this does not implement the interface and is not a wrapper, return false. This method should be implemented as a low-cost operation compared to unwrap so that callers can use this method to avoid expensive unwrap calls that may fail. If this method returns true then calling unwrap with the same argument should succeed.
        Specified by:
        isWrapperFor in interface java.sql.Wrapper
        Parameters:
        interfaceOrWrapper - a Class defining an interface.
        Returns:
        true if this implements the interface or directly or indirectly wraps an object that does.
        Throws:
        java.sql.SQLException - if an error occurs while determining whether this is a wrapper for an object with the given interface.
        Since:
        1.6
      • getXAConnection

        public javax.sql.XAConnection getXAConnection()
                                               throws java.sql.SQLException
        Specified by:
        getXAConnection in interface javax.sql.XADataSource
        Throws:
        java.sql.SQLException
      • getXAConnection

        public javax.sql.XAConnection getXAConnection​(java.lang.String user,
                                                      java.lang.String password)
                                               throws java.sql.SQLException
        Specified by:
        getXAConnection in interface javax.sql.XADataSource
        Throws:
        java.sql.SQLException
      • getParentLogger

        public java.util.logging.Logger getParentLogger()
        Specified by:
        getParentLogger in interface javax.sql.CommonDataSource
      • getUrlParser

        protected UrlParser getUrlParser()
        For testing purpose only.
        Returns:
        current url parser.
      • getPoolName

        public java.lang.String getPoolName()
      • setPoolName

        public void setPoolName​(java.lang.String poolName)
                         throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getMaxPoolSize

        public int getMaxPoolSize()
        Pool maximum connection size.
        Returns:
        current value.
      • setMaxPoolSize

        public void setMaxPoolSize​(int maxPoolSize)
                            throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getMinPoolSize

        public int getMinPoolSize()
        Get minimum pool size (pool will grow at creation untile reaching this size). Null mean use the pool maximum pool size.
        Returns:
        current value.
      • setMinPoolSize

        public void setMinPoolSize​(int minPoolSize)
                            throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getMaxIdleTime

        public int getMaxIdleTime()
        Max time a connection can be idle.
        Returns:
        current value.
      • setMaxIdleTime

        public void setMaxIdleTime​(int maxIdleTime)
                            throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getStaticGlobal

        public java.lang.Boolean getStaticGlobal()
      • setStaticGlobal

        public void setStaticGlobal​(java.lang.Boolean staticGlobal)
      • getPoolValidMinDelay

        public java.lang.Integer getPoolValidMinDelay()
        If connection has been used in less time than poolValidMinDelay, then no connection validation will be done (0=mean validation every time).
        Returns:
        current value of poolValidMinDelay
      • setPoolValidMinDelay

        public void setPoolValidMinDelay​(java.lang.Integer poolValidMinDelay)
      • initializeUrlParser

        private void initializeUrlParser()
                                  throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • close

        public void close()
        Close datasource.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • initialize

        public void initialize()
                        throws java.sql.SQLException
        Initialize pool.
        Throws:
        java.sql.SQLException - if connection string has error
      • testGetConnectionIdleThreadIds

        public java.util.List<java.lang.Long> testGetConnectionIdleThreadIds()
        Get current idle threads. !! For testing purpose only !!
        Returns:
        current thread id's
      • testForceMaxIdleTime

        public void testForceMaxIdleTime​(int maxIdleTime)
                                  throws java.sql.SQLException
        Permit to create test that doesn't wait for maxIdleTime minimum value of 60 seconds. !! For testing purpose only !!
        Parameters:
        maxIdleTime - forced value of maxIdleTime option.
        Throws:
        java.sql.SQLException - if connection string has error
      • testGetPool

        public Pool testGetPool()
        Get pool. !! For testing purpose only !!
        Returns:
        pool