Skip to content

Commit

Permalink
HHH-14557 Test JDBC resources are released on each rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Apr 14, 2021
1 parent e03beca commit 571af7b
Showing 1 changed file with 44 additions and 0 deletions.
Expand Up @@ -140,6 +140,50 @@ public void testResourcesReleasedThenConnectionClosedThenCommit() throws SQLExce
}
}

@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 );
Expand Down

0 comments on commit 571af7b

Please sign in to comment.