Skip to content

Instantly share code, notes, and snippets.

Created April 26, 2012 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2499458 to your computer and use it in GitHub Desktop.
Save anonymous/2499458 to your computer and use it in GitHub Desktop.
Resume/suspend
/**
* Transaction Manager implementation
*/
private TransactionManager tm;
@Override
public V call() throws Exception {
// Start a transaction that locks. We then suspend the transaction to
// allow for the main transaction to complete.
Transaction tx = null;
try {
tm.begin();
this.lock(this.key);
tx = tm.suspend();
return doTx();
}
finally {
if (tx != null) {
tm.resume(tx);
int status = -1;
try {
status = tm.getStatus();
} catch (Exception e) {
}
if (status == Status.STATUS_ACTIVE) {
tm.commit();
} else {
tm.rollback();
}
}
}
}
/**
* Wraps the work implementation around a transaction.
*
* @return
* @throws Exception
*/
protected V doTx() throws Exception {
try {
tm.begin();
return performWork();
} catch (Exception e) {
tm.setRollbackOnly();
throw e;
}
finally {
int status = -1;
try {
status = tm.getStatus();
} catch (Exception e) {
}
if (status == Status.STATUS_ACTIVE) {
tm.commit();
} else {
tm.rollback();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment