Adjustment on #1711, more explicit clearing of inactive transactions from thread local

This commit is contained in:
rob bygrave
2019-05-18 11:21:11 +12:00
parent 2678f85506
commit c839fe6520
10 changed files with 48 additions and 13 deletions
@@ -133,6 +133,11 @@ public interface SpiEbeanServer extends ExtendedServer, EbeanServer, BeanLoader,
*/
void externalModification(TransactionEventTable event);
/**
* Clear an implicit transaction from the scope.
*/
void clearServerTransaction();
/**
* Begin a managed transaction (Uses scope manager / ThreadLocal).
*/
@@ -78,6 +78,15 @@ public abstract class BeanRequest {
}
}
/**
* Clear the transaction from the thread local for implicit transactions.
*/
public void clearTransIfRequired() {
if (createdTransaction) {
ebeanServer.clearServerTransaction();
}
}
/**
* Return the server processing the request. Made available for
* BeanController and BeanFinder.
@@ -2254,6 +2254,11 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
return new ObtainedTransactionImplicit(trans, this);
}
@Override
public void clearServerTransaction() {
transactionManager.clearServerTransaction();
}
@Override
public SpiTransaction beginServerTransaction() {
return transactionManager.beginServerTransaction();
@@ -123,6 +123,8 @@ public final class DefaultPersister implements Persister {
} catch (RuntimeException e) {
request.rollbackTransIfRequired();
throw e;
} finally {
request.clearTransIfRequired();
}
}
@@ -435,6 +437,8 @@ public final class DefaultPersister implements Persister {
} catch (RuntimeException ex) {
req.rollbackTransIfRequired();
throw ex;
} finally {
req.clearTransIfRequired();
}
}
@@ -471,6 +475,8 @@ public final class DefaultPersister implements Persister {
} catch (RuntimeException ex) {
req.rollbackTransIfRequired();
throw ex;
} finally {
req.clearTransIfRequired();
}
}
@@ -623,6 +629,8 @@ public final class DefaultPersister implements Persister {
} catch (RuntimeException ex) {
req.rollbackTransIfRequired();
throw ex;
} finally {
req.clearTransIfRequired();
}
}
@@ -43,8 +43,8 @@ public class DefaultTransactionScopeManager extends TransactionScopeManager {
}
@Override
public void clear(SpiTransaction trans) {
DefaultTransactionThreadLocal.clear(serverName, trans);
public void clear() {
DefaultTransactionThreadLocal.clear(serverName);
}
}
@@ -41,13 +41,12 @@ public final class DefaultTransactionThreadLocal {
}
/**
* Clears a transaction from the ThreadLocal to prevent memory leaks.
* Will only clear, if trans == currentTransaction
* Clear a transaction. It should be inactive.
*/
public static void clear(String serverName, SpiTransaction trans) {
Map<String, SpiTransaction> map = local.get();
if (map.get(serverName) == trans) {
map.remove(serverName);
public static void clear(String serverName) {
SpiTransaction transaction = local.get().remove(serverName);
if (transaction != null && transaction.isActive()) {
throw new IllegalStateException("Clearing an ACTIVE transaction " + transaction);
}
}
@@ -920,9 +920,6 @@ public class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
}
connection = null;
active = false;
if (manager != null) {
manager.scope().clear(this);
}
profileEnd();
}
@@ -567,6 +567,13 @@ public class TransactionManager implements SpiTransactionManager {
}
}
/**
* Clear an implicit transaction from thread local scope.
*/
public void clearServerTransaction() {
scopeManager.clear();
}
/**
* Begin an implicit transaction.
*/
@@ -35,9 +35,9 @@ public abstract class TransactionScopeManager implements SpiTransactionScopeMana
public abstract void set(SpiTransaction trans);
/**
* Clears the given Transaction for this serverName and Thread.
* Clears the current Transaction from thread local scope (for implicit transactions).
*/
public abstract void clear(SpiTransaction trans);
public abstract void clear();
/**
* Replace the current transaction with this one.