mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor tidy on DefaultTransactionThreadLocal (#1715)
- remove unused methods - use clear() rather than set(null)
This commit is contained in:
@@ -70,7 +70,7 @@ public class ScopedTransaction extends SpiTransactionProxy {
|
||||
|
||||
private void clearScopeOnce() {
|
||||
if (!scopeCleared) {
|
||||
manager.set(null);
|
||||
manager.clear();
|
||||
scopeCleared = true;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-54
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user