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.HasTransactionOptions
Interface implemented by
Session.TransactionalAction's that want to provide custom transaction options. |
static interface |
Session.RetryableAction
Tagging interface indicating a
Session.TransactionalAction that should be retried if a
RetryTransactionException is thrown. |
static interface |
Session.TransactionalAction
Tagging interface indicating an
Session.Action that requires there to be an open transaction. |
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_INITIAL_RETRY_DELAY
Default value for the initial retry delay (in milliseconds).
|
static int |
DEFAULT_MAX_RETRIES
Default value for the maximum number of retry attempts.
|
static int |
DEFAULT_MAXIMUM_RETRY_DELAY
Default value for the maximum retry delay (in milliseconds).
|
protected Logger |
log |
| 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.
|
int |
getInitialRetryDelay()
Get the initial retry delay when a
Session.RetryableAction is given to
performSessionAction(). |
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. |
int |
getMaximumRetryDelay()
Configure the maximum retry delay when a
Session.RetryableAction is given to
performSessionAction(). |
int |
getMaxRetries()
Get the maximum number of allowed retries when a
Session.RetryableAction is given to
performSessionAction(). |
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 |
setInitialRetryDelay(int initialRetryDelay) |
void |
setMaximumRetryDelay(int maximumRetryDelay) |
void |
setMaxRetries(int maxRetries) |
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) |
public static final int DEFAULT_MAX_RETRIES
public static final int DEFAULT_INITIAL_RETRY_DELAY
public static final int DEFAULT_MAXIMUM_RETRY_DELAY
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)
public int getMaxRetries()
Session.RetryableAction is given to
performSessionAction().
Default value is DEFAULT_MAX_RETRIES.
Session.performSessionAction()public void setMaxRetries(int maxRetries)
public int getInitialRetryDelay()
Session.RetryableAction is given to
performSessionAction().
Default value is DEFAULT_INITIAL_RETRY_DELAY.
Session.performSessionAction()public void setInitialRetryDelay(int initialRetryDelay)
public int getMaximumRetryDelay()
Session.RetryableAction is given to
performSessionAction().
Default value is DEFAULT_MAXIMUM_RETRY_DELAY.
Session.performSessionAction()public void setMaximumRetryDelay(int maximumRetryDelay)
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; any transaction options
from action implementing Session.HasTransactionOptions will be appied. 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.
If there is already a transaction currently associated with this instance, it is left open while action
executes and upon return.
If action is a Session.RetryableAction, and a newly created transaction throws a RetryTransactionException,
it will be retried automatically up to the configured maximum number of retry attempts.
An exponential back-off algorithm is used: after the first failed attempt, the current thread sleeps for the
initial retry delay. After each subsequent failed attempt, the retry delay is doubled,
up to the limit imposed by the configured maximum retry delay.
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 © 2017. All rights reserved.