HHH-11740 : test case
(cherry picked from commit36d09411fb
) HHH-11740 : Skip test for H2 (cherry picked from commit6a0a6d7562
) HHH-11740 - Default MultiTableBulkIdStrategy for DB2 does not work with connection pools - Fix tests on PostgreSQL (cherry picked from commit386b04872f
) 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 commit733f55f362
) HHH-11740 - Fix test case to avoid pessimistic locking exception (cherry picked from commitcab613de11
) HHH-11740 : test case (cherry picked from commite6573ed623
)
This commit is contained in:
parent
071d9de96f
commit
abc3eb14a6
|
@ -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" };
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue