|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.helidb.txn.Transaction
public class Transaction
This object represents a database transaction. By using a database
transaction, a database client can combine several database operations over
several databases into one logical operation that is guaranteed to have ACID
properties. Read more on ACID:ity in this Wikipedia article. At the
end of the logical operation, it is either commit()
:ed, i.e.
persisted to the database storage, or rollback()
:ed, i.e. discarded.
A transaction always involves just one execution thread. Several different threads may have their own transactions, and the transactions' ACID:ity properties guarantee that they will never see data in an intermediate state. This is accomplished by the thread locking implemented in the involved databases.
HeliDB has both read/write and read only transactions. It is up to the
Database
implementation used if they are treated
differently. For instance, a Database
could allow several concurrent
read only transactions, but only one simultaneous transaction if that
transaction is read/write.
A new transaction is started by any of the static
startTransaction(boolean)
or getOrStartTransaction(boolean)
methods. If the thread already is involved in a transaction and wants to get
that transaction's Transaction
object, it can call any of the static
getter methods.
Implementation note:
A database joins a Transaction
via its
TransactionCollaborator
. It uses one collaborator object per
transaction it is involved in. The collaborator keeps track of the state for
the database associated with just that transaction.
TransactionCollaborator
,
TransactionalDatabase
Constructor Summary | |
---|---|
protected |
Transaction(boolean readOnly)
Create a new transaction. |
Method Summary | |
---|---|
protected void |
assertNotFinished()
Throw an IllegalStateException if the transaction has been
finished. |
void |
commit()
Commit the transaction. |
protected void |
finalize()
If the transaction is still active, it is rolled back before this object is discarded. |
TransactionCollaborator<?,?,?> |
getCollaborator(Database<?,?> owner)
Get the collaborator for the supplied object. |
protected Map<Integer,TransactionCollaborator<?,?,?>> |
getCollaborators()
Get all collaborators and their owners for the current transaction. |
static Transaction |
getCurrentTransaction()
Get the current transaction for the calling thread. |
static Transaction |
getCurrentTransactionOrNull()
Get the current transaction for the calling thread. |
static Transaction |
getOrStartTransaction(boolean readOnly)
Get the current transaction, possibly starting a new transaction if there is not one already. |
boolean |
isFinished()
Is this transaction finished, i.e. |
boolean |
isReadOnly()
Is this transaction read only? |
void |
rollback()
Roll back the transaction. |
TransactionCollaborator<?,?,?> |
setCollaborator(Database<?,?> owner,
TransactionCollaborator<?,?,?> cbr)
Register a collaborator. |
static Transaction |
startTransaction(boolean readOnly)
Start a new transaction for the calling thread. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected Transaction(boolean readOnly)
readOnly
- Should the transaction be read only?Method Detail |
---|
public boolean isReadOnly()
true
if this transaction is read only.public boolean isFinished()
true
if this transaction is finished.public static Transaction getCurrentTransaction() throws NoTransactionException
NoTransactionException
- If there is no current transaction for the
calling thread.getCurrentTransactionOrNull()
,
getOrStartTransaction(boolean)
,
startTransaction(boolean)
public static Transaction getCurrentTransactionOrNull()
null
if there is no current
transaction.getCurrentTransaction()
,
startTransaction(boolean)
,
getOrStartTransaction(boolean)
public static Transaction getOrStartTransaction(boolean readOnly) throws IllegalStateException
readOnly
- If a new transaction is started, should it be read only?
null
.
IllegalStateException
- If the client is trying to get a read/write
transaction when the thread is participating in a read only transaction.startTransaction(boolean)
,
getCurrentTransaction()
,
getCurrentTransactionOrNull()
public static Transaction startTransaction(boolean readOnly) throws IllegalStateException
readOnly
- Should the transaction be read only?
IllegalStateException
- If the calling thread already has a
transaction.protected void assertNotFinished() throws IllegalStateException
IllegalStateException
if the transaction has been
finished.
IllegalStateException
- If the transaction has been finished.protected Map<Integer,TransactionCollaborator<?,?,?>> getCollaborators()
System.identityHashCode(Object)
.) The returned map is the actual
map used by this object, so changes to it will be reflected in the
object's internal state.public TransactionCollaborator<?,?,?> getCollaborator(Database<?,?> owner) throws IllegalStateException
owner
- The owner of the collaborator.
null
if the
supplied object does not have a collaborator registered with this
transaction.
IllegalStateException
- If this transaction has already been
committed or rolled back.setCollaborator(Database, TransactionCollaborator)
public TransactionCollaborator<?,?,?> setCollaborator(Database<?,?> owner, TransactionCollaborator<?,?,?> cbr) throws IllegalStateException
owner
- The owner of the collaborator. The owner is identified by
its object instance.cbr
- The collaborator.
null
if it did not
have one.
IllegalStateException
- If this transaction has already been
committed or rolled back.getCollaborator(Database)
public void commit() throws IllegalStateException, UnableToCommitException
getCurrentTransactionOrNull()
by this thread
will return null
until a new transaction is started.
IllegalStateException
- If this transaction has already been
committed or rolled back.
UnableToCommitException
- If the transaction cannot be committed
for some reason. The exception message should contain the reason why.public void rollback() throws IllegalStateException
getCurrentTransactionOrNull()
by this thread
will return null
until a new transaction is started.
IllegalStateException
- If this transaction has already been
committed or rolled back.protected void finalize() throws Throwable
finalize
in class Object
Throwable
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |