Class BaseDataSource

  • All Implemented Interfaces:
    java.sql.Wrapper, javax.sql.CommonDataSource, javax.sql.DataSource

    public abstract class BaseDataSource
    extends org.springframework.jdbc.datasource.DriverManagerDataSource
    This is a base class for data sources. Specific DB support modules may provide its own DataSources with DB-specific options.

    The DataSource implements a connection pool and returns connections allocated from that pool. The close() method of those connections will only return the connection to the pool for reuse. Excess connections are closed.

    When a connection is returned to a thread, it is always returned to the thread. Those connection maintains a reference counter, so the thread properly returns connection to the pool just by calling close() on it. Current DIF code expects that dataSource.getConnection() will return the same Connection during the whole process, e.g. with respect to an active/opened transaction.

    The DataSource can be close()d, which closes all managed connection and detaches the connection pool. The next call to getConnection() will allocate a new connection pool.

    It is NOT PERMITTED to change credentials or connection URL while a connection is active. All connections must be closed first, preferably by close().

    • Field Summary

      • Fields inherited from class org.springframework.jdbc.datasource.AbstractDataSource

        logger
    • Constructor Summary

      Constructors 
      Constructor Description
      BaseDataSource()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean canGetConnection()
      Wrapper for canGetConnectionReportingException() method, eliminating throwing of exceptions.
      boolean canGetConnectionReportingException()
      Method explores the conectivity of the BaseDataSource object.
      BaseDataSource clone()
      The clone() is overridden to copy over all visible properties.
      void close()
      Closes all connections and the object pool.
      protected javax.sql.DataSource createDelegateWithConnectionPool​(java.lang.String url, java.util.Properties props)
      Returns a DataSource instance which obtains Connections from a pool.
      java.util.Properties filterConnectionProperties​(java.util.Properties props)
      Returns a new instance of properties which contains only properties "related to connection".
      java.lang.String getCatalog()  
      protected java.sql.Connection getConnectionFromDriverManager​(java.lang.String url, java.util.Properties props)
      This is overridden to acquire connections from the pool rather than directly from the DriverManager (see overridden method).
      org.apache.commons.pool2.impl.GenericObjectPoolConfig getConnectionPoolConfig()  
      java.util.Properties getConnectionProperties()
      Return the connection properties to be passed to the Driver, if any.
      java.lang.String getConnectionUrl()  
      DBType getDbType()  
      java.lang.String getDriverClassName()  
      int getNumActive()  
      int getPoolSize()  
      java.lang.String getSchema()  
      java.util.Properties getSchemaProperties()
      The schemaProperties field very likely does not belong here.
      protected boolean isConnectionProperty​(java.lang.String key)
      Returns whether the property with key is connection property or no.
      boolean isWrapperFor​(java.lang.Class<?> iface)  
      static java.lang.String obscurePassword​(java.lang.String key, java.lang.String value)  
      protected void prepareNewConnection​(java.sql.Connection con)  
      void setCatalog​(java.lang.String catalog)  
      void setDbType​(DBType dbType)  
      void setDriverClassName​(java.lang.String driverClassName)  
      void setPrepareConnectionForCartridge​(boolean prepareConnectionForCartridge)
      Set the value of prepareConnectionForCartridge.
      void setSchema​(java.lang.String schema)  
      void setSchemaProperties​(java.util.Properties schemaProperties)  
      java.lang.String toString()  
      <T> T unwrap​(java.lang.Class<T> iface)  
      • Methods inherited from class org.springframework.jdbc.datasource.DriverManagerDataSource

        getConnectionFromDriver
      • Methods inherited from class org.springframework.jdbc.datasource.AbstractDriverBasedDataSource

        getConnection, getConnection, getConnectionFromDriver, getPassword, getUrl, getUsername, setConnectionProperties, setPassword, setUrl, setUsername
      • Methods inherited from class org.springframework.jdbc.datasource.AbstractDataSource

        getLoginTimeout, getLogWriter, getParentLogger, setLoginTimeout, setLogWriter
      • Methods inherited from class java.lang.Object

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

        createShardingKeyBuilder
      • Methods inherited from interface javax.sql.DataSource

        createConnectionBuilder
    • Constructor Detail

      • BaseDataSource

        public BaseDataSource()
    • Method Detail

      • setPrepareConnectionForCartridge

        public void setPrepareConnectionForCartridge​(boolean prepareConnectionForCartridge)
        Set the value of prepareConnectionForCartridge.
        Parameters:
        prepareConnectionForCartridge - The new value of prepareConnectionForCartridge
      • getConnectionPoolConfig

        public org.apache.commons.pool2.impl.GenericObjectPoolConfig getConnectionPoolConfig()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • close

        public void close()
                   throws java.sql.SQLException
        Closes all connections and the object pool. May throw an SQLException if a connection cannot be closed. Other exceptions, such as those from the object pool are only logged and ignored.
        Throws:
        java.sql.SQLException - When closing connections fails.
      • setSchemaProperties

        public void setSchemaProperties​(java.util.Properties schemaProperties)
        See Also:
        getSchemaProperties()
      • getSchemaProperties

        public java.util.Properties getSchemaProperties()
        The schemaProperties field very likely does not belong here. BaseDataSource serves only as a container, or holder, for the schemaProperties field so that foreign code can exchange the information stored in this field via a BaseDataSource instance.
      • getConnectionProperties

        public java.util.Properties getConnectionProperties()
        Return the connection properties to be passed to the Driver, if any.

        The method returns a copy of the properties held by this object. Therefore, changes to the returned properties will not affect the connection properties.
        The method never returns null. Instead of null, it returns empty properties object.

        Overrides:
        getConnectionProperties in class org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
        Returns:
        a copy of connection properties
      • filterConnectionProperties

        public final java.util.Properties filterConnectionProperties​(java.util.Properties props)
        Returns a new instance of properties which contains only properties "related to connection". Whether a property relates to connection, is up to individual data sources implementations.
        Parameters:
        props - original properties
        Returns:
        filtered properties
        Since:
        15.4.13
      • isConnectionProperty

        protected boolean isConnectionProperty​(java.lang.String key)
        Returns whether the property with key is connection property or no.
        Parameters:
        key - the property key
        Returns:
        whether the property is connection property
        Since:
        15.4.13
      • getConnectionUrl

        public java.lang.String getConnectionUrl()
      • getSchema

        public java.lang.String getSchema()
        Overrides:
        getSchema in class org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
      • setSchema

        public void setSchema​(java.lang.String schema)
        Overrides:
        setSchema in class org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
      • getCatalog

        public java.lang.String getCatalog()
        Overrides:
        getCatalog in class org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
      • setCatalog

        public void setCatalog​(java.lang.String catalog)
        Overrides:
        setCatalog in class org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
      • createDelegateWithConnectionPool

        protected javax.sql.DataSource createDelegateWithConnectionPool​(java.lang.String url,
                                                                        java.util.Properties props)
        Returns a DataSource instance which obtains Connections from a pool. The pool is constructed according to current properties.

        Note: the getConnectionFromSuperClass approach to the DriverManager superclass is used to overcome DriverManager's checks whether the calling Class may access the particular driver. Spring module is already allowed to access any and all JDBC driver modules, so I tunnel the call through Spring instead of creating further module dependencies.

        Parameters:
        url - database URL
        props - connection properties
        Returns:
        new delegate
        See Also:
        DriverManagerConnectionFactory(String, Properties)
      • obscurePassword

        public static java.lang.String obscurePassword​(java.lang.String key,
                                                       java.lang.String value)
      • getNumActive

        public int getNumActive()
      • getPoolSize

        public int getPoolSize()
      • prepareNewConnection

        protected void prepareNewConnection​(java.sql.Connection con)
      • getConnectionFromDriverManager

        protected java.sql.Connection getConnectionFromDriverManager​(java.lang.String url,
                                                                     java.util.Properties props)
                                                              throws java.sql.SQLException
        This is overridden to acquire connections from the pool rather than directly from the DriverManager (see overridden method).

        But, if the pool itself wants to get the connection, it calls DriverManager directly. This is controlled by the getConnectionFromSuperClass thread-local variable.

        Overrides:
        getConnectionFromDriverManager in class org.springframework.jdbc.datasource.DriverManagerDataSource
        Throws:
        java.sql.SQLException
      • setDriverClassName

        public void setDriverClassName​(java.lang.String driverClassName)
        Overrides:
        setDriverClassName in class org.springframework.jdbc.datasource.DriverManagerDataSource
      • getDriverClassName

        public java.lang.String getDriverClassName()
      • clone

        public BaseDataSource clone()
        The clone() is overridden to copy over all visible properties. JDK clone() is not used because only the definition of the datasource not connection pools, etc. should be copied. Properties are copied using setter methods on the clone so that subclasses may override the behaviour.

        Subclasses, which define more properties MUST override.

        Overrides:
        clone in class java.lang.Object
        Returns:
        A copy of this datasource.
      • isWrapperFor

        public boolean isWrapperFor​(java.lang.Class<?> iface)
                             throws java.sql.SQLException
        Specified by:
        isWrapperFor in interface java.sql.Wrapper
        Overrides:
        isWrapperFor in class org.springframework.jdbc.datasource.AbstractDataSource
        Throws:
        java.sql.SQLException
      • unwrap

        public <T> T unwrap​(java.lang.Class<T> iface)
                     throws java.sql.SQLException
        Specified by:
        unwrap in interface java.sql.Wrapper
        Overrides:
        unwrap in class org.springframework.jdbc.datasource.AbstractDataSource
        Throws:
        java.sql.SQLException
      • getDbType

        public DBType getDbType()
      • setDbType

        public void setDbType​(DBType dbType)
      • canGetConnectionReportingException

        public boolean canGetConnectionReportingException()
                                                   throws java.sql.SQLException,
                                                          java.lang.IllegalArgumentException
        Method explores the conectivity of the BaseDataSource object.
        Returns:
        true in case the connection can be established.
        Throws:
        java.sql.SQLException - in case connection couldn't be established.
        java.lang.IllegalArgumentException - for BaseDataSource being invalid.
      • canGetConnection

        public boolean canGetConnection()
        Wrapper for canGetConnectionReportingException() method, eliminating throwing of exceptions.
        Returns:
        true in case the connection can be established.