public class Session extends Object
JSimpleDB database access.
Instances operate in one of three modes; see SessionMode.
Instances are not thread safe.
SessionMode| Modifier and Type | Class and Description |
|---|---|
static interface |
Session.Action
Callback interface used by
Session.performSessionAction()
and Session.performSessionActionWithCurrentTransaction(). |
static interface |
Session.TransactionalAction
Tagging interface indicating an
Session.Action that requires there to be an open transaction. |
| Constructor and Description |
|---|
Session(Database db)
Constructor for
SessionMode.CORE_API. |
Session(JSimpleDB jdb)
Constructor for
SessionMode.JSIMPLEDB. |
Session(KVDatabase kvdb)
Constructor for
SessionMode.KEY_VALUE. |
| Modifier and Type | Method and Description |
|---|---|
Database |
getDatabase()
Get the associated
Database, if any. |
String |
getDatabaseDescription()
Get a description of the database.
|
JSimpleDB |
getJSimpleDB()
Get the associated
JSimpleDB, if any. |
JTransaction |
getJTransaction()
Get the open
JTransaction currently associated with this instance. |
KVDatabase |
getKVDatabase()
Get the associated
KVDatabase. |
KVTransaction |
getKVTransaction()
Get the open
KVTransaction currently associated with this instance. |
SessionMode |
getMode()
Get this instance's
SessionMode. |
NameIndex |
getNameIndex()
Get the
NameIndex for this instance's SchemaModel. |
SchemaModel |
getSchemaModel()
Get the
SchemaModel configured for this instance. |
int |
getSchemaVersion()
Get the schema version associated with this instance.
|
Transaction |
getTransaction()
Get the open
Transaction currently associated with this instance. |
ValidationMode |
getValidationMode()
Get the
ValidationMode associated with this instance. |
boolean |
isAllowNewSchema()
Get whether the recording of new schema versions should be allowed.
|
boolean |
isReadOnly()
Get whether new transactions should be marked read-only.
|
boolean |
isTransactionOpen()
Determine whether there is a transaction already associated with this instance.
|
boolean |
performSessionAction(Session.Action action)
Perform the given action in the context of this session.
|
boolean |
performSessionActionWithCurrentTransaction(Session.Action action)
Associate the current
JTransaction with this instance, if not already associated, while performing the given action. |
protected void |
reportException(Exception e)
Handle an exception thrown during an invocation of
performSessionAction(). |
void |
setAllowNewSchema(boolean allowNewSchema) |
void |
setDatabaseDescription(String databaseDescription) |
void |
setMode(SessionMode mode)
Change this instance's
SessionMode. |
void |
setReadOnly(boolean readOnly) |
void |
setRollbackOnly()
Mark the current transaction to be rolled back.
|
void |
setSchemaModel(SchemaModel schemaModel) |
void |
setSchemaVersion(int schemaVersion) |
void |
setValidationMode(ValidationMode validationMode) |
protected final Logger log
public Session(KVDatabase kvdb)
SessionMode.KEY_VALUE.kvdb - key/value databaseIllegalArgumentException - if kvdb is nullpublic Session(Database db)
SessionMode.CORE_API.db - core databaseIllegalArgumentException - if db is nullpublic Session(JSimpleDB jdb)
SessionMode.JSIMPLEDB.jdb - databaseIllegalArgumentException - if jdb is nullpublic SessionMode getMode()
SessionMode.SessionMode.public void setMode(SessionMode mode)
SessionMode.mode - new SessionModeIllegalArgumentException - if mode is nullIllegalArgumentException - if mode requires a JSimpleDB (i.e., SessionMode.JSIMPLEDB),
or Database (i.e., SessionMode.CORE_API) instance, but none was provided at constructionpublic KVDatabase getKVDatabase()
KVDatabase.KVDatabasepublic Database getDatabase()
Database, if any.Database or null if this instance is not in SessionMode.JSIMPLEDB
or SessionMode.CORE_APIpublic JSimpleDB getJSimpleDB()
JSimpleDB, if any.JSimpleDB or null if this instance is not in SessionMode.JSIMPLEDBpublic KVTransaction getKVTransaction()
KVTransaction currently associated with this instance.KVTransaction in which to do workIllegalStateException - if performSessionAction() is not currently being invokedpublic Transaction getTransaction()
Transaction currently associated with this instance.Transaction in which to do workIllegalStateException - if performSessionAction() is not currently being invokedIllegalStateException - if this instance is not in mode SessionMode.CORE_API or SessionMode.JSIMPLEDBpublic JTransaction getJTransaction()
JTransaction currently associated with this instance.
This method just invokes JTransaction.getCurrent() and returns the result.
JTransaction in which to do workIllegalStateException - if performSessionAction() is not currently being invokedIllegalStateException - if this instance is not in mode SessionMode.JSIMPLEDBpublic SchemaModel getSchemaModel()
SchemaModel configured for this instance.
If this is left unconfigured, after the first transaction it will be updated with the schema model actually used.
In SessionMode.KEY_VALUE, this always returns null.
public void setSchemaModel(SchemaModel schemaModel)
public NameIndex getNameIndex()
NameIndex for this instance's SchemaModel.public String getDatabaseDescription()
public void setDatabaseDescription(String databaseDescription)
public int getSchemaVersion()
In SessionMode.KEY_VALUE, this always returns zero.
public void setSchemaVersion(int schemaVersion)
public ValidationMode getValidationMode()
ValidationMode associated with this instance.
If this is left unconfigured, ValidationMode.AUTOMATIC is used for new transactions.
This property is only relevant in SessionMode.JSIMPLEDB.
public void setValidationMode(ValidationMode validationMode)
public boolean isAllowNewSchema()
In SessionMode.KEY_VALUE, this setting is ignored.
public void setAllowNewSchema(boolean allowNewSchema)
public boolean isReadOnly()
public void setReadOnly(boolean readOnly)
protected void reportException(Exception e)
performSessionAction().
The implementation in Session logs an error message. Subclasses are encouraged to
handle errors more gracefully within the context of the associated application.
e - exception thrown during performSessionAction()public boolean performSessionActionWithCurrentTransaction(Session.Action action)
JTransaction with this instance, if not already associated, while performing the given action.
If action throws an Exception, it will be caught and handled by reportException()
and then false returned.
This instance must be in SessionMode.JSIMPLEDB, there must be a JTransaction open and
associated with the current thread, and this instance must not already
have a different JTransaction associated with it (it may already have the same JTransaction
associated with it). The JTransaction will be left open when this method returns.
This method safely handles re-entrant invocation.
action - action to performaction completed successfully, false if action threw an exceptionIllegalStateException - if there is a different open transaction already associated with this instanceIllegalStateException - if this instance is not in mode SessionMode.JSIMPLEDBIllegalArgumentException - if action is nullpublic boolean performSessionAction(Session.Action action)
If action is a Session.TransactionalAction, and there is no transaction currently associated with this instance,
a new transaction will be created, held open while action executes, then committed. Otherwise, action
is just executed directly.
In either case, if action throws an Exception, it will be caught and handled by
reportException() and then false returned.
In either case, if there is already a transaction currently associated with this instance, it is left open
while action executes and upon return.
action - action to perform, possibly within a transactionaction completed successfully, false if a transaction could not be created
or action threw an exceptionIllegalArgumentException - if action is nullpublic boolean isTransactionOpen()
public void setRollbackOnly()
IllegalStateException - if there is no transaction associated with this instanceCopyright © 2016. All rights reserved.