public class ActiveDatabase
extends java.lang.Object
implements java.io.Serializable
DataSource
to be used by ActiveRecord
and related classes. This class may be serialized but the active transaction will not
be serialized.Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
void |
addNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
Adds a default name translator to use when none are defined for the table.
|
void |
flush()
Writes all uncommitted cache to the database and removes all cached records.
|
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.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 |
isAutoGeneratedKeys()
Gets the most recent value of
setAutoGeneratedKeys(boolean) . |
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.
|
void |
setAutoGeneratedKeys(boolean autoGeneratedKeys)
Sets the default way identity columns are processed for newly created
Table objects. |
static void |
setDefault(ActiveDatabase activeDatabase)
Sets the default active database.
|
void |
setReadOnly(boolean readOnly)
Sets read-only indicator.
|
void |
setTimings(boolean timings)
Sets timings enabled for all database operations.
|
public ActiveDatabase(java.lang.String dataSourceName) throws ActiveException
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".
dataSourceName
- name to use in JNDI look up of data sourceActiveException
- if error looking up data sourcepublic ActiveDatabase(java.lang.String dataSourceName, java.lang.String schema) throws ActiveException
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".
dataSourceName
- name to use in JNDI look up of data sourceschema
- schema prefix for table names in sqlActiveException
- if error looking up data sourcepublic ActiveDatabase(javax.sql.DataSource dataSource)
dataSource
- data source for active recordspublic ActiveDatabase(javax.sql.DataSource dataSource, java.lang.String schema)
dataSource
- data source for active recordsschema
- schema prefix for table names in sqlpublic static ActiveDatabase getDefault()
ActiveTable.ActiveTable(Class)
and an ActiveRecord
that is not attached to a database will use the default active database.setDefault(ActiveDatabase)
has not been
used to set the defaultpublic static void setDefault(ActiveDatabase activeDatabase)
getDefault()
for explanation.activeDatabase
- default active databases or null to remove current defaultpublic javax.sql.DataSource getDataSource()
public java.lang.String getSchema()
public ActiveTransaction getActiveTransaction()
public void setActiveTransaction(ActiveTransaction activeTransaction) throws ActiveException
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.
activeTransaction
- active transaction to useActiveException
- if a transaction is already in usepublic boolean isAutoGeneratedKeys()
setAutoGeneratedKeys(boolean)
. The default is true.public void setAutoGeneratedKeys(boolean autoGeneratedKeys)
Table
objects. Table
objects created will have Table.setAutoGeneratedKeys(boolean)
set to
Database.isAutoGeneratedKeys()
. Setting to false is useful when a column
is defined as an identity column but the key will be manually created by the application.
Changing this value will have no affect on Table
objects that have already been
created.
autoGeneratedKeys
- true if Table
objects created for this database should
assume that database will automatically generate key values for
fields defined as identity (Column.identity()
is true)Column.identity()
,
Table.isAutoGeneratedKeys()
,
Statement.getGeneratedKeys()
public boolean isReadOnly()
SqlOperation.isReadOnly()
public void setReadOnly(boolean readOnly)
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.readOnly
- true to prevent modify operationsSqlOperation.setReadOnly(boolean)
public java.util.List<java.lang.Class<? extends NameTranslator>> getNameTranslatorClasses()
Database.getNameTranslatorClasses()
,
Table.translateName(String)
public void addNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
nameTranslatorClass
- class that will be used to translate table/column namesDatabase.addNameTranslatorClass(Class)
,
Table.translateName(String)
public void removeNameTranslatorClass(java.lang.Class<? extends NameTranslator> nameTranslatorClass)
nameTranslatorClass
- class to removeDatabase.removeNameTranslatorClass(Class)
,
Table.translateName(String)
public boolean isTimings()
public void setTimings(boolean timings)
timings
- true to enable all operations to record execution timings; false for no
operation timings by defaultpublic void flush()
Database.flush()
.