Skip to content

Commit 9b276ec

Browse files
committed
[#487] Close stateless sessions
1 parent 004d6ac commit 9b276ec

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/BaseReactiveTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public abstract class BaseReactiveTest {
7272
return vertx;
7373
} );
7474

75-
76-
private AutoCloseable session;
75+
private AutoCloseable session, statelessSession;
7776
private ReactiveConnection connection;
7877
private org.hibernate.SessionFactory sessionFactory;
7978
private ReactiveConnectionPool poolProvider;
@@ -213,6 +212,11 @@ public void after(TestContext context) {
213212
session.close();
214213
session = null;
215214
}
215+
if ( statelessSession != null && statelessSession.isOpen() ) {
216+
statelessSession.close();
217+
statelessSession = null;
218+
}
219+
216220
if ( connection != null ) {
217221
try {
218222
connection.close();
@@ -246,6 +250,15 @@ protected Stage.Session openSession() {
246250
return newSession;
247251
}
248252

253+
protected Stage.StatelessSession openStatelessSession() {
254+
if ( statelessSession != null && statelessSession.isOpen() ) {
255+
statelessSession.close();
256+
}
257+
Stage.StatelessSession newSession = getSessionFactory().openStatelessSession();
258+
this.statelessSession = newSession;
259+
return newSession;
260+
}
261+
249262
protected CompletionStage<ReactiveConnection> connection() {
250263
return poolProvider.getConnection().thenApply( c -> connection = c );
251264
}
@@ -268,4 +281,13 @@ protected Mutiny.SessionFactory getMutinySessionFactory() {
268281
return sessionFactory.unwrap( Mutiny.SessionFactory.class );
269282
}
270283

284+
protected Mutiny.StatelessSession openMutinyStatelessSession() {
285+
if ( statelessSession != null ) {
286+
statelessSession.close();
287+
}
288+
Mutiny.StatelessSession newSession = getMutinySessionFactory().openStatelessSession();
289+
this.statelessSession = newSession;
290+
return newSession;
291+
}
292+
271293
}

hibernate-reactive-core/src/test/java/org/hibernate/reactive/EagerOneToManyAssociationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ public void getBookWithAuthors(TestContext context) {
9797

9898
test(
9999
context,
100-
completedFuture( getSessionFactory().openStatelessSession() )
100+
completedFuture( openStatelessSession() )
101101
.thenCompose( s -> voidFuture()
102102
.thenCompose( v -> s.insert(goodOmens) )
103103
.thenCompose( v -> s.insert(neilGaiman) )
104104
.thenCompose( v -> s.insert(terryPratchett) )
105105
)
106-
.thenApply( v -> getSessionFactory().openStatelessSession() )
106+
.thenApply( v -> openStatelessSession() )
107107
.thenCompose( s -> s.get( Book.class, goodOmens.getId() ) )
108108
.thenAccept( optionalBook -> {
109109
context.assertNotNull( optionalBook );

hibernate-reactive-core/src/test/java/org/hibernate/reactive/LazyOneToManyAssociationWithFetchTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ public void getBookWithFetchAuthors(TestContext context) {
246246

247247
test(
248248
context,
249-
completedFuture( getSessionFactory().openStatelessSession() )
249+
completedFuture( openStatelessSession() )
250250
.thenCompose(s -> s.insert(goodOmens)
251251
.thenCompose(v -> s.insert(neilGaiman))
252252
.thenCompose(v -> s.insert(terryPratchett))
253253
)
254-
.thenApply( v -> getSessionFactory().openStatelessSession() )
254+
.thenApply( v -> openStatelessSession() )
255255
.thenCompose( s -> s.get(Book.class, goodOmens.getId())
256256
.thenCompose(
257257
book -> s.fetch( book.getAuthors() )
@@ -276,7 +276,7 @@ public void getBookWithEntityGrpahAuthors(TestContext context) {
276276

277277
test(
278278
context,
279-
completedFuture( getSessionFactory().openStatelessSession() )
279+
completedFuture( openStatelessSession() )
280280
.thenCompose(s -> s.insert(goodOmens)
281281
.thenCompose(v -> s.insert(neilGaiman))
282282
.thenCompose(v -> s.insert(terryPratchett))

hibernate-reactive-core/src/test/java/org/hibernate/reactive/ReactiveStatelessSessionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void cleanDb(TestContext context) {
3838
@Test
3939
public void testStatelessSession(TestContext context) {
4040
GuineaPig pig = new GuineaPig("Aloi");
41-
Stage.StatelessSession ss = getSessionFactory().openStatelessSession();
41+
Stage.StatelessSession ss = openStatelessSession();
4242
test(
4343
context,
4444
ss.insert(pig)
@@ -135,6 +135,7 @@ public void testStatelessSessionCriteria(TestContext context) {
135135
.thenAccept( rows -> context.assertEquals(1, rows) )
136136
.thenCompose( v -> ss.createQuery(delete).executeUpdate() )
137137
.thenAccept( rows -> context.assertEquals(1, rows) )
138+
.thenAccept( v -> ss.close() )
138139
);
139140
}
140141

0 commit comments

Comments
 (0)