public interface Transaction
UserTransaction
- the JTA API interfaceTransaction
interface defines the contract for handling transactions for the ExecutionContext.
In the X/Open/JTA layer the handling of XA resources is done. It means, XAResources are
obtained and enlisted to a TransactionManager. The TransactionManager will commit or rollback the resources
at the end of the transactions. There are two kinds of TransactionManager: DataNucleus and JTA. A
JTA TransactionManager is external to DataNucleus, while the DataNucleus TransactionManager is implemented
by DataNucleus as org.datanucleus.transaction
. The DataNucleus TransactionManager is used when the DataSource used
to obtain connections to the underlying database is not enlisted in an external JTA TransactionManager.
The JTA TransactionManager is usually found when running in JavaEE application servers, however
nowadays there are many JTA containers that can be used in JavaSE.
The scenarios where a JTA TransactionManager is used is: When an JTA TransactionManager exists, and the connections to the underlying databases are acquired via transactional DataSources. That means, when you ask a connection to the DataSource, it will automatically enlist it in a JTA TransactionManager.
The Resource layer is handled by the datastore. For example, with RDBMS databases, the javax.sql.Connection is the API used to demarcate the database transactions. In an RBDMS database, the resource layer is handling the database transaction. For a treatment of isolation levels, refer to http://www.cs.umb.edu/~poneil/iso.pdf
Modifier and Type | Field and Description |
---|---|
static String |
TRANSACTION_ISOLATION_OPTION
Option to use when wanting to set the transaction isolation level.
|
Modifier and Type | Method and Description |
---|---|
void |
addTransactionEventListener(TransactionEventListener listener)
Adds a transaction listener.
|
void |
begin()
Begin a transaction.
|
void |
bindTransactionEventListener(TransactionEventListener listener)
Listeners that are never cleared, and invoked for all transactions
|
void |
close()
Method to inform the transaction that it is closed.
|
void |
commit()
Commit the current transaction.
|
void |
end()
Method to notify that the transaction is ended.
|
void |
flush()
Method to allow the transaction to flush any resources.
|
long |
getBeginTime()
Accessor for the time (millisecs) from System.currentTimeMillis when the transaction started.
|
boolean |
getIsActive()
Return whether the transaction is active and return immediately.
|
boolean |
getNontransactionalRead()
If
true , allows persistent instances to be read without a transaction active. |
boolean |
getNontransactionalWrite()
If
true , allows persistent instances to be written without a transaction active. |
boolean |
getNontransactionalWriteAutoCommit()
Whether to auto-commit any non-tx writes.
|
boolean |
getOptimistic()
Optimistic transactions do not hold data store locks until commit time.
|
Map<String,Object> |
getOptions()
Obtain all settings for this Transaction
|
boolean |
getRestoreValues()
Return the current value of the restoreValues property.
|
boolean |
getRetainValues()
If
true , at commit time instances retain their field values. |
boolean |
getRollbackOnly()
Returns the rollback-only status of the transaction.
|
Boolean |
getSerializeRead()
Accessor for the setting for whether to serialize read objects (lock them).
|
Synchronization |
getSynchronization()
The user-specified
Synchronization instance for this Transaction instance. |
boolean |
isActive()
Returns whether there is a transaction currently active.
|
boolean |
isCommitting()
Checks whether a transaction is committing.
|
void |
preFlush()
Method to notify that flush is started.
|
void |
releaseSavepoint(String name)
Method to deregister the current position as a savepoint with the provided name (assuming the datastore supports it).
|
void |
removeTransactionEventListener(TransactionEventListener listener)
Removes the specified listener.
|
void |
rollback()
Rollback the current transaction.
|
void |
rollbackToSavepoint(String name)
Method to rollback the transaction to the specified savepoint (assuming the datastore supports it).
|
void |
setNontransactionalRead(boolean nontransactionalRead)
If
true , allow persistent instances to be read without a transaction active. |
void |
setNontransactionalWrite(boolean nontransactionalWrite)
If
true , allow persistent instances to be written without a transaction active. |
void |
setNontransactionalWriteAutoCommit(boolean autoCommit)
Set whether to auto-commit any non-tx writes.
|
void |
setOptimistic(boolean optimistic)
Optimistic transactions do not hold data store locks until commit time.
|
void |
setOption(String option,
boolean value) |
void |
setOption(String option,
int value) |
void |
setOption(String option,
Object value) |
void |
setOption(String option,
String value) |
void |
setRestoreValues(boolean restoreValues)
If
true , at rollback, fields of newly persistent instances are restored to their values as of the beginning of
the transaction, and the instances revert to transient. |
void |
setRetainValues(boolean retainValues)
If
true , at commit instances retain their values and the instances transition to persistent-nontransactional. |
void |
setRollbackOnly()
Sets the rollback-only status of the transaction to
true . |
void |
setSavepoint(String name)
Method to register the current position as a savepoint with the provided name (assuming the datastore supports it).
|
void |
setSerializeRead(Boolean serializeRead)
Mutator for whether to serialize (lock) any read objects in this transaction.
|
void |
setSynchronization(Synchronization sync)
The user can specify a
Synchronization instance to be notified on transaction completions. |
static final String TRANSACTION_ISOLATION_OPTION
void close()
void begin()
NucleusUserException
- if transactions are managed by a container in the managed environment, or if the transaction is already active.void commit()
NucleusUserException
- if transactions are managed by a container in the managed environment, or if the transaction is not active.void rollback()
NucleusUserException
- if transactions are managed by a container in the managed environment, or if the transaction is not active.boolean isActive()
getIsActiveif you just want the active flag.
boolean getIsActive()
long getBeginTime()
void preFlush()
void flush()
void end()
boolean getRollbackOnly()
void setRollbackOnly()
true
.
After this flag is set to true
, the transaction can no longer be committed.NucleusUserException
- if the flag is true and an attempt is made to commit the txnvoid setNontransactionalRead(boolean nontransactionalRead)
true
, allow persistent instances to be read without a transaction active.
If an implementation does not support this option, a NucleusUserException is thrown.nontransactionalRead
- Whether to have non-tx readsNucleusUserException
- if not supportedboolean getNontransactionalRead()
true
, allows persistent instances to be read without a transaction active.void setNontransactionalWrite(boolean nontransactionalWrite)
true
, allow persistent instances to be written without a transaction active.nontransactionalWrite
- Whether requiring non-tx writesNucleusUserException
- if not supportedboolean getNontransactionalWrite()
true
, allows persistent instances to be written without a transaction active.void setNontransactionalWriteAutoCommit(boolean autoCommit)
autoCommit
- Whether to auto-commit any non-tx writesNucleusUserException
- if not supportedboolean getNontransactionalWriteAutoCommit()
void setRetainValues(boolean retainValues)
true
, at commit instances retain their values and the instances transition to persistent-nontransactional.retainValues
- the value of the retainValues propertyNucleusUserException
- if not supportedboolean getRetainValues()
true
, at commit time instances retain their field values.void setRestoreValues(boolean restoreValues)
true
, at rollback, fields of newly persistent instances are restored to their values as of the beginning of
the transaction, and the instances revert to transient. Additionally, fields of modified instances of primitive types and
immutable reference types are restored to their values as of the beginning of the transaction.
If false
, at rollback, the values of fields of newly persistent instances are unchanged and the instances revert
to transient. Additionally, dirty instances transition to hollow.
restoreValues
- the value of the restoreValues propertyNucleusUserException
- if not supportedboolean getRestoreValues()
void setOptimistic(boolean optimistic)
optimistic
- the value of the Optimistic flag.NucleusUserException
- if not supportedboolean getOptimistic()
void setSerializeRead(Boolean serializeRead)
serializeRead
- Whether to serialise (lock) any read objectsBoolean getSerializeRead()
void setSynchronization(Synchronization sync)
Synchronization
instance to be notified on transaction completions.
The beforeCompletion
method is called prior to flushing instances to the data store.
The afterCompletion
method is called after performing state transitions of persistent and transactional instances,
following the data store commit or rollback operation.
Only one Synchronization
instance can be registered with the Transaction
. If the application requires
more than one instance to receive synchronization callbacks, then the single application instance is responsible for managing
them, and forwarding callbacks to them.
sync
- the Synchronization
instance to be notified; null
for noneSynchronization getSynchronization()
Synchronization
instance for this Transaction
instance.Synchronization
instance.boolean isCommitting()
void setSavepoint(String name)
name
- Savepoint namevoid releaseSavepoint(String name)
name
- Savepoint namevoid rollbackToSavepoint(String name)
name
- Savepoint namevoid addTransactionEventListener(TransactionEventListener listener)
listener
- The listener to addvoid removeTransactionEventListener(TransactionEventListener listener)
listener
- Listener to removevoid bindTransactionEventListener(TransactionEventListener listener)
listener
- listener to bindMap<String,Object> getOptions()
void setOption(String option, int value)
void setOption(String option, boolean value)
Copyright © 2019. All rights reserved.