diff --git a/hibernate-core/src/test/java/org/hibernate/test/connections/BeforeCompletionReleaseTest.java b/hibernate-core/src/test/java/org/hibernate/test/connections/BeforeCompletionReleaseTest.java index 91ed0e6d66..4d50804aef 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/connections/BeforeCompletionReleaseTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/connections/BeforeCompletionReleaseTest.java @@ -10,6 +10,8 @@ package org.hibernate.test.connections; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.util.Arrays; +import java.util.List; import java.util.Map; import javax.persistence.Entity; import javax.persistence.Id; @@ -19,18 +21,22 @@ import javax.transaction.SystemException; import javax.transaction.xa.XAException; import javax.transaction.xa.XAResource; +import org.hibernate.Session; import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.H2Dialect; import org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl; import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; +import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; + import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.env.ConnectionProviderBuilder; import org.hibernate.testing.jta.TestingJtaBootstrap; import org.hibernate.testing.jta.TestingJtaPlatformImpl; +import org.hibernate.testing.junit4.CustomParameterized; import org.hibernate.testing.transaction.TransactionUtil2; import org.junit.Rule; import org.junit.Test; @@ -40,6 +46,8 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.mockito.quality.Strictness; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; @@ -51,17 +59,46 @@ import static org.mockito.Mockito.spy; * @author Luis Barreiro */ @RequiresDialect( H2Dialect.class ) +@RunWith(CustomParameterized.class) public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTestCase { + @Parameterized.Parameters(name = "{0}") + public static List params() { + return Arrays.asList( new Object[][] { + { + "Setting connection handling mode from properties", + PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION, + null + }, + { + "Setting connection handling mode through SessionBuilder", + PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT, + PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION + } + } ); + } + @Rule public MockitoRule mockito = MockitoJUnit.rule().strictness( Strictness.STRICT_STUBS ); + private final PhysicalConnectionHandlingMode connectionHandlingModeInProperties; + private final PhysicalConnectionHandlingMode connectionHandlingModeInSessionBuilder; + + public BeforeCompletionReleaseTest( + String ignoredTestLabel, PhysicalConnectionHandlingMode connectionHandlingModeInProperties, + PhysicalConnectionHandlingMode connectionHandlingModeInSessionBuilder) { + this.connectionHandlingModeInProperties = connectionHandlingModeInProperties; + this.connectionHandlingModeInSessionBuilder = connectionHandlingModeInSessionBuilder; + } + @Override protected Map getConfig() { Map config = super.getConfig(); TestingJtaBootstrap.prepare( config ); config.put( AvailableSettings.CONNECTION_PROVIDER, new ConnectionProviderDecorator() ); - config.put( AvailableSettings.CONNECTION_HANDLING, PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION ); + if ( connectionHandlingModeInProperties != null ) { + config.put( AvailableSettings.CONNECTION_HANDLING, connectionHandlingModeInProperties ); + } return config; } @@ -77,17 +114,19 @@ public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTest Connection[] connectionSpies = new Connection[1]; Statement statementMock = Mockito.mock( Statement.class ); - TransactionUtil2.inTransaction( entityManagerFactory(), session -> { - spyOnTransaction( transactionSpy ); + try (SessionImplementor s = (SessionImplementor) openSession()) { + TransactionUtil2.inTransaction( s, session -> { + spyOnTransaction( transactionSpy ); - Thing thing = new Thing(); - thing.setId( 1 ); - session.persist( thing ); + Thing thing = new Thing(); + thing.setId( 1 ); + session.persist( thing ); - LogicalConnectionImplementor logicalConnection = session.getJdbcCoordinator().getLogicalConnection(); - logicalConnection.getResourceRegistry().register( statementMock, true ); - connectionSpies[0] = logicalConnection.getPhysicalConnection(); - } ); + LogicalConnectionImplementor logicalConnection = session.getJdbcCoordinator().getLogicalConnection(); + logicalConnection.getResourceRegistry().register( statementMock, true ); + connectionSpies[0] = logicalConnection.getPhysicalConnection(); + } ); + } Connection connectionSpy = connectionSpies[0]; @@ -107,6 +146,13 @@ public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTest } } + private Session openSession() { + return connectionHandlingModeInSessionBuilder == null + ? entityManagerFactory().openSession() + : entityManagerFactory().withOptions().connectionHandlingMode( connectionHandlingModeInSessionBuilder ) + .openSession(); + } + // --- // @Entity(name = "Thing") @@ -146,3 +192,4 @@ public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTest } } } +