HHH-11740 - Fix test case to avoid pessimistic locking exception

This commit is contained in:
Gail Badner 2017-05-15 14:38:22 -07:00
parent 733f55f362
commit cab613de11
1 changed files with 92 additions and 45 deletions

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.test.unionsubclass; package org.hibernate.test.unionsubclass;
import java.sql.Connection;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -15,12 +16,16 @@ import org.hibernate.FetchMode;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SharedSessionContract;
import org.hibernate.Transaction; import org.hibernate.Transaction;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order; import org.hibernate.criterion.Order;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.SQLServerDialect; import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.jdbc.Work;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
@ -33,6 +38,7 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -437,67 +443,108 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-11740" ) @TestForIssue( jiraKey = "HHH-11740" )
public void testBulkOperationsInTwoConcurrentSessions() throws Exception { public void testBulkOperationsWithDifferentConnections() throws Exception {
doInHibernate( this::sessionFactory, s -> { doInHibernate(
Location mars = new Location( "Mars" ); this::sessionFactory, s -> {
s.persist( mars ); Location mars = new Location( "Mars" );
s.persist( mars );
Location earth = new Location( "Earth" ); Location earth = new Location( "Earth" );
s.persist( earth ); s.persist( earth );
Hive hive = new Hive(); Hive hive = new Hive();
hive.setLocation( mars ); hive.setLocation( mars );
s.persist( hive ); s.persist( hive );
Alien alien = new Alien(); Alien alien = new Alien();
alien.setIdentity( "Uncle Martin" ); alien.setIdentity( "Uncle Martin" );
alien.setSpecies( "Martian" ); alien.setSpecies( "Martian" );
alien.setHive( hive ); alien.setHive( hive );
hive.getMembers().add( alien ); hive.getMembers().add( alien );
mars.addBeing( alien ); mars.addBeing( alien );
s.persist( alien ); s.persist( alien );
Human human = new Human(); Human human = new Human();
human.setIdentity( "Jane Doe" ); human.setIdentity( "Jane Doe" );
human.setSex( 'M' ); human.setSex( 'M' );
earth.addBeing( human ); earth.addBeing( human );
s.persist( human ); s.persist( human );
} ); }
);
AtomicBoolean pessimisticLocking = new AtomicBoolean( false ); // The following tests that bulk operations can be executed using 2 different
// connections.
doInHibernate( this::sessionFactory, s1 -> { doInHibernate( this::sessionFactory, s1 -> {
TransactionUtil.setJdbcTimeout( s1 ); // Transaction used by s1 is already started.
// Assert that the Connection is already physically connected.
SharedSessionContractImplementor s1Implementor = (SharedSessionContractImplementor) s1;
assertTrue( s1Implementor.getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected() );
// Assert that the same Connection will be used for s1's entire transaction
assertEquals(
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION,
s1Implementor.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode()
);
// Get the Connection s1 will use.
final Connection connection1 = s1Implementor.connection();
// Avoid a pessimistic lock exception by not doing anything with s1 until
// after a second Session (with a different connection) is used
// for a bulk operation.
doInHibernate( this::sessionFactory, s2 -> {
// Check same assertions for s2 as was done for s1.
SharedSessionContractImplementor s2Implementor = (SharedSessionContractImplementor) s2;
assertTrue( s2Implementor.getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected() );
assertEquals(
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION,
s2Implementor.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode()
);
// Get the Connection s2 will use.
Connection connection2 = s2Implementor.connection();
// Assert that connection2 is not the same as connection1
assertNotSame( connection1, connection2 );
// Execute a bulk operation on s2 (using connection2)
assertEquals(
1,
s2.createQuery( "delete from Being where species = 'Martian'" ).executeUpdate()
);
// Assert the Connection has not changed
assertSame( connection2, s2Implementor.connection() );
}
);
// Assert that the Connection used by s1 has hot changed.
assertSame( connection1, s1Implementor.connection() );
// Execute a bulk operation on s1 (using connection1)
assertEquals( assertEquals(
1, 1,
s1.createQuery( "update Being set identity = 'John Doe' where identity = 'Jane Doe'" ) s1.createQuery( "update Being set identity = 'John Doe' where identity = 'Jane Doe'" )
.executeUpdate() .executeUpdate()
); );
try { // Assert that the Connection used by s1 has hot changed.
doInHibernate( this::sessionFactory, s2 -> { assertSame( connection1, s1Implementor.connection() );
TransactionUtil.setJdbcTimeout( s2 );
assertEquals( 1, s2.createQuery( "delete from Being where species = 'Martian'" ).executeUpdate() );
} );
}
catch (Exception e) {
if ( !ExceptionUtil.isSqlLockTimeout(e) ) {
fail( e.getMessage() );
}
pessimisticLocking.set( true );
}
} );
doInHibernate( this::sessionFactory, s -> { });
if ( !pessimisticLocking.get() ) {
Human human = (Human) s.createQuery( "from Being" ).uniqueResult(); // Clean up
assertEquals( "John Doe", human.getIdentity() ); doInHibernate(
} this::sessionFactory, s -> {
s.createQuery( "delete from Being" ).executeUpdate(); Human human = (Human) s.createQuery( "from Being" ).uniqueResult();
s.createQuery( "delete from Hive" ).executeUpdate(); assertEquals( "John Doe", human.getIdentity() );
s.createQuery( "delete from Location" ).executeUpdate(); s.createQuery( "delete from Being" ).executeUpdate();
s.createQuery( "delete from Hive" ).executeUpdate();
s.createQuery( "delete from Location" ).executeUpdate();
} ); } );
} }
} }