Created
April 26, 2012 13:12
-
-
Save anonymous/2499458 to your computer and use it in GitHub Desktop.
Resume/suspend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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