HHH-14557 Test JDBC resources are released on each rollback

This commit is contained in:
Yoann Rodière 2021-04-13 14:32:14 +02:00
parent e03beca97f
commit 571af7bc9e
1 changed files with 44 additions and 0 deletions

View File

@ -140,6 +140,50 @@ public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTest
}
}
@Test
@TestForIssue(jiraKey = {"HHH-14557"})
public void testResourcesReleasedThenConnectionClosedOnEachRollback() throws SQLException {
try (SessionImplementor s = (SessionImplementor) openSession()) {
Connection[] connectionSpies = new Connection[1];
Statement statementMock = Mockito.mock( Statement.class );
RuntimeException rollbackException = new RuntimeException("Rollback");
try {
TransactionUtil2.inTransaction( s, session -> {
Thing thing = new Thing();
thing.setId( 1 );
session.persist( thing );
LogicalConnectionImplementor logicalConnection = session.getJdbcCoordinator().getLogicalConnection();
logicalConnection.getResourceRegistry().register( statementMock, true );
connectionSpies[0] = logicalConnection.getPhysicalConnection();
throw rollbackException;
} );
}
catch (RuntimeException e) {
if ( e != rollbackException ) {
throw e;
}
// Else: ignore, that was expected.
}
// Note: all this must happen BEFORE the session is closed;
// it's particularly important when reusing the session.
Connection connectionSpy = connectionSpies[0];
// Must close the resources, then the connection
InOrder inOrder = inOrder( statementMock, connectionSpy );
inOrder.verify( statementMock ).close();
inOrder.verify( connectionSpy ).close();
// We don't check the relative ordering of the rollback here,
// because unfortunately we know it's wrong:
// we don't get a "before transaction completion" event for rollbacks,
// so in the case of rollbacks the closing always happen after transaction completion.
}
}
private void spyOnTransaction(XAResource xaResource) {
try {
TestingJtaPlatformImpl.transactionManager().getTransaction().enlistResource( xaResource );