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
This commit is contained in:
rob bygrave committed 2020-11-12 13:55:10 +13:00
1 parent b540d70b2d
commit 1eb5cf1d22
1 file changed
+37 -34
@@ -32,47 +32,50 @@ public class TransactionManagerTest extends BaseTestCase {
DataSource dataSource = transactionManager.getDataSource(); DataSource dataSource = transactionManager.getDataSource();
Connection connection = dataSource.getConnection(); 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 { 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(); 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"); Customer.find.byName("external still active");
log.info("inner commit");
inner.commit();
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 { } finally {
log.info("inner end"); connection.close();
inner.end();
} }
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); Customer.find.byNameStatus("New implicit transaction", Customer.Status.ACTIVE);
} }
} }