HHH-9946 - Do better job cleaning up "stray" transactions in base test

This commit is contained in:
Steve Ebersole 2015-07-18 14:20:02 -05:00
parent ee2099ff8a
commit 6812c07a16
2 changed files with 53 additions and 6 deletions

View File

@ -34,6 +34,7 @@ import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.jdbc.AbstractReturningWork;
import org.hibernate.jdbc.Work;
import org.hibernate.resource.transaction.TransactionCoordinator;
import org.hibernate.testing.AfterClassOnce;
import org.hibernate.testing.BeforeClassOnce;
@ -322,6 +323,8 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
@After
public final void afterTest() throws Exception {
completeStrayTransaction();
if ( isCleanupTestDataRequired() ) {
cleanupTestData();
}
@ -333,6 +336,30 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
}
private void completeStrayTransaction() {
if ( session == null ) {
// nothing to do
return;
}
if ( ( (SessionImplementor) session ).isClosed() ) {
// nothing to do
return;
}
if ( !session.isConnected() ) {
// nothing to do
return;
}
final TransactionCoordinator.TransactionDriver tdc =
( (SessionImplementor) session ).getTransactionCoordinator().getTransactionDriverControl();
if ( tdc.getStatus().canRollback() ) {
session.getTransaction().rollback();
}
}
protected void cleanupCache() {
if ( sessionFactory != null ) {
sessionFactory.getCache().evictAllRegions();
@ -354,9 +381,6 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
private void cleanupSession() {
if ( session != null && ! ( (SessionImplementor) session ).isClosed() ) {
if ( session.isConnected() ) {
session.doWork( new RollbackWork() );
}
session.close();
}
session = null;

View File

@ -42,6 +42,7 @@ import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.resource.transaction.TransactionCoordinator;
import org.hibernate.type.BlobType;
import org.hibernate.type.ClobType;
import org.hibernate.type.NClobType;
@ -439,6 +440,8 @@ public class BaseNonConfigCoreFunctionalTestCase extends BaseUnitTestCase {
@After
public final void afterTest() throws Exception {
completeStrayTransaction();
if ( isCleanupTestDataRequired() ) {
cleanupTestData();
}
@ -447,7 +450,30 @@ public class BaseNonConfigCoreFunctionalTestCase extends BaseUnitTestCase {
cleanupSession();
assertAllDataRemoved();
}
private void completeStrayTransaction() {
if ( session == null ) {
// nothing to do
return;
}
if ( ( (SessionImplementor) session ).isClosed() ) {
// nothing to do
return;
}
if ( !session.isConnected() ) {
// nothing to do
return;
}
final TransactionCoordinator.TransactionDriver tdc =
( (SessionImplementor) session ).getTransactionCoordinator().getTransactionDriverControl();
if ( tdc.getStatus().canRollback() ) {
session.getTransaction().rollback();
}
}
protected boolean isCleanupTestDataRequired() {
@ -465,9 +491,6 @@ public class BaseNonConfigCoreFunctionalTestCase extends BaseUnitTestCase {
private void cleanupSession() {
if ( session != null && ! ( (SessionImplementor) session ).isClosed() ) {
if ( session.isConnected() ) {
session.doWork( new RollbackWork() );
}
session.close();
}
session = null;