44 */
55package org .hibernate .orm .test .connections ;
66
7- import java .sql .Connection ;
8- import java .util .ArrayList ;
9- import java .util .List ;
10- import java .util .Map ;
11-
127import org .hibernate .ScrollableResults ;
138import org .hibernate .Session ;
14- import org .hibernate .cfg .AvailableSettings ;
159import org .hibernate .cfg .Environment ;
1610import org .hibernate .dialect .H2Dialect ;
1711import org .hibernate .engine .jdbc .connections .spi .ConnectionProvider ;
12+ import org .hibernate .engine .spi .SessionFactoryImplementor ;
1813import org .hibernate .internal .util .SerializationHelper ;
1914import org .hibernate .resource .jdbc .spi .PhysicalConnectionHandlingMode ;
20- import org .hibernate .resource .transaction .backend .jta .internal .JtaTransactionCoordinatorBuilderImpl ;
2115import org .hibernate .stat .Statistics ;
22-
23- import org .hibernate .testing .RequiresDialect ;
2416import org .hibernate .testing .jta .TestingJtaBootstrap ;
2517import org .hibernate .testing .jta .TestingJtaPlatformImpl ;
26- import org .junit .Test ;
18+ import org .hibernate .testing .orm .junit .RequiresDialect ;
19+ import org .hibernate .testing .orm .junit .ServiceRegistry ;
20+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
21+ import org .hibernate .testing .orm .junit .Setting ;
22+ import org .hibernate .testing .orm .junit .SettingConfiguration ;
23+ import org .hibernate .testing .orm .junit .SettingProvider ;
24+ import org .junit .jupiter .api .Test ;
25+
26+ import java .sql .Connection ;
27+ import java .util .ArrayList ;
28+ import java .util .List ;
2729
28- import static org .junit .Assert .assertEquals ;
29- import static org .junit .Assert .assertTrue ;
30- import static org .junit .Assert .fail ;
30+ import static org .assertj .core .api .Assertions .assertThat ;
31+ import static org .hibernate .cfg .TransactionSettings .ENABLE_LAZY_LOAD_NO_TRANS ;
32+ import static org .hibernate .cfg .TransactionSettings .TRANSACTION_COORDINATOR_STRATEGY ;
33+ import static org .junit .jupiter .api .Assertions .fail ;
3134
3235/**
3336 * Implementation of AggressiveReleaseTest.
3437 *
3538 * @author Steve Ebersole
3639 */
3740@ RequiresDialect (H2Dialect .class )
41+ @ ServiceRegistry (
42+ settings = {
43+ @ Setting (name = Environment .GENERATE_STATISTICS , value = "true" ),
44+ @ Setting (name = Environment .STATEMENT_BATCH_SIZE , value = "0" ),
45+ @ Setting (name = TRANSACTION_COORDINATOR_STRATEGY , value = "jta" ),
46+ @ Setting (name = ENABLE_LAZY_LOAD_NO_TRANS , value = "true" )
47+ },
48+ settingProviders = @ SettingProvider (
49+ settingName = Environment .CONNECTION_HANDLING ,
50+ provider = AggressiveReleaseTest .ConnectionmHandlingProvider .class
51+ ),
52+ settingConfigurations = @ SettingConfiguration (configurer = TestingJtaBootstrap .class )
53+ )
3854public class AggressiveReleaseTest extends ConnectionManagementTestCase {
39- @ Override
40- protected void addSettings (Map <String ,Object > settings ) {
41- super .addSettings ( settings );
42-
43- TestingJtaBootstrap .prepare ( settings );
44- // settings.put( Environment.TRANSACTION_STRATEGY, CMTTransactionFactory.class.getName() );
45- settings .put ( AvailableSettings .TRANSACTION_COORDINATOR_STRATEGY , JtaTransactionCoordinatorBuilderImpl .class .getName () );
46- settings .put ( Environment .CONNECTION_HANDLING , PhysicalConnectionHandlingMode .DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT .toString () );
47- settings .put ( Environment .GENERATE_STATISTICS , "true" );
48- settings .put ( Environment .STATEMENT_BATCH_SIZE , "0" );
55+
56+ public static class ConnectionmHandlingProvider implements SettingProvider .Provider <String > {
57+ @ Override
58+ public String getSetting () {
59+ return PhysicalConnectionHandlingMode .DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT .toString ();
60+ }
4961 }
5062
5163 @ Override
52- protected Session getSessionUnderTest () throws Throwable {
53- return openSession ();
64+ protected Session getSessionUnderTest (SessionFactoryScope scope ) {
65+ return scope . getSessionFactory (). openSession ();
5466 }
5567
5668 @ Override
@@ -70,10 +82,11 @@ protected void done() throws Throwable {
7082 // Some additional tests specifically for the aggressive-release functionality...
7183
7284 @ Test
73- public void testSerializationOnAfterStatementAggressiveRelease () throws Throwable {
85+ public void testSerializationOnAfterStatementAggressiveRelease (SessionFactoryScope scope ) throws Throwable {
7486 prepare ();
87+ Session s = null ;
7588 try {
76- Session s = getSessionUnderTest ();
89+ s = getSessionUnderTest ( scope );
7790 Silly silly = new Silly ( "silly" );
7891 s .persist ( silly );
7992
@@ -86,134 +99,157 @@ public void testSerializationOnAfterStatementAggressiveRelease() throws Throwabl
8699 s .remove ( silly );
87100 s .flush ();
88101
89- release ( s );
90102 }
91103 finally {
104+ release ( s , scope );
92105 done ();
93106 }
94107 }
95108
96109 @ Test
97- public void testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources () throws Throwable {
110+ public void testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources (SessionFactoryScope scope )
111+ throws Throwable {
98112 prepare ();
99- Session s = getSessionUnderTest ();
113+ try ( Session s = getSessionUnderTest ( scope )) {
100114
101- Silly silly = new Silly ( "silly" );
102- s .persist ( silly );
103-
104- // this should cause the CM to obtain a connection, and then release it
105- s .flush ();
115+ Silly silly = new Silly ( "silly" );
116+ s .persist ( silly );
106117
107- // both scroll() and iterate() cause batching to hold on
108- // to resources, which should make aggressive-release not release
109- // the connection (and thus cause serialization to fail)
110- try (ScrollableResults sr = s .createQuery ( "from Silly" ).scroll ()) {
111- sr .next ();
118+ // this should cause the CM to obtain a connection, and then release it
119+ s .flush ();
112120
113- try {
114- SerializationHelper .serialize ( s );
115- fail ( "Serialization allowed on connected session; or aggressive release released connection with open resources" );
116- }
117- catch (IllegalStateException e ) {
118- // expected behavior
121+ // both scroll() and iterate() cause batching to hold on
122+ // to resources, which should make aggressive-release not release
123+ // the connection (and thus cause serialization to fail)
124+ try (ScrollableResults <Silly > sr = s .createQuery ( "from Silly" , Silly .class ).scroll ()) {
125+ sr .next ();
126+
127+ try {
128+ SerializationHelper .serialize ( s );
129+ fail ( "Serialization allowed on connected session; or aggressive release released connection with open resources" );
130+ }
131+ catch (IllegalStateException e ) {
132+ // expected behavior
133+ }
134+
135+ // getting the first row only because SybaseASE15Dialect throws NullPointerException
136+ // if data is not read before closing the ResultSet
137+ sr .next ();
138+
139+ // Closing the ScrollableResults does currently force batching to
140+ // aggressively release the connection
119141 }
142+ SerializationHelper .serialize ( s );
120143
121- // getting the first row only because SybaseASE15Dialect throws NullPointerException
122- // if data is not read before closing the ResultSet
123- sr .next ();
124-
125- // Closing the ScrollableResults does currently force batching to
126- // aggressively release the connection
144+ s .remove ( silly );
145+ s .flush ();
127146 }
128- SerializationHelper .serialize ( s );
129-
130- s .remove ( silly );
131- s .flush ();
132-
133- release ( s );
134147 done ();
135148 }
136149
137150 @ Test
138- public void testQueryScrolling () throws Throwable {
151+ public void testQueryScrolling (SessionFactoryScope scope ) throws Throwable {
139152 prepare ();
140- Session s = getSessionUnderTest ();
141- Silly silly = new Silly ( "silly" );
142- s .persist ( silly );
143- s .flush ();
144-
145- try (ScrollableResults sr = s .createQuery ( "from Silly" ).scroll ()) {
146- assertTrue ( sr .next () );
147- Silly silly2 = (Silly ) sr .get ();
148- assertEquals ( silly , silly2 );
149- }
153+ Session s = null ;
154+ try {
155+ s = getSessionUnderTest (scope );
156+ Silly silly = new Silly ( "silly" );
157+ s .persist ( silly );
158+ s .flush ();
150159
151- try (ScrollableResults sr = s .createQuery ( "from Silly" ).scroll ();
152- ScrollableResults sr2 = s .createQuery ( "from Silly where name = 'silly'" ).scroll ()) {
153- assertTrue ( sr .next () );
154- assertEquals ( silly , sr .get () );
155- assertTrue ( sr2 .next () );
156- assertEquals ( silly , sr2 .get () );
157- }
160+ try (ScrollableResults <Silly > sr = s .createQuery ( "from Silly" , Silly .class ).scroll ()) {
161+ assertThat ( sr .next () ).isTrue ();
162+ Silly silly2 = sr .get ();
163+ assertThat ( silly2 ).isEqualTo ( silly );
164+ }
158165
159- s .remove ( silly );
160- s .flush ();
166+ try (ScrollableResults <Silly > sr = s .createQuery ( "from Silly" , Silly .class ).scroll ();
167+ ScrollableResults <Silly > sr2 = s .createQuery ( "from Silly where name = 'silly'" , Silly .class )
168+ .scroll ()) {
169+ assertThat ( sr .next () ).isTrue ();
170+ assertThat ( sr .get () ).isEqualTo ( silly );
171+ assertThat ( sr2 .next () ).isTrue ();
172+ assertThat ( sr2 .get () ).isEqualTo ( silly );
173+ }
161174
162- release ( s );
163- done ();
175+ s .remove ( silly );
176+ s .flush ();
177+ }
178+ finally {
179+ release ( s , scope );
180+ done ();
181+ }
164182 }
165183
166184 @ Test
167- public void testSuppliedConnection () throws Throwable {
185+ public void testSuppliedConnection (SessionFactoryScope scope ) throws Throwable {
186+ SessionFactoryImplementor sessionFactory = scope .getSessionFactory ();
168187 prepare ();
169188
170- Connection originalConnection = sessionFactory () .getServiceRegistry ().getService ( ConnectionProvider .class ). getConnection ();
171- Session session = sessionFactory (). withOptions (). connection ( originalConnection ). openSession ();
172-
173- Silly silly = new Silly ( "silly" );
174- session . persist ( silly );
189+ Connection originalConnection = sessionFactory .getServiceRegistry ().getService ( ConnectionProvider .class )
190+ . getConnection ();
191+ Session session = null ;
192+ try {
193+ session = sessionFactory . withOptions (). connection ( originalConnection ). openSession ( );
175194
176- // this will cause the connection manager to cycle through the aggressive release logic;
177- // it should not release the connection since we explicitly suplied it ourselves.
178- session .flush ();
179- assertTrue ( session .isConnected () );
195+ Silly silly = new Silly ( "silly" );
196+ session .persist ( silly );
180197
181- session .remove ( silly );
182- session .flush ();
198+ // this will cause the connection manager to cycle through the aggressive release logic;
199+ // it should not release the connection since we explicitly suplied it ourselves.
200+ session .flush ();
201+ assertThat ( session .isConnected () ).isTrue ();
183202
184- release ( session );
185- done ();
203+ session .remove ( silly );
204+ session .flush ();
205+ }
206+ finally {
207+ release ( session , scope );
208+ done ();
209+ sessionFactory .getServiceRegistry ().getService ( ConnectionProvider .class )
210+ .closeConnection ( originalConnection );
186211
187- sessionFactory (). getServiceRegistry (). getService ( ConnectionProvider . class ). closeConnection ( originalConnection );
212+ }
188213 }
189214
190215 @ Test
191- public void testConnectionMaintanenceDuringFlush () throws Throwable {
192- final Statistics statistics = sessionFactory ().getStatistics ();
216+ public void testConnectionMaintanenceDuringFlush (SessionFactoryScope scope ) throws Throwable {
217+ SessionFactoryImplementor sessionFactory = scope .getSessionFactory ();
218+
219+ final Statistics statistics = sessionFactory .getStatistics ();
193220 prepare ();
194- Session s = getSessionUnderTest ();
195221
196- List <Silly > entities = new ArrayList <Silly >();
197- for ( int i = 0 ; i < 10 ; i ++ ) {
198- Other other = new Other ( "other-" + i );
199- Silly silly = new Silly ( "silly-" + i , other );
200- entities .add ( silly );
201- s .persist ( silly );
202- }
203- s .flush ();
222+ Session s = null ;
223+ try {
224+ s = getSessionUnderTest ( scope );
225+
226+ List <Silly > entities = new ArrayList <>();
227+ for ( int i = 0 ; i < 10 ; i ++ ) {
228+ Other other = new Other ( "other-" + i );
229+ Silly silly = new Silly ( "silly-" + i , other );
230+ entities .add ( silly );
231+ s .persist ( silly );
232+ }
233+ s .flush ();
204234
205- for ( Silly silly : entities ) {
206- silly .setName ( "new-" + silly .getName () );
207- silly .getOther ().setName ( "new-" + silly .getOther ().getName () );
235+ for ( Silly silly : entities ) {
236+ silly .setName ( "new-" + silly .getName () );
237+ silly .getOther ().setName ( "new-" + silly .getOther ().getName () );
238+ }
239+ long initialCount = statistics .getConnectCount ();
240+ s .flush ();
241+ assertThat ( statistics .getConnectCount () )
242+ .describedAs ( "connection not maintained through flush" )
243+ .isEqualTo ( initialCount + 1 );
244+
245+ s .createMutationQuery ( "delete from Silly" ).executeUpdate ();
246+ s .createMutationQuery ( "delete from Other" ).executeUpdate ();
247+ s .getTransaction ().commit ();
208248 }
209- long initialCount = statistics .getConnectCount ();
210- s .flush ();
211- assertEquals ( "connection not maintained through flush" , initialCount + 1 , statistics .getConnectCount () );
212-
213- s .createQuery ( "delete from Silly" ).executeUpdate ();
214- s .createQuery ( "delete from Other" ).executeUpdate ();
215- s .getTransaction ().commit ();
216- release ( s );
217- done ();
249+ finally {
250+ release ( s , scope );
251+ done ();
252+ }
253+
218254 }
219255}
0 commit comments