org.sormula.active
Class ActiveDatabase

java.lang.Object
  extended by org.sormula.active.ActiveDatabase
All Implemented Interfaces:
java.io.Serializable

public class ActiveDatabase
extends java.lang.Object
implements java.io.Serializable

A lightweight class for defining the DataSource to be used by ActiveRecord and related classes. This class may be serialized but the active transaction will not be serialized.

Since:
1.7 and 2.1
Author:
Jeff Miller
See Also:
Serialized Form

Constructor Summary
ActiveDatabase(javax.sql.DataSource dataSource)
          Constructs for a data source and empty schema.
ActiveDatabase(javax.sql.DataSource dataSource, java.lang.String schema)
          Constructs for a data source and schema.
ActiveDatabase(java.lang.String dataSourceName)
          Constructs for a data source name and empty schema.
ActiveDatabase(java.lang.String dataSourceName, java.lang.String schema)
          Constructs for a data source name and schema.
 
Method Summary
 void addNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
          Adds a default name translator to use when none are defined for the table.
 ActiveTransaction getActiveTransaction()
          Gets the transaction in use by this active database.
 javax.sql.DataSource getDataSource()
          Gets the data source.
static ActiveDatabase getDefault()
          Gets the active database to use when none is specified.
 java.lang.Class<? extends NameTranslator> getNameTranslatorClass()
          Deprecated. 
 java.util.List<java.lang.Class<? extends NameTranslator>> getNameTranslatorClasses()
          Gets the default name translators to be used when none are defined for the table.
 java.lang.String getSchema()
          Gets the schema.
 boolean isReadOnly()
          Gets read-only indicator.
 boolean isTimings()
          Gets status of timings.
 void removeNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
          Removes a default name translator.
 void setActiveTransaction(ActiveTransaction activeTransaction)
          Sets the transaction in use.
static void setDefault(ActiveDatabase activeDatabase)
          Sets the default active database.
 void setNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
          Deprecated. 
 void setReadOnly(boolean readOnly)
          Sets read-only indicator.
 void setTimings(boolean timings)
          Sets timings enabled for all database operations.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActiveDatabase

public ActiveDatabase(java.lang.String dataSourceName)
               throws ActiveException
Constructs for a data source name and empty schema.

DataSource will be obtained from JNDI look up of dataSourceName. Typically web containers require data source to be configured with a name like "someds" but the full path to the data source contains an implied context of "java:comp/env". So the constructor parameter, dataSourceName, would be "java:comp/env/someds".

Parameters:
dataSourceName - name to use in JNDI look up of data source
Throws:
ActiveException - if error looking up data source
Since:
1.9 and 2.3

ActiveDatabase

public ActiveDatabase(java.lang.String dataSourceName,
                      java.lang.String schema)
               throws ActiveException
Constructs for a data source name and schema.

DataSource will be obtained from JNDI look up of dataSourceName. Typically web containers require data source to be configured with a name like "someds" but the full path to the data source contains an implied context of "java:comp/env". So the constructor parameter, dataSourceName, would be "java:comp/env/someds".

Parameters:
dataSourceName - name to use in JNDI look up of data source
schema - schema prefix for table names in sql
Throws:
ActiveException - if error looking up data source
Since:
1.9 and 2.3

ActiveDatabase

public ActiveDatabase(javax.sql.DataSource dataSource)
Constructs for a data source and empty schema.

Parameters:
dataSource - data source for active records

ActiveDatabase

public ActiveDatabase(javax.sql.DataSource dataSource,
                      java.lang.String schema)
Constructs for a data source and schema.

Parameters:
dataSource - data source for active records
schema - schema prefix for table names in sql
Method Detail

getDefault

public static ActiveDatabase getDefault()
Gets the active database to use when none is specified. Only one default active database may be used per class loader. ActiveTable.ActiveTable(Class) and an ActiveRecord that is not attached to a database will use the default active database.

Returns:
default active database or null if setDefault(ActiveDatabase) has not been used to set the default

setDefault

public static void setDefault(ActiveDatabase activeDatabase)
Sets the default active database. See getDefault() for explanation.

Parameters:
activeDatabase - default active databases or null to remove current default

getDataSource

public javax.sql.DataSource getDataSource()
Gets the data source.

Returns:
data source supplied in constructor

getSchema

public java.lang.String getSchema()
Gets the schema.

Returns:
schema supplied in constructor or empty string if none

getActiveTransaction

public ActiveTransaction getActiveTransaction()
Gets the transaction in use by this active database.

Returns:
transaction or null if no transaction is in use
Since:
1.7.1 and 2.1.1

setActiveTransaction

public void setActiveTransaction(ActiveTransaction activeTransaction)
                          throws ActiveException
Sets the transaction in use. Set by ActiveTransaction.begin() when a transaction starts. Set to null by ActiveTransaction.close() and ActiveTransaction.rollback() when the transaction is over.

Only one active transaction may be in use at one time by the same instance of ActiveDatabase. Create an instance of ActiveDatabse for each possible active transaction that may be in use at the same time.

Parameters:
activeTransaction - active transaction to use
Throws:
ActiveException - if a transaction is already in use
Since:
1.7.1 and 2.1.1

isReadOnly

public boolean isReadOnly()
Gets read-only indicator.

Returns:
true if modify operations are not permitted
See Also:
SqlOperation.isReadOnly()

setReadOnly

public void setReadOnly(boolean readOnly)
Sets read-only indicator. When true, modify operations, ModifyOperation will fail with an exception. By default read-only is false. Set to true as a safe-guard to prevent accidental modification of database.

Parameters:
readOnly - true to prevent modify operations
See Also:
SqlOperation.setReadOnly(boolean)

getNameTranslatorClass

@Deprecated
public java.lang.Class<? extends NameTranslator> getNameTranslatorClass()
Deprecated. 

Gets the default name translator class for tables when none is specified. Use getNameTranslatorClasses() instead of this method.

Returns:
default name translator class; null if none

setNameTranslatorClass

@Deprecated
public void setNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Deprecated. 

Sets the name translator class to use for a table if no translator is specified by Column.translator(). A new instance is created for each table. Use addNameTranslatorClass(Class) instead of this method.

Parameters:
nameTranslatorClass - default name translator class

getNameTranslatorClasses

public java.util.List<java.lang.Class<? extends NameTranslator>> getNameTranslatorClasses()
Gets the default name translators to be used when none are defined for the table.

Returns:
list of name translator classes; empty list if none
Since:
1.8 and 2.2
See Also:
Database.getNameTranslatorClasses(), Table.translateName(String)

addNameTranslatorClass

public void addNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Adds a default name translator to use when none are defined for the table. Name translators are applied in order they are added.

Parameters:
nameTranslatorClass - class that will be used to translate table/column names
Since:
1.8 and 2.2
See Also:
Database.addNameTranslatorClass(Class), Table.translateName(String)

removeNameTranslatorClass

public void removeNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Removes a default name translator.

Parameters:
nameTranslatorClass - class to remove
Since:
1.8 and 2.2
See Also:
Database.removeNameTranslatorClass(Class), Table.translateName(String)

isTimings

public boolean isTimings()
Gets status of timings.

Returns:
true if operations are to record execution times

setTimings

public void setTimings(boolean timings)
Sets timings enabled for all database operations. When enabled all operations will write execution times to log upon commit.

Parameters:
timings - true to enable all operations to record execution timings; false for no operation timings by default