org.jminor.common.db
Class AbstractDatabase

java.lang.Object
  extended by org.jminor.common.db.AbstractDatabase
All Implemented Interfaces:
Database
Direct Known Subclasses:
DerbyDatabase, H2Database, HSQLDatabase, MySQLDatabase, OracleDatabase, PostgreSQLDatabase, SQLServerDatabase

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 org.jminor.common.db.Database
Database.Statistics
 
Field Summary
 
Fields inherited from interface org.jminor.common.db.Database
DATABASE_EMBEDDED, DATABASE_HOST, DATABASE_IMPLEMENTATION_CLASS, DATABASE_POOL_STATISTICS, DATABASE_PORT, DATABASE_SID, DATABASE_TYPE, DEFAULT_LOGIN_TIMEOUT, DERBY, H2, HSQL, MYSQL, ORACLE, PASSWORD_PROPERTY, POSTGRESQL, SQLSERVER, USER_PROPERTY
 
Constructor Summary
AbstractDatabase(String databaseType)
          Instantiates a new AbstractDatabase using host/port/sid/embedded settings specified by system properties
AbstractDatabase(String databaseType, String host)
          Instantiates a new AbstractDatabase using port/sid/embedded settings specified by system properties
AbstractDatabase(String databaseType, String host, String port)
          Instantiates a new AbstractDatabase using sid/embedded settings specified by system properties
AbstractDatabase(String databaseType, String host, String port, String sid)
          Instantiates a new AbstractDatabase using the embedded settings specified by the system property
AbstractDatabase(String databaseType, String host, String port, String sid, boolean embedded)
          Instantiates a new AbstractDatabase
 
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)
          Returns a string containing authentication info to append to the connection URL, base on the values found in connectionProperties.
 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()
          
protected  int getLoginTimeout()
           
 String getPort()
          
 String getSequenceSQL(String sequenceName)
          
 String getSid()
          
 boolean isEmbedded()
          
 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jminor.common.db.Database
getAutoIncrementValueSQL, getURL, loadDriver
 

Constructor Detail

AbstractDatabase

public AbstractDatabase(String databaseType)
Instantiates a new AbstractDatabase using host/port/sid/embedded settings specified by system properties

Parameters:
databaseType - a string identifying the database type
See Also:
Database.DATABASE_HOST, Database.DATABASE_PORT, Database.DATABASE_SID, Database.DATABASE_EMBEDDED

AbstractDatabase

public AbstractDatabase(String databaseType,
                        String host)
Instantiates a new AbstractDatabase using port/sid/embedded settings specified by system properties

Parameters:
databaseType - a string identifying the database type
host - the database host name or path to the database files in case of an embedded database
See Also:
Database.DATABASE_PORT, Database.DATABASE_SID, Database.DATABASE_EMBEDDED

AbstractDatabase

public AbstractDatabase(String databaseType,
                        String host,
                        String port)
Instantiates a new AbstractDatabase using sid/embedded settings specified by system properties

Parameters:
databaseType - a string identifying the database type
host - the database host name or path to the database files in case of an embedded database
port - the database server port
See Also:
Database.DATABASE_SID, Database.DATABASE_EMBEDDED

AbstractDatabase

public AbstractDatabase(String databaseType,
                        String host,
                        String port,
                        String sid)
Instantiates a new AbstractDatabase using the embedded settings specified by the system property

Parameters:
databaseType - a string identifying the database type
host - the database host name or path to the database files in case of an embedded database
port - the database server port
sid - the service identifier
See Also:
Database.DATABASE_EMBEDDED

AbstractDatabase

public AbstractDatabase(String databaseType,
                        String host,
                        String port,
                        String sid,
                        boolean embedded)
Instantiates a new AbstractDatabase

Parameters:
databaseType - a string identifying the database type
host - the database host name or path to the database files in case of an embedded database
port - the database server port
sid - the service identifier
embedded - true if the database is embedded
Method Detail

getDatabaseType

public final String getDatabaseType()

Specified by:
getDatabaseType in interface Database
Returns:
the name of the dbms in use

getHost

public final String getHost()

Specified by:
getHost in interface Database
Returns:
the database host name

getPort

public final String getPort()

Specified by:
getPort in interface Database
Returns:
the database port

getSid

public final String getSid()

Specified by:
getSid in interface Database
Returns:
the database service id

isEmbedded

public final boolean isEmbedded()

Specified by:
isEmbedded in interface Database
Returns:
true if this database is an embedded one

createConnection

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

Specified by:
createConnection in interface Database
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

supportsNowait

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

Specified by:
supportsNowait in interface Database
Returns:
true if NOWAIT is supported for select for update

supportsIsValid

public boolean supportsIsValid()

Specified by:
supportsIsValid in interface Database
Returns:
true if the dbms supports the Java 6 jdbc call Connection.isValid()

getCheckConnectionQuery

public 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.

Specified by:
getCheckConnectionQuery in interface Database
Returns:
a check connection query
See Also:
Database.supportsIsValid()

shutdownEmbedded

public 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.

Specified by:
shutdownEmbedded in interface Database
Parameters:
connectionProperties - the connection properties

getSequenceSQL

public String getSequenceSQL(String sequenceName)

Specified by:
getSequenceSQL in interface Database
Parameters:
sequenceName - the name of the sequence
Returns:
a query for selecting the next value from the given sequence

getAuthenticationInfo

public String getAuthenticationInfo(Properties connectionProperties)
Returns a string containing authentication info to append to the connection URL, base on the values found in connectionProperties. This default implementation returns the following assuming that connectionProperties contains values for both "user" and "password" keys: user=scott;password=tiger

Specified by:
getAuthenticationInfo in interface Database
Parameters:
connectionProperties - the connection properties
Returns:
a string containing authentication info to append to the connection URL

getErrorMessage

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

Specified by:
getErrorMessage in interface Database
Parameters:
exception - the underlying SQLException
Returns:
the message assigned to the given exception

addConnectionProperties

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

Specified by:
addConnectionProperties in interface Database
Parameters:
properties - the properties map to add to
Returns:
the properties map

getLoginTimeout

protected int getLoginTimeout()
Returns:
the connection timeout in seconds
See Also:
Database.DEFAULT_LOGIN_TIMEOUT