From abc3eb14a69ee534c28c16f7c964f0cac3e6a77d Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 12 May 2017 13:48:03 -0700 Subject: [PATCH] HHH-11740 : test case (cherry picked from commit 36d09411fb1e36c9d3a4450bcea96f80911d18de) HHH-11740 : Skip test for H2 (cherry picked from commit 6a0a6d756295c517fb9cb1ee1505c3cb9cfd4784) HHH-11740 - Default MultiTableBulkIdStrategy for DB2 does not work with connection pools - Fix tests on PostgreSQL (cherry picked from commit 386b04872ff181ba7f377742e622ee44168b079b) HHH-11740 - Default MultiTableBulkIdStrategy for DB2 does not work with connection pools - Made test run on H2, Oracle, SQL server, MySQL and PostgreSQL (cherry picked from commit 733f55f36283e56fa3a9e17595a654f75c5e2cf6) HHH-11740 - Fix test case to avoid pessimistic locking exception (cherry picked from commit cab613de11deb3c3fabc43211908fe97d1fd248b) HHH-11740 : test case (cherry picked from commit e6573ed623cd5fd317b1b6374bbe6561c494f70d) --- .../test/unionsubclass/UnionSubclassTest.java | 131 +++++++++++++++++- 1 file changed, 129 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/unionsubclass/UnionSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/unionsubclass/UnionSubclassTest.java index e7902029ec..23184d1464 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/unionsubclass/UnionSubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/unionsubclass/UnionSubclassTest.java @@ -6,6 +6,7 @@ */ package org.hibernate.test.unionsubclass; +import java.sql.Connection; import java.util.List; import org.junit.Test; @@ -16,11 +17,17 @@ import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Order; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.resource.jdbc.spi.JdbcSessionOwner; +import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; + +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; @@ -29,6 +36,7 @@ import static org.junit.Assert.assertTrue; */ @SuppressWarnings("unchecked") public class UnionSubclassTest extends BaseCoreFunctionalTestCase { + @Override public String[] getMappings() { return new String[] { "unionsubclass/Beings.hbm.xml" }; @@ -76,8 +84,8 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase { gavin.setLocation(mel); mel.addBeing(gavin); Human max = new Human(); - max.setIdentity("max"); - max.setSex('M'); + max.setIdentity( "max" ); + max.setSex( 'M' ); max.setLocation(mel); mel.addBeing(gavin); @@ -403,5 +411,124 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase { s.close(); } + @Test + @TestForIssue( jiraKey = "HHH-11740" ) + public void testBulkOperationsWithDifferentConnections() throws Exception { + Session s = openSession(); + s.getTransaction().begin(); + { + Location mars = new Location( "Mars" ); + s.persist( mars ); + + Location earth = new Location( "Earth" ); + s.persist( earth ); + + Hive hive = new Hive(); + hive.setLocation( mars ); + s.persist( hive ); + + Alien alien = new Alien(); + alien.setIdentity( "Uncle Martin" ); + alien.setSpecies( "Martian" ); + alien.setHive( hive ); + hive.getMembers().add( alien ); + mars.addBeing( alien ); + + s.persist( alien ); + + Human human = new Human(); + human.setIdentity( "Jane Doe" ); + human.setSex( 'M' ); + earth.addBeing( human ); + + s.persist( human ); + } + s.getTransaction().commit(); + s.close(); + + // The following tests that bulk operations can be executed using 2 different + // connections. + + Session s1 = openSession(); + s1.getTransaction().begin(); + { + // Transaction used by s1 is already started. + // Assert that the Connection is already physically connected. + SessionImplementor s1Implementor = (SessionImplementor) 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, + ( (JdbcSessionOwner) s1Implementor ).getJdbcSessionContext().getPhysicalConnectionHandlingMode() + ); + + // 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. + + Session s2 = openSession(); + s2.getTransaction().begin(); + { + // Check same assertions for s2 as was done for s1. + SessionImplementor s2Implementor = (SessionImplementor) s2; + assertTrue( s2Implementor.getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected() ); + assertEquals( + PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION, + ( (JdbcSessionOwner) s2Implementor ).getJdbcSessionContext().getPhysicalConnectionHandlingMode() + ); + + // 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() ); + } + s2.getTransaction().commit(); + s2.close(); + + // Assert that the Connection used by s1 has hot changed. + assertSame( connection1, s1Implementor.connection() ); + + // Execute a bulk operation on s1 (using connection1) + assertEquals( + 1, + s1.createQuery( "update Being set identity = 'John Doe' where identity = 'Jane Doe'" ) + .executeUpdate() + ); + + // Assert that the Connection used by s1 has hot changed. + assertSame( connection1, s1Implementor.connection() ); + + } + s1.getTransaction().commit(); + s1.close(); + + + // Clean up + s = openSession(); + s.getTransaction().begin(); + { + Human human = (Human) s.createQuery( "from Being" ).uniqueResult(); + assertEquals( "John Doe", human.getIdentity() ); + s.createQuery( "delete from Being" ).executeUpdate(); + s.createQuery( "delete from Hive" ).executeUpdate(); + s.createQuery( "delete from Location" ).executeUpdate(); + } + s.getTransaction().commit(); + s.close(); + } }