Interface Context.Tx
- All Known Implementing Classes:
PlaceboTx
,TransactionAdapter.TxAdapter
- Enclosing interface:
Context
This gives acces to a simple transaction api.
In the following example, the call to commit() (2) will commit the transaction. On finish() (3) the transaction is rolled back, if no commit() has previously been called. The exception handling code doesn't need to worry about doing rollbacks.
public void doSomething() { Context.Tx tx = context.beginTx(); (1) try { //do your stuff here tx.commit(); (2) } catch (PersistException e) { //do your exception handling } finally { tx.finish(); (3) } }Nested transactions
At point (1) a new "toplevel" transaction is created with the same behaviour
as stated above. Then entering method doSomethingB a "placebo" transaction
is returned. The call to commit() (5) has no effect, unless it's called more
than one time or fail()
has been called before. In that case an exception
is thrown. A call to fail()
will always set the entire transaction to
rollback-only, so a call to commit()
afterwards yields in an
exception. The finish()
method will set the entire transaction to
rollback-only if commit()
has not been called. Thus you usually
indicate a successful completion with a call to commit()
. It may really commit
the transaction or not, that depends on whether the current code is participating
in an existing transaction or has created a new one. This detail shouldn't be important
to client code in most cases.
public void doSomethingA() { Context.Tx tx = context.beginTx(); (1) try { //do some stuff doSomethingB(); tx.commit(); (2) } catch (PersistException e) { //exception handling code here } finally { tx.finish(); (3) } } public void doSomethingB() throws PersistException { Context.Tx tx = context.beginTx(); (4) try { //do stuff here again tx.commit(); (5) } finally { tx.finish(); (6) } }
An inner-transaction may call fail()
to set the entire transaction to rollback-only.
-
Method Summary
-
Method Details
-
commit
- Throws:
PersistException
-
fail
void fail() -
finish
void finish()
-