org.opensubsystems.core.persist.db
Class DatabaseImpl

java.lang.Object
  extended by org.opensubsystems.core.persist.db.DatabaseImpl
All Implemented Interfaces:
Database
Direct Known Subclasses:
DB2DatabaseImpl, HsqlDBDatabaseImpl, MSSQLDatabaseImpl, MySQLDatabaseImpl, OracleDatabaseImpl, PostgreSQLDatabaseImpl, SapDBDatabaseImpl, SybaseDatabaseImpl

public abstract class DatabaseImpl
extends java.lang.Object
implements Database

Base class for all classes encapsulating various differences in behaviour for different database. Most likely one project will use only one database therefore this class contains factory methods to instantiate and return default instance of database even though it still allows multiple instances of database to exist since all attributes are non-static.

Version:
$Id: DatabaseImpl.java,v 1.35 2007/01/07 06:14:18 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.28 2005/10/10 09:00:32 bastafidli

Field Summary
protected  boolean m_bDatabaseSchemaInitialized
          Flag signaling that the database schema was initialized;
protected  boolean m_bDatabaseStarted
          Flag if database was started.
protected  boolean m_bDatabaseStartInProgress
          Flag signaling that the database start is in progress.
protected  VersionedDatabaseSchema m_vdsSchema
          Database schema for this database.
static int MAX_SAFE_LENGTH
          Number specifying maximal safe length for members within the IN () clause Because there is limitation for sql statement length (in sap db is default 64 kb) and in() expression can contain lot of members.
 
Fields inherited from interface org.opensubsystems.core.persist.db.Database
BATCH_ITERATOR, DB2_DATABASE_TYPE, DB2_DATABASE_TYPE_IDENTIFIER, HSQLDB_DATABASE_TYPE, HSQLDB_DATABASE_TYPE_IDENTIFIER, MAXDB_DATABASE_TYPE, MAXDB_DATABASE_TYPE_IDENTIFIER, MSSQL_DATABASE_TYPE, MSSQL_DATABASE_TYPE_IDENTIFIER, MYSQL_DATABASE_TYPE, MYSQL_DATABASE_TYPE_IDENTIFIER, ORACLE_DATABASE_TYPE, ORACLE_DATABASE_TYPE_IDENTIFIER, POSTGRESQL_DATABASE_TYPE, POSTGRESQL_DATABASE_TYPE_IDENTIFIER, SAPDB_DATABASE_TYPE, SAPDB_DATABASE_TYPE_IDENTIFIER, SYBASE_DATABASE_TYPE, SYBASE_DATABASE_TYPE_IDENTIFIER
 
Constructor Summary
protected DatabaseImpl()
          Default constructor.
 
Method Summary
 void add(java.lang.Class clsSchema)
          Add new schema to the database schema by specifying the generic schema class or interface.
 void add(DatabaseSchema dsSchema)
          Add new schema to the database schema.
abstract  void createDatabaseInstance()
          Method for creating database instance.
protected abstract  void createUser(java.sql.Connection cntAdminDBConnection)
          Create database user which will be used by connection pool to access the database.
protected  java.sql.Connection getAdminConnection(boolean bAutoCommit)
          Get connection with administration priviledges.
 java.lang.String getConnectionTestStatement()
          Get very efficient parameterless SQL statement which can be used to test connection validity.
static Database getInstance()
          Get the default database instance.
static Database getInstanceIfStarted()
          Get the default database instance if it was started otherwise return null.
 int getSelectListResultSetConcurrency()
          This method returns default result set concurrency which should be used to load lists of items from the result set efficiently.
 int getSelectListResultSetType()
          This method returns default result set type which should be used to load lists of items from the result set efficiently.
 int getTransactionIsolation(int iTransactionIsolation)
          Different databases have different bugs about what they support so this method will take the desired transaction isolation level and convert it to the one supported by database.
 boolean hasAbsolutePositioningSupport()
          Find out if database (driver) allows us to call methods such as absolute() or last() for retrieved result sets.
 boolean hasSelectListRangeSupport()
          Find out if database allows us to support rows limitation.
 boolean isStarted()
          Test if the database is started.
 boolean preferCountToLast()
          Find out if when trying to find out size of the result set we should use rather count(*)/count(1)/count(id) instead of using hasAbsolutePositioningSupport.
static void setInstance(Database dbDatabase)
          Set default database instance.
 void start()
          Start the database.
abstract  void startDatabaseServer()
          Method for starting database instance.
 void stop()
          Stop the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.opensubsystems.core.persist.db.Database
getCurrentTimestampFunctionCall, getDatabaseType, getDatabaseTypeIdentifier, getSQLAnalyzeFunctionCall, getSQLCountFunctionCall, insertAndFetchGeneratedValues, isCallableStatement, updatedAndFetchGeneratedValues
 

Field Detail

MAX_SAFE_LENGTH

public static final int MAX_SAFE_LENGTH
Number specifying maximal safe length for members within the IN () clause Because there is limitation for sql statement length (in sap db is default 64 kb) and in() expression can contain lot of members.

See Also:
Constant Field Values

m_bDatabaseSchemaInitialized

protected boolean m_bDatabaseSchemaInitialized
Flag signaling that the database schema was initialized;


m_bDatabaseStartInProgress

protected boolean m_bDatabaseStartInProgress
Flag signaling that the database start is in progress.


m_bDatabaseStarted

protected boolean m_bDatabaseStarted
Flag if database was started.


m_vdsSchema

protected VersionedDatabaseSchema m_vdsSchema
Database schema for this database. By default it is versioned database schema which manages all other schemas in the database.

Constructor Detail

DatabaseImpl

protected DatabaseImpl()
                throws OSSException
Default constructor. Protected so that default database cannot be constructed explicitely.

Throws:
OSSException - - problem creating connection factory
Method Detail

getInstance

public static Database getInstance()
                            throws OSSException
Get the default database instance.

Returns:
Database
Throws:
OSSException - - problem accessing the database

getInstanceIfStarted

public static Database getInstanceIfStarted()
                                     throws OSSException
Get the default database instance if it was started otherwise return null.

Returns:
Database - null if it wasn't started otherwise instance of the database
Throws:
OSSException - - problem accessing the database

setInstance

public static void setInstance(Database dbDatabase)
Set default database instance. This instance will be returned by getInstance method until reset.

Parameters:
dbDatabase - - new default database instance
See Also:
getInstance()

start

public void start()
           throws OSSException
Start the database.

Specified by:
start in interface Database
Throws:
OSSException - - database cannot be started.

stop

public void stop()
          throws OSSException
Stop the database. After the database is stopped, no more requests can be issued until the database is started again.

Specified by:
stop in interface Database
Throws:
OSSException - - problems stopping the database

isStarted

public boolean isStarted()
Test if the database is started.

Specified by:
isStarted in interface Database
Returns:
boolean - true if started

startDatabaseServer

public abstract void startDatabaseServer()
                                  throws OSSException
Method for starting database instance. For HSQL it is not needed because the database is started (created) when the administrator is connected to. For HSQL will be empty implementation of this method. For SAPDB is neccessary to start DB instance before connecting to.

Throws:
OSSException - - problem starting the server

createDatabaseInstance

public abstract void createDatabaseInstance()
                                     throws OSSException
Method for creating database instance. For HSQL it is not needed because the database is created when the administrator is connected to. For HSQL will be empty implementation of this method. For SAPDB is neccessary to create DB instance before connecting to.

Throws:
OSSException - - problem creating the database instance

add

public void add(DatabaseSchema dsSchema)
         throws OSSException
Add new schema to the database schema.

Specified by:
add in interface Database
Parameters:
dsSchema - - schema to add to the database
Throws:
OSSException - - database cannot be started.

add

public void add(java.lang.Class clsSchema)
         throws OSSException
Add new schema to the database schema by specifying the generic schema class or interface. The schema itself will be created by DatabaseSchemaManager for current environment and based on any configuration settings.

Specified by:
add in interface Database
Parameters:
clsSchema - - class representing schema to add to the database
Throws:
OSSException - - database cannot be started.

getConnectionTestStatement

public java.lang.String getConnectionTestStatement()
Get very efficient parameterless SQL statement which can be used to test connection validity.

Specified by:
getConnectionTestStatement in interface Database
Returns:
String - SQL statement

getTransactionIsolation

public int getTransactionIsolation(int iTransactionIsolation)
Different databases have different bugs about what they support so this method will take the desired transaction isolation level and convert it to the one supported by database.

Specified by:
getTransactionIsolation in interface Database
Parameters:
iTransactionIsolation - - desired transaction isolation level
Returns:
int - supported transaction isolation level

getSelectListResultSetType

public int getSelectListResultSetType()
This method returns default result set type which should be used to load lists of items from the result set efficiently. Some databases supports efficient absolute cursors that allow us to efficiently find out the size of result set and efficiently allocate memory for it and they may require special result set type to do so.

Specified by:
getSelectListResultSetType in interface Database
Returns:
int - result set type to use load list, see ResultSet.TYPE_XXX constants

hasAbsolutePositioningSupport

public boolean hasAbsolutePositioningSupport()
Find out if database (driver) allows us to call methods such as absolute() or last() for retrieved result sets.

Specified by:
hasAbsolutePositioningSupport in interface Database
Returns:
boolean - true if it is possible to call methods such as absolute, last() for retrieved result sets.

preferCountToLast

public boolean preferCountToLast()
Find out if when trying to find out size of the result set we should use rather count(*)/count(1)/count(id) instead of using hasAbsolutePositioningSupport.

Specified by:
preferCountToLast in interface Database
Returns:
boolean - true if should use count(x) instead of last()

hasSelectListRangeSupport

public boolean hasSelectListRangeSupport()
Find out if database allows us to support rows limitation. This means that database has to provide a way how to construct EFFICIENT SQL which allows us to retrieve items starting from row X and ending at row Y.

Specified by:
hasSelectListRangeSupport in interface Database
Returns:
boolean - true if it is possible to support row limitation

getSelectListResultSetConcurrency

public int getSelectListResultSetConcurrency()
This method returns default result set concurrency which should be used to load lists of items from the result set efficiently. Some databases supports efficient absolute cursors that allow us to efficiently find out the size of result set and efficiently allocate memory for it and they may require special result set concurrency to do so.

Specified by:
getSelectListResultSetConcurrency in interface Database
Returns:
int - result set concurrency to use load list, see ResultSet.CONCUR_XXX constants

createUser

protected abstract void createUser(java.sql.Connection cntAdminDBConnection)
                            throws OSSException
Create database user which will be used by connection pool to access the database.

Parameters:
cntAdminDBConnection - - connection with rights to create users
Throws:
OSSException - - cannot create user

getAdminConnection

protected java.sql.Connection getAdminConnection(boolean bAutoCommit)
                                          throws OSSException
Get connection with administration priviledges.

Parameters:
bAutoCommit - - desired autocommit setting for the connection
Returns:
Connection - connection with administrator priviledges, never null
Throws:
OSSException - - problem connecting to database


Copyright © 2003 - 2006 OpenSubsystems s.r.o.