org.jminor.common.db
Interface Database

All Known Implementing Classes:
AbstractDatabase, DerbyDatabase, H2Database, HSQLDatabase, MySQLDatabase, OracleDatabase, PostgreSQLDatabase, SQLServerDatabase

public interface Database

Defines DBMS specific functionality as well as basic database configuration settings.


Nested Class Summary
static interface Database.Statistics
          Encapsulates basic database usage statistics.
 
Field Summary
static String DATABASE_EMBEDDED
          Specifies whether or not the database should be run in embedded mode, if applicable
Values: "true"/"false"
Default: "false"
static String DATABASE_HOST
          Specifies the machine hosting the database, in the case of embedded databases this specifies the name of the database
static String DATABASE_IMPLEMENTATION_CLASS
          Specifies the Database implementation class to use in case of a dbms that is not directly supported
static String DATABASE_POOL_STATISTICS
          Specifies whether or not connection pools should collect fine grained performance/usage statistics by default, true or false
static String DATABASE_PORT
          Specifies the database port
static String DATABASE_SID
          Specifies the database sid (used for dbname for MySQL, SQLServer and Derby server connections)
static String DATABASE_TYPE
          Specifies the database type
static int DEFAULT_LOGIN_TIMEOUT
          The default connection login timeout
static String DERBY
          The constant used to denote the Derby database type
static String H2
          The constant used to denote the H2 database type
static String HSQL
          The constant used to denote the HSQL database type
static String MYSQL
          The constant used to denote the MySQL database type
static String ORACLE
          The constant used to denote the Oracle database type
static String PASSWORD_PROPERTY
          The constant used to denote the password value in the connection properties
static String POSTGRESQL
          The constant used to denote the PostgreSQL database type
static String SQLSERVER
          The constant used to denote the Microsoft SQL Server database type
static String USER_PROPERTY
          The constant used to denote the username value in the connection properties
 
Method Summary
 Properties addConnectionProperties(Properties properties)
          Adds any dbms specific connection properties to the given properties map
 Connection createConnection(User user)
          Creates a connection for the given user.
 String getAuthenticationInfo(Properties connectionProperties)
          In the case of embedded databases the user login info can be appended to the connection url, this method should return that string in case the dbms requires it and connectionProperties contains the username and password info, otherwise it should be safe to return null.
 String getAutoIncrementValueSQL(String idSource)
          Returns a query string for retrieving the last automatically generated id from the given id source
 String getCheckConnectionQuery()
          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.
 String getDatabaseType()
           
 String getErrorMessage(SQLException exception)
          Returns a user friendly error message for the given exception, otherwise simply return the message from exception
 String getHost()
           
 String getPort()
           
 String getSequenceSQL(String sequenceName)
           
 String getSid()
           
 String getURL(Properties connectionProperties)
           
 boolean isEmbedded()
           
 void loadDriver()
          Loads the database driver
 void shutdownEmbedded(Properties connectionProperties)
          This should shutdown the database in case it is an embedded one and if that is applicable, such as for Derby.
 boolean supportsIsValid()
           
 boolean supportsNowait()
          Returns true if the dbms supports the select for update NOWAIT option
 

Field Detail

DATABASE_TYPE

static final String DATABASE_TYPE
Specifies the database type

See Also:
DERBY, HSQL, H2, MYSQL, ORACLE, POSTGRESQL, SQLSERVER, Constant Field Values

DATABASE_HOST

static final String DATABASE_HOST
Specifies the machine hosting the database, in the case of embedded databases this specifies the name of the database

See Also:
Constant Field Values

DATABASE_SID

static final String DATABASE_SID
Specifies the database sid (used for dbname for MySQL, SQLServer and Derby server connections)

See Also:
Constant Field Values

DATABASE_PORT

static final String DATABASE_PORT
Specifies the database port

See Also:
Constant Field Values

DATABASE_EMBEDDED

static final String DATABASE_EMBEDDED
Specifies whether or not the database should be run in embedded mode, if applicable
Values: "true"/"false"
Default: "false"

See Also:
Constant Field Values

DATABASE_IMPLEMENTATION_CLASS

static final String DATABASE_IMPLEMENTATION_CLASS
Specifies the Database implementation class to use in case of a dbms that is not directly supported

See Also:
Database, Constant Field Values

DATABASE_POOL_STATISTICS

static final String DATABASE_POOL_STATISTICS
Specifies whether or not connection pools should collect fine grained performance/usage statistics by default, true or false

See Also:
Constant Field Values

ORACLE

static final String ORACLE
The constant used to denote the Oracle database type

See Also:
DATABASE_TYPE, Constant Field Values

MYSQL

static final String MYSQL
The constant used to denote the MySQL database type

See Also:
DATABASE_TYPE, Constant Field Values

POSTGRESQL

static final String POSTGRESQL
The constant used to denote the PostgreSQL database type

See Also:
DATABASE_TYPE, Constant Field Values

SQLSERVER

static final String SQLSERVER
The constant used to denote the Microsoft SQL Server database type

See Also:
DATABASE_TYPE, Constant Field Values

DERBY

static final String DERBY
The constant used to denote the Derby database type

See Also:
DATABASE_TYPE, Constant Field Values

H2

static final String H2
The constant used to denote the H2 database type

See Also:
DATABASE_TYPE, Constant Field Values

HSQL

static final String HSQL
The constant used to denote the HSQL database type

See Also:
DATABASE_TYPE, Constant Field Values

USER_PROPERTY

static final String USER_PROPERTY
The constant used to denote the username value in the connection properties

See Also:
Constant Field Values

PASSWORD_PROPERTY

static final String PASSWORD_PROPERTY
The constant used to denote the password value in the connection properties

See Also:
Constant Field Values

DEFAULT_LOGIN_TIMEOUT

static final int DEFAULT_LOGIN_TIMEOUT
The default connection login timeout

See Also:
DriverManager.setLoginTimeout(int), Constant Field Values
Method Detail

loadDriver

void loadDriver()
                throws ClassNotFoundException
Loads the database driver

Throws:
ClassNotFoundException - in case the driver class in not found

getDatabaseType

String getDatabaseType()
Returns:
the name of the dbms in use

getHost

String getHost()
Returns:
the database host name

getPort

String getPort()
Returns:
the database port

getSid

String getSid()
Returns:
the database service id

isEmbedded

boolean isEmbedded()
Returns:
true if this database is an embedded one

getAutoIncrementValueSQL

String getAutoIncrementValueSQL(String idSource)
Returns a query string for retrieving the last automatically generated id from the given id source

Parameters:
idSource - the source for the id, for example a sequence name or in the case of Derby, the name of the table
Returns:
a query string for retrieving the last auto-increment value from idSource

getSequenceSQL

String getSequenceSQL(String sequenceName)
Parameters:
sequenceName - the name of the sequence
Returns:
a query for selecting the next value from the given sequence

getURL

String getURL(Properties connectionProperties)
Parameters:
connectionProperties - the connection properties, used primarily to provide embedded databases with user info for authentication purposes
Returns:
the database url of the active database, based on system properties

getAuthenticationInfo

String getAuthenticationInfo(Properties connectionProperties)
In the case of embedded databases the user login info can be appended to the connection url, this method should return that string in case the dbms requires it and connectionProperties contains the username and password info, otherwise it should be safe to return null. This method is usually used in getURL() and onDisconnect().

Parameters:
connectionProperties - the connection properties
Returns:
an authentication string to append to the connection url, f.ex. user=scott;password=tiger, null if none is required

shutdownEmbedded

void shutdownEmbedded(Properties connectionProperties)
This should shutdown the database in case it is an embedded one and if that is applicable, such as for Derby.

Parameters:
connectionProperties - the connection properties

supportsIsValid

boolean supportsIsValid()
Returns:
true if the dbms supports the Java 6 jdbc call Connection.isValid()

supportsNowait

boolean supportsNowait()
Returns true if the dbms supports the select for update NOWAIT option

Returns:
true if NOWAIT is supported for select for update

getCheckConnectionQuery

String getCheckConnectionQuery()
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.

Returns:
a check connection query
See Also:
supportsIsValid()

getErrorMessage

String getErrorMessage(SQLException exception)
Returns a user friendly error message for the given exception, otherwise simply return the message from exception

Parameters:
exception - the underlying SQLException
Returns:
the message assigned to the given exception

addConnectionProperties

Properties addConnectionProperties(Properties properties)
Adds any dbms specific connection properties to the given properties map

Parameters:
properties - the properties map to add to
Returns:
the properties map

createConnection

Connection createConnection(User user)
                            throws ClassNotFoundException,
                                   SQLException
Creates a connection for the given user.

Parameters:
user - the user for which to create a connection
Returns:
a Connection
Throws:
ClassNotFoundException - in case the driver class was not on the classpath
SQLException - in case of a connection error