From 1eb5cf1d22414abc8b87a18024fa240c56efd3f7 Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Thu, 12 Nov 2020 13:55:10 +1300 Subject: [PATCH] Fix test TransactionManagerTest - close leaked connection Connection properties to help find leaks are capturestacktrace and maxStackTraceSize, e.g. datasource.h2.capturestacktrace=true datasource.h2.maxStackTraceSize=20 --- .../transaction/TransactionManagerTest.java | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/ebean-core/src/test/java/io/ebeaninternal/server/transaction/TransactionManagerTest.java b/ebean-core/src/test/java/io/ebeaninternal/server/transaction/TransactionManagerTest.java index 6b851c56d..da624fdbb 100644 --- a/ebean-core/src/test/java/io/ebeaninternal/server/transaction/TransactionManagerTest.java +++ b/ebean-core/src/test/java/io/ebeaninternal/server/transaction/TransactionManagerTest.java @@ -32,47 +32,50 @@ public class TransactionManagerTest extends BaseTestCase { DataSource dataSource = transactionManager.getDataSource(); Connection connection = dataSource.getConnection(); - - SpiTransaction externalTxn = new ExternalJdbcTransaction("external0", true, connection, null); - - // push an externally managed transaction onto scope - ScopedTransaction scopedTransaction = transactionManager.externalBeginTransaction(externalTxn, TxScope.required()); - - Transaction current = Transaction.current(); - assertThat(current).as("external transaction is in scope").isSameAs(scopedTransaction); - - Customer.find.byName("In external"); - - log.info("inner begin"); - Transaction inner = Ebean.beginTransaction(); try { + SpiTransaction externalTxn = new ExternalJdbcTransaction("external0", true, connection, null); + + // push an externally managed transaction onto scope + ScopedTransaction scopedTransaction = transactionManager.externalBeginTransaction(externalTxn, TxScope.required()); + + Transaction current = Transaction.current(); + assertThat(current).as("external transaction is in scope").isSameAs(scopedTransaction); + + Customer.find.byName("In external"); + + log.info("inner begin"); + Transaction inner = Ebean.beginTransaction(); + try { + + current = Transaction.current(); + assertThat(current).as("still using external transaction, nested transaction pushed").isSameAs(scopedTransaction); + + Customer.find.byName("In inner"); + log.info("inner commit"); + inner.commit(); + } finally { + log.info("inner end"); + inner.end(); + } current = Transaction.current(); - assertThat(current).as("still using external transaction, nested transaction pushed").isSameAs(scopedTransaction); + assertThat(current).as("external transaction still in scope").isSameAs(scopedTransaction); - Customer.find.byName("In inner"); - log.info("inner commit"); - inner.commit(); + Customer.find.byName("external still active"); + + + log.info("external transaction ends with commit and remove"); + connection.commit(); + // remove externally managed transaction out of scope + transactionManager.externalRemoveTransaction(); + + + current = Transaction.current(); + assertThat(current).as("external transaction out of scope").isNull(); } finally { - log.info("inner end"); - inner.end(); + connection.close(); } - current = Transaction.current(); - assertThat(current).as("external transaction still in scope").isSameAs(scopedTransaction); - - Customer.find.byName("external still active"); - - - log.info("external transaction ends with commit and remove"); - connection.commit(); - // remove externally managed transaction out of scope - transactionManager.externalRemoveTransaction(); - - - current = Transaction.current(); - assertThat(current).as("external transaction out of scope").isNull(); - Customer.find.byNameStatus("New implicit transaction", Customer.Status.ACTIVE); } }