1010import java .sql .Connection ;
1111import java .sql .SQLException ;
1212import java .sql .Statement ;
13+ import java .util .Arrays ;
14+ import java .util .List ;
1315import java .util .Map ;
1416import javax .persistence .Entity ;
1517import javax .persistence .Id ;
1921import javax .transaction .xa .XAException ;
2022import javax .transaction .xa .XAResource ;
2123
24+ import org .hibernate .Session ;
2225import org .hibernate .cfg .AvailableSettings ;
2326import org .hibernate .dialect .H2Dialect ;
2427import org .hibernate .engine .jdbc .connections .internal .UserSuppliedConnectionProviderImpl ;
2528import org .hibernate .engine .jdbc .connections .spi .ConnectionProvider ;
29+ import org .hibernate .engine .spi .SessionImplementor ;
2630import org .hibernate .jpa .test .BaseEntityManagerFunctionalTestCase ;
2731import org .hibernate .resource .jdbc .spi .LogicalConnectionImplementor ;
2832import org .hibernate .resource .jdbc .spi .PhysicalConnectionHandlingMode ;
33+
2934import org .hibernate .testing .RequiresDialect ;
3035import org .hibernate .testing .TestForIssue ;
3136import org .hibernate .testing .env .ConnectionProviderBuilder ;
3237import org .hibernate .testing .jta .TestingJtaBootstrap ;
3338import org .hibernate .testing .jta .TestingJtaPlatformImpl ;
39+ import org .hibernate .testing .junit4 .CustomParameterized ;
3440import org .hibernate .testing .transaction .TransactionUtil2 ;
3541import org .junit .Rule ;
3642import org .junit .Test ;
4046import org .mockito .junit .MockitoJUnit ;
4147import org .mockito .junit .MockitoRule ;
4248import org .mockito .quality .Strictness ;
49+ import org .junit .runner .RunWith ;
50+ import org .junit .runners .Parameterized ;
4351
4452import static org .mockito .ArgumentMatchers .any ;
4553import static org .mockito .ArgumentMatchers .anyBoolean ;
5159 * @author Luis Barreiro
5260 */
5361@ RequiresDialect ( H2Dialect .class )
62+ @ RunWith (CustomParameterized .class )
5463public class BeforeCompletionReleaseTest extends BaseEntityManagerFunctionalTestCase {
5564
65+ @ Parameterized .Parameters (name = "{0}" )
66+ public static List <Object []> params () {
67+ return Arrays .asList ( new Object [][] {
68+ {
69+ "Setting connection handling mode from properties" ,
70+ PhysicalConnectionHandlingMode .DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION ,
71+ null
72+ },
73+ {
74+ "Setting connection handling mode through SessionBuilder" ,
75+ PhysicalConnectionHandlingMode .DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT ,
76+ PhysicalConnectionHandlingMode .DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION
77+ }
78+ } );
79+ }
80+
5681 @ Rule
5782 public MockitoRule mockito = MockitoJUnit .rule ().strictness ( Strictness .STRICT_STUBS );
5883
84+ private final PhysicalConnectionHandlingMode connectionHandlingModeInProperties ;
85+ private final PhysicalConnectionHandlingMode connectionHandlingModeInSessionBuilder ;
86+
87+ public BeforeCompletionReleaseTest (
88+ String ignoredTestLabel , PhysicalConnectionHandlingMode connectionHandlingModeInProperties ,
89+ PhysicalConnectionHandlingMode connectionHandlingModeInSessionBuilder ) {
90+ this .connectionHandlingModeInProperties = connectionHandlingModeInProperties ;
91+ this .connectionHandlingModeInSessionBuilder = connectionHandlingModeInSessionBuilder ;
92+ }
93+
5994 @ Override
6095 protected Map getConfig () {
6196 Map config = super .getConfig ();
6297 TestingJtaBootstrap .prepare ( config );
6398 config .put ( AvailableSettings .CONNECTION_PROVIDER , new ConnectionProviderDecorator () );
64- config .put ( AvailableSettings .CONNECTION_HANDLING , PhysicalConnectionHandlingMode .DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION );
99+ if ( connectionHandlingModeInProperties != null ) {
100+ config .put ( AvailableSettings .CONNECTION_HANDLING , connectionHandlingModeInProperties );
101+ }
65102 return config ;
66103 }
67104
@@ -77,17 +114,19 @@ public void testResourcesReleasedThenConnectionClosedThenCommit() throws SQLExce
77114 Connection [] connectionSpies = new Connection [1 ];
78115 Statement statementMock = Mockito .mock ( Statement .class );
79116
80- TransactionUtil2 .inTransaction ( entityManagerFactory (), session -> {
81- spyOnTransaction ( transactionSpy );
117+ try (SessionImplementor s = (SessionImplementor ) openSession ()) {
118+ TransactionUtil2 .inTransaction ( s , session -> {
119+ spyOnTransaction ( transactionSpy );
82120
83- Thing thing = new Thing ();
84- thing .setId ( 1 );
85- session .persist ( thing );
121+ Thing thing = new Thing ();
122+ thing .setId ( 1 );
123+ session .persist ( thing );
86124
87- LogicalConnectionImplementor logicalConnection = session .getJdbcCoordinator ().getLogicalConnection ();
88- logicalConnection .getResourceRegistry ().register ( statementMock , true );
89- connectionSpies [0 ] = logicalConnection .getPhysicalConnection ();
90- } );
125+ LogicalConnectionImplementor logicalConnection = session .getJdbcCoordinator ().getLogicalConnection ();
126+ logicalConnection .getResourceRegistry ().register ( statementMock , true );
127+ connectionSpies [0 ] = logicalConnection .getPhysicalConnection ();
128+ } );
129+ }
91130
92131 Connection connectionSpy = connectionSpies [0 ];
93132
@@ -107,6 +146,13 @@ private void spyOnTransaction(XAResource xaResource) {
107146 }
108147 }
109148
149+ private Session openSession () {
150+ return connectionHandlingModeInSessionBuilder == null
151+ ? entityManagerFactory ().openSession ()
152+ : entityManagerFactory ().withOptions ().connectionHandlingMode ( connectionHandlingModeInSessionBuilder )
153+ .openSession ();
154+ }
155+
110156 // --- //
111157
112158 @ Entity (name = "Thing" )
@@ -146,3 +192,4 @@ public void closeConnection(Connection connection) throws SQLException {
146192 }
147193 }
148194}
195+
0 commit comments