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.DriverManagerDataSource
This is a base class for data sources. Specific DB support modules may provide its ownDataSources
with DB-specific options.The
DataSource
implements 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 sameConnection
during the whole process, e.g. with respect to an active/opened transaction.The
DataSource
can 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 boolean
canGetConnection()
Wrapper forcanGetConnectionReportingException()
method, eliminating throwing of exceptions.boolean
canGetConnectionReportingException()
Method explores the conectivity of theBaseDataSource
object.BaseDataSource
clone()
Theclone()
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 aDataSource
instance which obtainsConnection
s 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 theDriverManager
(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()
TheschemaProperties
field very likely does not belong here.protected boolean
isConnectionProperty(java.lang.String key)
Returns whether the property withkey
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
-
-
-
-
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 classjava.lang.Object
-
close
public void close() throws java.sql.SQLException
Closes all connections and the object pool. May throw anSQLException
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()
TheschemaProperties
field very likely does not belong here.BaseDataSource
serves only as a container, or holder, for theschemaProperties
field so that foreign code can exchange the information stored in this field via aBaseDataSource
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 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 withkey
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 classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
setSchema
public void setSchema(java.lang.String schema)
- Overrides:
setSchema
in classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
getCatalog
public java.lang.String getCatalog()
- Overrides:
getCatalog
in classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
setCatalog
public void setCatalog(java.lang.String catalog)
- Overrides:
setCatalog
in classorg.springframework.jdbc.datasource.AbstractDriverBasedDataSource
-
createDelegateWithConnectionPool
protected javax.sql.DataSource createDelegateWithConnectionPool(java.lang.String url, java.util.Properties props)
Returns aDataSource
instance which obtainsConnection
s from a pool. The pool is constructed according to current properties.Note: the
getConnectionFromSuperClass
approach to theDriverManager
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 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.SQLException
This 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
DriverManager
directly. This is controlled by thegetConnectionFromSuperClass
thread-local variable.- Overrides:
getConnectionFromDriverManager
in classorg.springframework.jdbc.datasource.DriverManagerDataSource
- Throws:
java.sql.SQLException
-
setDriverClassName
public void setDriverClassName(java.lang.String driverClassName)
- Overrides:
setDriverClassName
in 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:
clone
in classjava.lang.Object
- Returns:
- A copy of this datasource.
-
isWrapperFor
public boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException
- Specified by:
isWrapperFor
in interfacejava.sql.Wrapper
- Overrides:
isWrapperFor
in 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:
unwrap
in interfacejava.sql.Wrapper
- Overrides:
unwrap
in 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.IllegalArgumentException
Method explores the conectivity of theBaseDataSource
object.- Returns:
- true in case the connection can be established.
- Throws:
java.sql.SQLException
- in case connection couldn't be established.java.lang.IllegalArgumentException
- forBaseDataSource
being invalid.
-
canGetConnection
public boolean canGetConnection()
Wrapper forcanGetConnectionReportingException()
method, eliminating throwing of exceptions.- Returns:
- true in case the connection can be established.
-
-