Class BaseDataSource
- java.lang.Object
-
- org.springframework.jdbc.datasource.AbstractDataSource
-
- org.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
- org.springframework.jdbc.datasource.DriverManagerDataSource
-
- com.im.commons.db.datasource.BaseDataSource
-
- All Implemented Interfaces:
java.sql.Wrapper,javax.sql.CommonDataSource,javax.sql.DataSource
public abstract class BaseDataSource extends org.springframework.jdbc.datasource.DriverManagerDataSourceThis is a base class for data sources. Specific DB support modules may provide its ownDataSourceswith DB-specific options.The
DataSourceimplements a connection pool and returns connections allocated from that pool. Theclose()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 thatdataSource.getConnection()will return the sameConnectionduring the whole process, e.g. with respect to an active/opened transaction.The
DataSourcecan beclose()d, which closes all managed connection and detaches the connection pool. The next call togetConnection()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().
-
-
Constructor Summary
Constructors Constructor Description BaseDataSource()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanGetConnection()Wrapper forcanGetConnectionReportingException()method, eliminating throwing of exceptions.booleancanGetConnectionReportingException()Method explores the conectivity of theBaseDataSourceobject.BaseDataSourceclone()Theclone()is overridden to copy over all visible properties.voidclose()Closes all connections and the object pool.protected javax.sql.DataSourcecreateDelegateWithConnectionPool(java.lang.String url, java.util.Properties props)Returns aDataSourceinstance which obtainsConnections from a pool.java.util.PropertiesfilterConnectionProperties(java.util.Properties props)Returns a new instance of properties which contains only properties "related to connection".java.lang.StringgetCatalog()protected java.sql.ConnectiongetConnectionFromDriverManager(java.lang.String url, java.util.Properties props)This is overridden to acquire connections from the pool rather than directly from theDriverManager(see overridden method).org.apache.commons.pool2.impl.GenericObjectPoolConfiggetConnectionPoolConfig()java.util.PropertiesgetConnectionProperties()Return the connection properties to be passed to the Driver, if any.java.lang.StringgetConnectionUrl()DBTypegetDbType()java.lang.StringgetDriverClassName()intgetNumActive()intgetPoolSize()java.lang.StringgetSchema()java.util.PropertiesgetSchemaProperties()TheschemaPropertiesfield very likely does not belong here.protected booleanisConnectionProperty(java.lang.String key)Returns whether the property withkeyis connection property or no.booleanisWrapperFor(java.lang.Class<?> iface)static java.lang.StringobscurePassword(java.lang.String key, java.lang.String value)protected voidprepareNewConnection(java.sql.Connection con)voidsetCatalog(java.lang.String catalog)voidsetDbType(DBType dbType)voidsetDriverClassName(java.lang.String driverClassName)voidsetPrepareConnectionForCartridge(boolean prepareConnectionForCartridge)Set the value of prepareConnectionForCartridge.voidsetSchema(java.lang.String schema)voidsetSchemaProperties(java.util.Properties schemaProperties)java.lang.StringtoString()<T> Tunwrap(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
-
-
-
-
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:
toStringin classjava.lang.Object
-
close
public void close() throws java.sql.SQLExceptionCloses all connections and the object pool. May throw anSQLExceptionif 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()
TheschemaPropertiesfield very likely does not belong here.BaseDataSourceserves only as a container, or holder, for theschemaPropertiesfield so that foreign code can exchange the information stored in this field via aBaseDataSourceinstance.
-
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:
getConnectionPropertiesin classorg.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 withkeyis 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:
getSchemain classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
setSchema
public void setSchema(java.lang.String schema)
- Overrides:
setSchemain classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
getCatalog
public java.lang.String getCatalog()
- Overrides:
getCatalogin classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
setCatalog
public void setCatalog(java.lang.String catalog)
- Overrides:
setCatalogin classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
createDelegateWithConnectionPool
protected javax.sql.DataSource createDelegateWithConnectionPool(java.lang.String url, java.util.Properties props)Returns aDataSourceinstance which obtainsConnections from a pool. The pool is constructed according to current properties.Note: the
getConnectionFromSuperClassapproach to theDriverManagersuperclass 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 URLprops- 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.SQLExceptionThis is overridden to acquire connections from the pool rather than directly from theDriverManager(see overridden method).But, if the pool itself wants to get the connection, it calls
DriverManagerdirectly. This is controlled by thegetConnectionFromSuperClassthread-local variable.- Overrides:
getConnectionFromDriverManagerin classorg.springframework.jdbc.datasource.DriverManagerDataSource- Throws:
java.sql.SQLException
-
setDriverClassName
public void setDriverClassName(java.lang.String driverClassName)
- Overrides:
setDriverClassNamein classorg.springframework.jdbc.datasource.DriverManagerDataSource
-
getDriverClassName
public java.lang.String getDriverClassName()
-
clone
public BaseDataSource clone()
Theclone()is overridden to copy over all visible properties. JDKclone()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:
clonein classjava.lang.Object- Returns:
- A copy of this datasource.
-
isWrapperFor
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException- Specified by:
isWrapperForin interfacejava.sql.Wrapper- Overrides:
isWrapperForin classorg.springframework.jdbc.datasource.AbstractDataSource- Throws:
java.sql.SQLException
-
unwrap
public <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException- Specified by:
unwrapin interfacejava.sql.Wrapper- Overrides:
unwrapin classorg.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.IllegalArgumentExceptionMethod explores the conectivity of theBaseDataSourceobject.- Returns:
- true in case the connection can be established.
- Throws:
java.sql.SQLException- in case connection couldn't be established.java.lang.IllegalArgumentException- forBaseDataSourcebeing invalid.
-
canGetConnection
public boolean canGetConnection()
Wrapper forcanGetConnectionReportingException()method, eliminating throwing of exceptions.- Returns:
- true in case the connection can be established.
-
-