Refactor tidy on DefaultTransactionThreadLocal (#1715)

- remove unused methods
- use clear() rather than set(null)
This commit is contained in:
Rob Bygrave
2019-05-18 12:32:17 +12:00
committed by GitHub
parent 9326c9c79e
commit 201958f38c
2 changed files with 6 additions and 55 deletions
@@ -70,7 +70,7 @@ public class ScopedTransaction extends SpiTransactionProxy {
private void clearScopeOnce() {
if (!scopeCleared) {
manager.set(null);
manager.clear();
scopeCleared = true;
}
}
@@ -31,12 +31,11 @@ public final class DefaultTransactionThreadLocal {
*/
public static void set(String serverName, SpiTransaction trans) {
if (trans == null) {
remove(serverName);
} else {
SpiTransaction existingTransaction = local.get().put(serverName, trans);
if (existingTransaction != null && existingTransaction.isActive()) {
throw new PersistenceException("The existing transaction is still active?");
}
throw new IllegalStateException("Setting a null transaction?");
}
SpiTransaction existingTransaction = local.get().put(serverName, trans);
if (existingTransaction != null && existingTransaction.isActive()) {
throw new PersistenceException("The existing transaction is still active?");
}
}
@@ -82,52 +81,4 @@ public final class DefaultTransactionThreadLocal {
return local.get();
}
private static SpiTransaction obtain(String serverName) {
SpiTransaction transaction = local.get().remove(serverName);
if (transaction == null) {
throw new IllegalStateException("No current transaction for [" + serverName + "]");
}
return transaction;
}
/**
* Commit the current transaction.
*/
public static void commit(String serverName) {
obtain(serverName).commit();
}
/**
* Rollback the current transaction.
*/
public static void rollback(String serverName) {
obtain(serverName).rollback();
}
/**
* If the transaction has not been committed then roll it back.
* <p>
* Designed to be put in a finally block instead of a rollback() in each catch
* block.
* <p>
* <pre>
* Ebean.beginTransaction();
* try {
* // ... perform some actions in a single transaction
*
* Ebean.commitTransaction();
*
* } finally {
* // ensure transaction ended. If some error occurred then rollback()
* Ebean.endTransaction();
* }
* </pre>
*/
public static void end(String serverName) {
SpiTransaction transaction = local.get().remove(serverName);
if (transaction != null) {
transaction.end();
}
}
}