Class Datasource
- All Implemented Interfaces:
AutoCloseable
,Cloneable
The initial connection will only be made and the pool will only be initialized when a connection is obtained for the first time. The instantiation only stores the connection parameters.
A Datasource
also defines the type of database that is used for
all database-independent logic such as sql to java and java to sql type
mappings, query builders, database-based authentication, database-based
scheduling, ... The key that identifies a supported type is the class name of
the jdbc driver.
Once a connection has been obtained from a pooled datasource, modifying its connection parameters is not possible anymore, a new instance has to be created to set the parameters to different values.
- Since:
- 1.0
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionInstantiates a newDatasource
object with no connection information.Datasource
(String driver, String url, String user, String password, int poolSize) Instantiates a newDatasource
object with all the connection parameters that are required.Datasource
(DataSource dataSource, int poolSize) Instantiates a newDatasource
object from a standardjavax.sql.DataSource
.Datasource
(DataSource dataSource, String driver, String user, String password, int poolSize) Instantiates a newDatasource
object from a standardjavax.sql.DataSource
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
cleanup()
Cleans up all connections that have been reserved by this datasource.clone()
Simply clones the instance with the default clone method.void
close()
static void
Closes all the active datasource.boolean
Indicates whether some other object is "equal to" this one.Retrieves the fully qualified class name of the jdbc driver that's used by thisDatasource
.rife.database.capabilities.CapabilitiesCompensator
Retrieves aCapabilitiesCompensator
instance that is able to compensate for certain missing database featuresRetrieves a free database connection.Retrieves the standard datasource that is used by this RIFE datasource to obtain a database connection.Retrieves the fully qualified class name of the jdbc driver that's used by thisDatasource
.Retrieves the password that's used by thisDatasource
.getPool()
Retrieves the instance of the connection pool that is provided by this datasource.int
Retrieves the size of the pool that's used by thisDatasource
.Retrieves the sql to java and java to sql type mapping logic that corresponds to the provide driver class name.getUrl()
Retrieves the connection url that's used by thisDatasource
.getUser()
Retrieves the user that's used by thisDatasource
.int
hashCode()
Returns a hash code value for theDatasource
.boolean
isPooled()
Indicates whether theDatasource
uses a connection pool or notvoid
setDataSource
(DataSource dataSource) Sets the standard datasource that will be used to connect to the database.void
Sets the jdbc driver that will be used to connect to the database.void
setPassword
(String password) Sets the password that will be used to connect to the database.void
setPoolSize
(int poolSize) Sets the size of the connection pool that will be used to connect to the database.void
Sets the connection url that will be used to connect to the database.void
Sets the user that will be used to connect to the database.
-
Field Details
-
sDriverAliases
-
sDriverNames
-
DRIVER_NAME_DERBY
-
DRIVER_NAME_H2
-
DRIVER_NAME_HSQLDB
-
DRIVER_NAME_PGSQL
-
DRIVER_NAME_MYSQL
-
DRIVER_NAME_ORACLE
-
-
Constructor Details
-
Datasource
public Datasource()Instantiates a newDatasource
object with no connection information. The setters need to be used afterward to provide each required parameter before theDatasource
can be used. -
Datasource
Instantiates a newDatasource
object with all the connection parameters that are required.- Parameters:
driver
- the fully-qualified classname of the jdbc driver that will be used to connect to the databaseurl
- the connection url which identifies the database to which the connection will be made, this is entirely driver-dependentuser
- the user that will be used to connect to the databasepassword
- the password that will be used to connect to the databasepoolSize
- the size of the connection pool,0
means that the connections will not be pooled- Since:
- 1.0
-
Datasource
Instantiates a newDatasource
object from a standardjavax.sql.DataSource
.The driver will be detected from the connection that is provided by this
DataSource
. If the driver couldn't be detected, an exception will be thrown upon connect.- Parameters:
dataSource
- the standard datasource that will be used to obtain the connectionpoolSize
- the size of the connection pool,0
means that the connections will not be pooled- Since:
- 1.0
-
Datasource
Instantiates a newDatasource
object from a standardjavax.sql.DataSource
.- Parameters:
dataSource
- the standard datasource that will be used to obtain the connectiondriver
- the fully-qualified classname of the jdbc driver that will be used to provide an identifier for the database abstraction functionalities,null
will let RIFE try to figure it out by itselfuser
- the user that will be used to connect to the databasepassword
- the password that will be used to connect to the databasepoolSize
- the size of the connection pool,0
means that the connections will not be pooled- Since:
- 1.0
-
-
Method Details
-
getConnection
Retrieves a free database connection. If no connection pool is used, a newDbConnection
will always be created, otherwise the first available connection in the pool will be returned.- Returns:
- a free
DbConnection
instance which can be used to create an execute statements - Throws:
DatabaseException
- when errors occured during the creation of a new connection or during the obtainance of a connection from the pool- Since:
- 1.0
-
getDriver
Retrieves the fully qualified class name of the jdbc driver that's used by thisDatasource
.- Returns:
- a
String
with the name of the jdbc driver; ornull
if the driver hasn't been set - Since:
- 1.0
- See Also:
-
getAliasedDriver
Retrieves the fully qualified class name of the jdbc driver that's used by thisDatasource
. Instead of straight retrieval of the internal value, it looks for jdbc driver aliases and changes the driver classname if it's not supported by RIFE, but its alias is.- Returns:
- a
String
with the name of the jdbc driver; ornull
if the driver hasn't been set - Since:
- 1.0
- See Also:
-
setDriver
Sets the jdbc driver that will be used to connect to the database. This has to be a fully qualified class name and will be looked up through reflection. It's not possible to change the driver after a connection has been obtained from a pooled datasource.If the class name can't be resolved, an exception is thrown during the creation of the first connection.
- Parameters:
driver
- aString
with the fully qualified class name of the jdbc driver that will be used- Since:
- 1.0
- See Also:
-
getDataSource
Retrieves the standard datasource that is used by this RIFE datasource to obtain a database connection.- Returns:
- a standard
DataSource
; ornull
if the standard datasource hasn't been set - Since:
- 1.0
- See Also:
-
setDataSource
Sets the standard datasource that will be used to connect to the database.- Parameters:
dataSource
- a standardDataSource
that will be used by this RIFE datasource to obtain a database connection.- Since:
- 1.0
- See Also:
-
getUrl
Retrieves the connection url that's used by thisDatasource
.- Returns:
- a
String
with the connection url; ornull
if the url hasn't been set - Since:
- 1.0
- See Also:
-
setUrl
Sets the connection url that will be used to connect to the database. It's not possible to change the url after a connection has been obtained from a pooled datasource.- Parameters:
url
- aString
with the connection url that will be used- Since:
- 1.0
- See Also:
-
getUser
Retrieves the user that's used by thisDatasource
.- Returns:
- a
String
with the user; ornull
if the user hasn't been set - Since:
- 1.0
- See Also:
-
setUser
Sets the user that will be used to connect to the database. It's not possible to change the user after a connection has been obtained from a pooled datasource.- Parameters:
user
- aString
with the user that will be used- Since:
- 1.0
- See Also:
-
getPassword
Retrieves the password that's used by thisDatasource
.- Returns:
- a
String
with the password; ornull
if the password hasn't been set - Since:
- 1.0
- See Also:
-
setPassword
Sets the password that will be used to connect to the database. It's not possible to change the password after a connection has been obtained from a pooled datasource.- Parameters:
password
- aString
with the password that will be used- Since:
- 1.0
- See Also:
-
getPoolSize
public int getPoolSize()Retrieves the size of the pool that's used by thisDatasource
.- Returns:
- a positive
int
with the size of the pool; or0
if no pool is being used - Since:
- 1.0
- See Also:
-
isPooled
public boolean isPooled()Indicates whether theDatasource
uses a connection pool or not- Returns:
true
if a pool is being used by thisDatasource
; orfalse
otherwise- Since:
- 1.0
- See Also:
-
setPoolSize
public void setPoolSize(int poolSize) Sets the size of the connection pool that will be used to connect to the database.- Parameters:
poolSize
- a positiveint
with the size of the pool, providing0
will disable the use of a connection pool for thisDatasource
.- Since:
- 1.0
- See Also:
-
getSqlConversion
Retrieves the sql to java and java to sql type mapping logic that corresponds to the provide driver class name.- Returns:
- a
SqlConversion
instance that is able to perform the required type conversions for the provided jdbc driver - Throws:
UnsupportedJdbcDriverException
- when the provided jdbc isn't supported- Since:
- 1.0
-
getCapabilitiesCompensator
public rife.database.capabilities.CapabilitiesCompensator getCapabilitiesCompensator() throws UnsupportedJdbcDriverExceptionRetrieves aCapabilitiesCompensator
instance that is able to compensate for certain missing database features- Returns:
- the requested
CapabilitiesCompensator
instance - Throws:
UnsupportedJdbcDriverException
- when the provided jdbc isn't supported- Since:
- 1.0
-
hashCode
public int hashCode()Returns a hash code value for theDatasource
. This method is supported for the benefit of hashtables such as those provided byjava.util.Hashtable
. -
equals
Indicates whether some other object is "equal to" this one. Only the real connection parameters will be taken into account. The size of the pool is not used for the comparison. -
clone
Simply clones the instance with the default clone method. This creates a shallow copy of all fields and the clone will in fact just be another reference to the same underlying data. The independence of each cloned instance is consciously not respected since they rely on resources that can't be cloned. -
cleanup
Cleans up all connections that have been reserved by this datasource.- Throws:
DatabaseException
- when an error occurred during the cleanup- Since:
- 1.0
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
DatabaseException
-
getPool
Retrieves the instance of the connection pool that is provided by this datasource.- Returns:
- the requested instance of
ConnectionPool
-
closeAllActiveDatasources
public static void closeAllActiveDatasources()Closes all the active datasource.This can be used to ensure that no connections or drivers are dangling when an application shuts down. It's already used by the destroy method of the
RifeFilter
.- Since:
- 1.6.1
-