HHH-14404 Test setting the connection handling mode through SessionBuilder

Signed-off-by: Yoann Rodière <yoann@hibernate.org>
This commit is contained in:
Yoann Rodière 2021-01-14 11:25:46 +01:00 committed by Sanne Grinovero
parent d0b44c48ef
commit 8210bc220b
1 changed files with 57 additions and 10 deletions

View File

@ -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<Object[]> 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,7 +114,8 @@ public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTest
Connection[] connectionSpies = new Connection[1];
Statement statementMock = Mockito.mock( Statement.class );
TransactionUtil2.inTransaction( entityManagerFactory(), session -> {
try (SessionImplementor s = (SessionImplementor) openSession()) {
TransactionUtil2.inTransaction( s, session -> {
spyOnTransaction( transactionSpy );
Thing thing = new Thing();
@ -88,6 +126,7 @@ public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTest
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
}
}
}