Package db
Class Transaction
java.lang.Object
db.Transaction
- All Implemented Interfaces:
AutoCloseable
Provides syntax for opening a database transaction using a try-with-resources block
For example, using DBHandle.startTransaction()
directly:
int txid = dbHandle.startTransaction(); try { // ... Do something } finally { program.endTransaction(txid, true); }
Can be expressed using an an Transaction
instead:
try (Transaction tx = dbHandle.openTransaction(dbErrorHandler)) { // ... Do something }
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Mark transaction for rollback/non-commit and end transaction if active.void
Mark transaction for rollback/non-commit upon closing.void
close()
End this transaction if active using the current commit state.void
commit()
Mark transaction for commit and end transaction if active.void
Mark transaction for commit upon closing.protected abstract boolean
endTransaction
(boolean commit) End this transaction if currently active.boolean
Determine if this is a sub-transaction to a larger transaction.
-
Constructor Details
-
Transaction
protected Transaction()
-
-
Method Details
-
endTransaction
protected abstract boolean endTransaction(boolean commit) End this transaction if currently active.- Parameters:
commit
- true if changes should be commited, false if all changes in this transaction should be discarded (i.e., rollback). If this is a "sub-transaction" and commit is false, the larger transaction will rollback upon completion.- Returns:
- true if changes have been commited or false if nothing to commit or commit parameter was specified as false.
-
isSubTransaction
public boolean isSubTransaction()Determine if this is a sub-transaction to a larger transaction. If true is returned the larger transaction will not complete until all sub-transactions have ended. The larger transaction will rollback upon completion if any of the sub-transactions do not commit.- Returns:
- true if this is a sub-transaction, else false.
-
abortOnClose
public void abortOnClose()Mark transaction for rollback/non-commit upon closing. A subsequent invocation ofcommitOnClose()
will alter this state prior to closing. -
commitOnClose
public void commitOnClose()Mark transaction for commit upon closing. This state is assumed by default. A subsequent invocation ofabortOnClose()
will alter this state prior to closing. -
abort
public void abort()Mark transaction for rollback/non-commit and end transaction if active. -
commit
public void commit()Mark transaction for commit and end transaction if active. -
close
public void close()End this transaction if active using the current commit state. SeecommitOnClose()
,abortOnClose()
.- Specified by:
close
in interfaceAutoCloseable
-