- java.lang.Object
-
- is.codion.common.db.database.AbstractDatabase
-
- All Implemented Interfaces:
Database
,ConnectionFactory
public abstract class AbstractDatabase extends Object implements Database
A default abstract implementation of the Database interface.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface is.codion.common.db.database.Database
Database.SelectForUpdateSupport, Database.Statistics
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
FOR_UPDATE
protected static String
FOR_UPDATE_NOWAIT
-
Fields inherited from interface is.codion.common.db.database.Database
CONNECTION_VALIDITY_CHECK_TIMEOUT, DATABASE_INIT_SCRIPTS, DATABASE_URL, LOGIN_TIMEOUT, PASSWORD_PROPERTY, QUERY_COUNTER_ENABLED, SELECT_FOR_UPDATE_NOWAIT, TRANSACTION_ISOLATION, USER_PROPERTY
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractDatabase(String url)
Instantiates a new AbstractDatabase.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
checkConnectionQuery()
Returns a query to use when checking if the connection is valid, this is used in cases where the dbms does not support the isValid() call.void
closeConnectionPool(String username)
Closes and removes the pool associated with the given uservoid
closeConnectionPools()
Closes and removes all available connection poolsConnectionPoolWrapper
connectionPool(String username)
Collection<String>
connectionPoolUsernames()
void
countQuery(String query)
Counts this query, based on the first character.Connection
createConnection(User user)
Creates a connection for the given user.void
createConnectionPool(ConnectionPoolFactory connectionPoolFactory, User poolUser)
Creates a connection pool for the given user in this database.protected static String
createLimitOffsetClause(Integer limit, Integer offset)
protected static String
createOffsetFetchNextClause(Integer limit, Integer offset)
String
errorMessage(SQLException exception)
Returns a user-friendly error message for the given exception, otherwise simply return the message fromexception
boolean
isAuthenticationException(SQLException exception)
This default implementation returns falseboolean
isConnectionValid(Connection connection)
Validates the given connection.boolean
isReferentialIntegrityException(SQLException exception)
This default implementation returns falseboolean
isTimeoutException(SQLException exception)
Returns true if this exception is a timeout exceptionboolean
isUniqueConstraintException(SQLException exception)
Returns true if this exception is a unique key exceptionprotected int
loginTimeout()
int
maximumNumberOfParameters()
Returns the maximum number of prepared statement parameters, supported by this database.protected static String
removeUrlPrefixOptionsAndParameters(String url, String... prefixes)
String
sequenceQuery(String sequenceName)
Returns a query string for selecting the next value from the given sequence.void
setConnectionProvider(ConnectionProvider connectionProvider)
Sets theConnectionProvider
instance used when creating connections.void
shutdownEmbedded()
This should shut down the database in case it is an embedded one and if that is applicable, such as for Derby.Database.Statistics
statistics()
Returns statistics collected viaDatabase.countQuery(String)
.boolean
subqueryRequiresAlias()
Returns true if this database requires that subqueries by aliased.boolean
supportsIsValid()
Returns true if the dbms supports the Java 6 jdbc callConnection.isValid(int)
.String
url()
Returns the jdbc database url for this connection factory.int
validityCheckTimeout()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface is.codion.common.db.database.Database
autoIncrementQuery, limitOffsetClause, name, selectForUpdateClause
-
-
-
-
Field Detail
-
FOR_UPDATE
protected static final String FOR_UPDATE
- See Also:
- Constant Field Values
-
FOR_UPDATE_NOWAIT
protected static final String FOR_UPDATE_NOWAIT
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractDatabase
protected AbstractDatabase(String url)
Instantiates a new AbstractDatabase.- Parameters:
url
- the jdbc url
-
-
Method Detail
-
url
public final String url()
Description copied from interface:ConnectionFactory
Returns the jdbc database url for this connection factory.- Specified by:
url
in interfaceConnectionFactory
- Returns:
- the jdbc database url for this connection factory.
-
createConnection
public final Connection createConnection(User user) throws DatabaseException
Description copied from interface:ConnectionFactory
Creates a connection for the given user.- Specified by:
createConnection
in interfaceConnectionFactory
- Parameters:
user
- the user for which to create a connection- Returns:
- a new JDBC connection
- Throws:
DatabaseException
- in case of a connection errorAuthenticationException
- in case of an authentication error
-
isConnectionValid
public final boolean isConnectionValid(Connection connection)
Description copied from interface:ConnectionFactory
Validates the given connection.- Specified by:
isConnectionValid
in interfaceConnectionFactory
- Parameters:
connection
- the connection to validate- Returns:
- true if the connection is valid
-
countQuery
public final void countQuery(String query)
Description copied from interface:Database
Counts this query, based on the first character.- Specified by:
countQuery
in interfaceDatabase
- Parameters:
query
- the query to count- See Also:
Database.statistics()
-
statistics
public final Database.Statistics statistics()
Description copied from interface:Database
Returns statistics collected viaDatabase.countQuery(String)
. Note that calling this method resets the counter.- Specified by:
statistics
in interfaceDatabase
- Returns:
- collected statistics.
-
createConnectionPool
public final void createConnectionPool(ConnectionPoolFactory connectionPoolFactory, User poolUser) throws DatabaseException
Description copied from interface:Database
Creates a connection pool for the given user in this database.- Specified by:
createConnectionPool
in interfaceDatabase
- Parameters:
connectionPoolFactory
- the ConnectionPoolFactory implementation to usepoolUser
- the user for which to create a connection pool- Throws:
DatabaseException
- in case of a database exception
-
connectionPool
public final ConnectionPoolWrapper connectionPool(String username)
- Specified by:
connectionPool
in interfaceDatabase
- Parameters:
username
- the username- Returns:
- the connection pool for the given user, null if none exists
-
closeConnectionPool
public final void closeConnectionPool(String username)
Description copied from interface:Database
Closes and removes the pool associated with the given user- Specified by:
closeConnectionPool
in interfaceDatabase
- Parameters:
username
- the username of the pool that should be removed
-
closeConnectionPools
public final void closeConnectionPools()
Description copied from interface:Database
Closes and removes all available connection pools- Specified by:
closeConnectionPools
in interfaceDatabase
-
connectionPoolUsernames
public final Collection<String> connectionPoolUsernames()
- Specified by:
connectionPoolUsernames
in interfaceDatabase
- Returns:
- the usernames of all available connection pools
-
setConnectionProvider
public final void setConnectionProvider(ConnectionProvider connectionProvider)
Description copied from interface:Database
Sets theConnectionProvider
instance used when creating connections.- Specified by:
setConnectionProvider
in interfaceDatabase
- Parameters:
connectionProvider
- the connection provider
-
supportsIsValid
public boolean supportsIsValid()
Description copied from interface:Database
Returns true if the dbms supports the Java 6 jdbc callConnection.isValid(int)
.- Specified by:
supportsIsValid
in interfaceDatabase
- Returns:
- true if the dbms supports the Java 6 jdbc call
Connection.isValid(int)
-
subqueryRequiresAlias
public boolean subqueryRequiresAlias()
Description copied from interface:Database
Returns true if this database requires that subqueries by aliased.- Specified by:
subqueryRequiresAlias
in interfaceDatabase
- Returns:
- true if subqueries require an alias
-
maximumNumberOfParameters
public int maximumNumberOfParameters()
Description copied from interface:Database
Returns the maximum number of prepared statement parameters, supported by this database. The default implementation simply returnsInteger.MAX_VALUE
, as in, no limit.- Specified by:
maximumNumberOfParameters
in interfaceDatabase
- Returns:
- the maximum number of prepared statement parameters, supported by this database.
-
checkConnectionQuery
public String checkConnectionQuery()
Description copied from interface:Database
Returns a query to use when checking if the connection is valid, this is used in cases where the dbms does not support the isValid() call. Returning null is safe if isValid() is supported.- Specified by:
checkConnectionQuery
in interfaceDatabase
- Returns:
- a check connection query
- See Also:
Database.supportsIsValid()
-
validityCheckTimeout
public int validityCheckTimeout()
- Specified by:
validityCheckTimeout
in interfaceDatabase
- Returns:
- the timeout in seconds to use when checking connection validity
-
shutdownEmbedded
public void shutdownEmbedded()
Description copied from interface:Database
This should shut down the database in case it is an embedded one and if that is applicable, such as for Derby.- Specified by:
shutdownEmbedded
in interfaceDatabase
-
sequenceQuery
public String sequenceQuery(String sequenceName)
Description copied from interface:Database
Returns a query string for selecting the next value from the given sequence.- Specified by:
sequenceQuery
in interfaceDatabase
- Parameters:
sequenceName
- the name of the sequence- Returns:
- a query for selecting the next value from the given sequence
-
errorMessage
public String errorMessage(SQLException exception)
Description copied from interface:Database
Returns a user-friendly error message for the given exception, otherwise simply return the message fromexception
- Specified by:
errorMessage
in interfaceDatabase
- Parameters:
exception
- the underlying SQLException- Returns:
- the message assigned to the given exception
-
isAuthenticationException
public boolean isAuthenticationException(SQLException exception)
This default implementation returns false- Specified by:
isAuthenticationException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- false
-
isReferentialIntegrityException
public boolean isReferentialIntegrityException(SQLException exception)
This default implementation returns false- Specified by:
isReferentialIntegrityException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- false
-
isUniqueConstraintException
public boolean isUniqueConstraintException(SQLException exception)
Description copied from interface:Database
Returns true if this exception is a unique key exception- Specified by:
isUniqueConstraintException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- true if this exception is a unique key exception
-
isTimeoutException
public boolean isTimeoutException(SQLException exception)
Description copied from interface:Database
Returns true if this exception is a timeout exception- Specified by:
isTimeoutException
in interfaceDatabase
- Parameters:
exception
- the exception- Returns:
- true if this exception is a timeout exception
-
loginTimeout
protected int loginTimeout()
- Returns:
- the connection timeout in seconds
- See Also:
Database.LOGIN_TIMEOUT
-
createLimitOffsetClause
protected static final String createLimitOffsetClause(Integer limit, Integer offset)
-
createOffsetFetchNextClause
protected static final String createOffsetFetchNextClause(Integer limit, Integer offset)
-
-