Skip to content

Commit 2739325

Browse files
committed
[#2518] Minor clean up
1 parent e27a041 commit 2739325

File tree

7 files changed

+280
-312
lines changed

7 files changed

+280
-312
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/impl/MutinySessionFactoryImpl.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,18 @@ public Context getContext() {
9292
public Uni<Mutiny.Session> openSession() {
9393
SessionCreationOptions options = options();
9494
return uni( () -> connection( getTenantIdentifier( options ) ) )
95-
.chain( reactiveConnection -> create( reactiveConnection,
96-
() -> new ReactiveSessionImpl( delegate, options, reactiveConnection ) ) )
97-
.map( s -> new MutinySessionImpl(s, this) );
95+
.chain( reactiveConnection -> create(
96+
reactiveConnection,
97+
() -> new ReactiveSessionImpl( delegate, options, reactiveConnection )
98+
) )
99+
.map( s -> new MutinySessionImpl( s, this ) );
98100
}
99101

100102
@Override
101103
public Uni<Mutiny.Session> openSession(String tenantId) {
102104
return uni( () -> connection( tenantId ) )
103-
.chain( reactiveConnection -> create( reactiveConnection,
104-
() -> new ReactiveSessionImpl( delegate, options( tenantId ), reactiveConnection ) ) )
105-
.map( s -> new MutinySessionImpl(s, this) );
105+
.chain( reactiveConnection -> create( reactiveConnection, () -> new ReactiveSessionImpl( delegate, options( tenantId ), reactiveConnection ) ) )
106+
.map( s -> new MutinySessionImpl( s, this ) );
106107
}
107108

108109
/**
@@ -122,16 +123,20 @@ private static Uni<Void> close(ReactiveConnection connection) {
122123
public Uni<Mutiny.StatelessSession> openStatelessSession() {
123124
SessionCreationOptions options = options();
124125
return uni( () -> connection( getTenantIdentifier( options ) ) )
125-
.chain( reactiveConnection -> create( reactiveConnection,
126-
() -> new ReactiveStatelessSessionImpl( delegate, options, reactiveConnection ) ) )
127-
.map( s -> new MutinyStatelessSessionImpl(s, this) );
126+
.chain( reactiveConnection -> create(
127+
reactiveConnection,
128+
() -> new ReactiveStatelessSessionImpl( delegate, options, reactiveConnection )
129+
) )
130+
.map( s -> new MutinyStatelessSessionImpl( s, this ) );
128131
}
129132

130133
@Override
131134
public Uni<Mutiny.StatelessSession> openStatelessSession(String tenantId) {
132135
return uni( () -> connection( tenantId ) )
133-
.chain( reactiveConnection -> create( reactiveConnection,
134-
() -> new ReactiveStatelessSessionImpl( delegate, options( tenantId ), reactiveConnection ) ) )
136+
.chain( reactiveConnection -> create(
137+
reactiveConnection,
138+
() -> new ReactiveStatelessSessionImpl( delegate, options( tenantId ), reactiveConnection )
139+
) )
135140
.map( s -> new MutinyStatelessSessionImpl( s, this ) );
136141
}
137142

@@ -190,8 +195,8 @@ public <T> Uni<T> withSession(String tenantId, Function<Mutiny.Session, Uni<T>>
190195
Objects.requireNonNull( tenantId, "parameter 'tenantId' is required" );
191196
Objects.requireNonNull( work, "parameter 'work' is required" );
192197
Context.Key<Mutiny.Session> key = new MultitenantKey<>( contextKeyForSession, tenantId );
193-
Mutiny.Session current = context.get(key);
194-
if ( current!=null && current.isOpen() ) {
198+
Mutiny.Session current = context.get( key );
199+
if ( current != null && current.isOpen() ) {
195200
LOG.debugf( "Reusing existing open Mutiny.Session which was found in the current Vert.x context for current tenant '%s'", tenantId );
196201
return work.apply( current );
197202
}
@@ -227,11 +232,11 @@ public <T> Uni<T> withStatelessSession(String tenantId, Function<Mutiny.Stateles
227232
}
228233
else {
229234
LOG.debugf( "No existing open Mutiny.StatelessSession was found in the current Vert.x context for current tenant '%s': opening a new instance", tenantId );
230-
return withSession( openStatelessSession( tenantId), work, key );
235+
return withSession( openStatelessSession( tenantId ), work, key );
231236
}
232237
}
233238

234-
private<S extends Mutiny.Closeable, T> Uni<T> withSession(
239+
private <S extends Mutiny.Closeable, T> Uni<T> withSession(
235240
Uni<S> sessionUni,
236241
Function<S, Uni<T>> work,
237242
Context.Key<S> contextKey) {
@@ -246,25 +251,25 @@ private<S extends Mutiny.Closeable, T> Uni<T> withSession(
246251
@Override
247252
public <T> Uni<T> withTransaction(BiFunction<Mutiny.Session, Mutiny.Transaction, Uni<T>> work) {
248253
Objects.requireNonNull( work, "parameter 'work' is required" );
249-
return withSession( s -> s.withTransaction( t -> work.apply(s, t) ) );
254+
return withSession( s -> s.withTransaction( t -> work.apply( s, t ) ) );
250255
}
251256

252257
@Override
253258
public <T> Uni<T> withStatelessTransaction(BiFunction<Mutiny.StatelessSession, Mutiny.Transaction, Uni<T>> work) {
254259
Objects.requireNonNull( work, "parameter 'work' is required" );
255-
return withStatelessSession( s -> s.withTransaction( t -> work.apply(s, t) ) );
260+
return withStatelessSession( s -> s.withTransaction( t -> work.apply( s, t ) ) );
256261
}
257262

258263
@Override
259264
public <T> Uni<T> withTransaction(String tenantId, BiFunction<Mutiny.Session, Mutiny.Transaction, Uni<T>> work) {
260265
Objects.requireNonNull( work, "parameter 'work' is required" );
261-
return withSession( tenantId, s -> s.withTransaction( t -> work.apply(s, t) ) );
266+
return withSession( tenantId, s -> s.withTransaction( t -> work.apply( s, t ) ) );
262267
}
263268

264269
@Override
265270
public <T> Uni<T> withStatelessTransaction(String tenantId, BiFunction<Mutiny.StatelessSession, Mutiny.Transaction, Uni<T>> work) {
266271
Objects.requireNonNull( work, "parameter 'work' is required" );
267-
return withStatelessSession( tenantId, s -> s.withTransaction( t -> work.apply(s, t) ) );
272+
return withStatelessSession( tenantId, s -> s.withTransaction( t -> work.apply( s, t ) ) );
268273
}
269274

270275
@Override

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageSessionFactoryImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
*/
66
package org.hibernate.reactive.stage.impl;
77

8-
import jakarta.persistence.metamodel.Metamodel;
8+
import java.lang.invoke.MethodHandles;
9+
import java.util.Objects;
10+
import java.util.concurrent.CompletionStage;
11+
import java.util.function.BiFunction;
12+
import java.util.function.Function;
13+
import java.util.function.Supplier;
14+
915
import org.hibernate.Cache;
1016
import org.hibernate.engine.creation.internal.SessionBuilderImpl;
1117
import org.hibernate.engine.creation.internal.SessionCreationOptions;
@@ -27,12 +33,7 @@
2733
import org.hibernate.service.ServiceRegistry;
2834
import org.hibernate.stat.Statistics;
2935

30-
import java.lang.invoke.MethodHandles;
31-
import java.util.Objects;
32-
import java.util.concurrent.CompletionStage;
33-
import java.util.function.BiFunction;
34-
import java.util.function.Function;
35-
import java.util.function.Supplier;
36+
import jakarta.persistence.metamodel.Metamodel;
3637

3738
import static org.hibernate.reactive.util.impl.CompletionStages.completedFuture;
3839
import static org.hibernate.reactive.util.impl.CompletionStages.rethrow;

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/impl/StageSessionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public Stage.Session setSubselectFetchingEnabled(boolean enabled) {
391391

392392
@Override
393393
public <T> CompletionStage<T> withTransaction(Function<Stage.Transaction, CompletionStage<T>> work) {
394-
return currentTransaction==null ? new Transaction<T>().execute(work) : work.apply(currentTransaction);
394+
return currentTransaction == null ? new Transaction<T>().execute( work ) : work.apply( currentTransaction );
395395
}
396396

397397
private Transaction<?> currentTransaction;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public void start(Promise<Void> startPromise) {
212212
startPromise.fail( "Thread switch detected!" );
213213
}
214214
else {
215-
allResults.deliverResulst( generatedIds );
215+
allResults.deliverResults( generatedIds );
216216
startPromise.complete();
217217
}
218218
}
@@ -233,7 +233,7 @@ private static class ResultsCollector {
233233

234234
private final ConcurrentMap<String, List<Long>> resultsByThread = new ConcurrentHashMap<>();
235235

236-
public void deliverResulst(List<Long> generatedIds) {
236+
public void deliverResults(List<Long> generatedIds) {
237237
final String threadName = Thread.currentThread().getName();
238238
resultsByThread.put( threadName, generatedIds );
239239
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,7 @@ public class MultithreadedInsertionTest {
9898

9999
@BeforeAll
100100
public static void setupSessionFactory() {
101-
final VertxOptions vertxOptions = new VertxOptions();
102-
vertxOptions.setEventLoopPoolSize( N_THREADS );
103-
//We relax the blocked thread checks as we'll actually use latches to block them
104-
//intentionally for the purpose of the test; functionally this isn't required
105-
//but it's useful as self-test in the design of this, to ensure that the way
106-
//things are setup are indeed being run in multiple, separate threads.
107-
vertxOptions.setBlockedThreadCheckInterval( TIMEOUT_MINUTES );
108-
vertxOptions.setBlockedThreadCheckIntervalUnit( TimeUnit.MINUTES );
109-
vertx = Vertx.vertx( vertxOptions );
101+
vertx = Vertx.vertx( getVertxOptions() );
110102
Configuration configuration = new Configuration();
111103
setDefaultProperties( configuration );
112104
configuration.addAnnotatedClass( EntityWithGeneratedId.class );
@@ -121,6 +113,18 @@ public static void setupSessionFactory() {
121113
stageSessionFactory = sessionFactory.unwrap( Stage.SessionFactory.class );
122114
}
123115

116+
private static VertxOptions getVertxOptions() {
117+
final VertxOptions vertxOptions = new VertxOptions();
118+
vertxOptions.setEventLoopPoolSize( N_THREADS );
119+
//We relax the blocked thread checks as we'll actually use latches to block them
120+
//intentionally for the purpose of the test; functionally this isn't required,
121+
//but it's useful as self-test in the design of this, to ensure that the way
122+
//things are set up are indeed being run in multiple, separate threads.
123+
vertxOptions.setBlockedThreadCheckInterval( TIMEOUT_MINUTES );
124+
vertxOptions.setBlockedThreadCheckIntervalUnit( TimeUnit.MINUTES );
125+
return vertxOptions;
126+
}
127+
124128
@AfterAll
125129
public static void closeSessionFactory() {
126130
stageSessionFactory.close();
@@ -203,7 +207,7 @@ public void stop() {
203207
*/
204208
@Entity
205209
@Table(name="Entity")
206-
private static class EntityWithGeneratedId {
210+
public static class EntityWithGeneratedId {
207211
@Id
208212
@GeneratedValue
209213
Long id;

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

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323
import jakarta.persistence.Version;
2424

2525
import static java.util.concurrent.TimeUnit.MINUTES;
26+
import static org.assertj.core.api.Assertions.assertThat;
2627
import static org.hibernate.reactive.MyCurrentTenantIdentifierResolver.Tenant.DEFAULT;
2728
import static org.hibernate.reactive.MyCurrentTenantIdentifierResolver.Tenant.TENANT_1;
2829
import static org.hibernate.reactive.MyCurrentTenantIdentifierResolver.Tenant.TENANT_2;
2930
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
30-
import static org.junit.jupiter.api.Assertions.assertEquals;
31-
import static org.junit.jupiter.api.Assertions.assertFalse;
32-
import static org.junit.jupiter.api.Assertions.assertNotNull;
33-
import static org.junit.jupiter.api.Assertions.assertTrue;
3431

3532
/**
3633
* This class creates multiple additional databases so that we can check that queries run
@@ -46,10 +43,8 @@ public class ReactiveMultitenantTest extends BaseReactiveTest {
4643
protected Configuration constructConfiguration() {
4744
Configuration configuration = super.constructConfiguration();
4845
configuration.addAnnotatedClass( GuineaPig.class );
49-
configuration.setProperty(
50-
AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER,
51-
"anything"
52-
);//FIXME this is terrible?
46+
// FIXME this is terrible?
47+
configuration.setProperty( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, "anything" );
5348
configuration.getProperties().put( Settings.MULTI_TENANT_IDENTIFIER_RESOLVER, TENANT_RESOLVER );
5449
// Contains the SQL scripts for the creation of the additional databases
5550
configuration.setProperty( Settings.HBM2DDL_IMPORT_FILES, "/multitenancy-test.sql" );
@@ -61,27 +56,27 @@ protected Configuration constructConfiguration() {
6156
public void reactivePersistFindDelete(VertxTestContext context) {
6257
TENANT_RESOLVER.setTenantIdentifier( DEFAULT );
6358
final GuineaPig guineaPig = new GuineaPig( 5, "Aloi" );
64-
test(
65-
context,
66-
getSessionFactory().openSession()
67-
.thenCompose( session -> session.withTransaction( t -> session
68-
.persist( guineaPig )
69-
.thenCompose( v -> session.flush() )
70-
.thenAccept( v -> session.detach( guineaPig ) )
71-
.thenAccept( v -> assertFalse( session.contains( guineaPig ) ) )
72-
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
73-
.thenAccept( actualPig -> {
74-
assertThatPigsAreEqual( guineaPig, actualPig );
75-
assertTrue( session.contains( actualPig ) );
76-
assertFalse( session.contains( guineaPig ) );
77-
assertEquals( LockMode.READ, session.getLockMode( actualPig ) );
78-
session.detach( actualPig );
79-
assertFalse( session.contains( actualPig ) );
80-
} )
81-
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
82-
.thenCompose( session::remove )
83-
.thenCompose( v -> session.flush() ) )
84-
)
59+
test( context, openSession()
60+
.thenCompose( session -> session
61+
.persist( guineaPig )
62+
.thenCompose( v -> session.flush() )
63+
.thenAccept( v -> session.detach( guineaPig ) )
64+
.thenAccept( v -> assertThat( session.contains( guineaPig ) ).isFalse() )
65+
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
66+
.thenAccept( actualPig -> {
67+
assertThat( actualPig ).isNotNull();
68+
assertThat( actualPig.getId() ).isEqualTo( guineaPig.getId() );
69+
assertThat( actualPig.getName() ).isEqualTo( guineaPig.getName() );
70+
assertThat( session.contains( actualPig ) ).isTrue();
71+
assertThat( session.contains( guineaPig ) ).isFalse();
72+
assertThat( session.getLockMode( actualPig ) ).isEqualTo( LockMode.READ );
73+
session.detach( actualPig );
74+
assertThat( session.contains( actualPig ) ).isFalse();
75+
} )
76+
.thenCompose( v -> session.find( GuineaPig.class, guineaPig.getId() ) )
77+
.thenCompose( session::remove )
78+
.thenCompose( v -> session.flush() )
79+
)
8580
);
8681
}
8782

@@ -92,13 +87,13 @@ public void testTenantSelection(VertxTestContext context) {
9287
.thenCompose( session -> session
9388
.createNativeQuery( "select current_database()" )
9489
.getSingleResult()
95-
.thenAccept( result -> assertEquals( TENANT_1.getDbName(), result ) ) )
90+
.thenAccept( result -> assertThat( result ).isEqualTo( TENANT_1.getDbName() ) ) )
9691
.thenAccept( unused -> TENANT_RESOLVER.setTenantIdentifier( TENANT_2 ) )
9792
.thenCompose( unused -> openSession() )
9893
.thenCompose( session -> session
9994
.createNativeQuery( "select current_database()" )
10095
.getSingleResult()
101-
.thenAccept( result -> assertEquals( TENANT_2.getDbName(), result ) ) )
96+
.thenAccept( result -> assertThat( result ).isEqualTo( TENANT_2.getDbName() ) ) )
10297
);
10398
}
10499

@@ -109,41 +104,36 @@ public void testTenantSelectionStatelessSession(VertxTestContext context) {
109104
.thenCompose( t1Session -> t1Session
110105
.createNativeQuery( "select current_database()" )
111106
.getSingleResult()
112-
.thenAccept( result -> assertEquals( TENANT_1.getDbName(), result ) )
107+
.thenAccept( result -> assertThat( result ).isEqualTo( TENANT_1.getDbName() ) )
113108
.thenCompose( unused -> t1Session.close() ) )
114109
.thenAccept( unused -> TENANT_RESOLVER.setTenantIdentifier( TENANT_2 ) )
115110
.thenCompose( v -> getSessionFactory().openStatelessSession() )
116111
.thenCompose( t2Session -> t2Session
117112
.createNativeQuery( "select current_database()" )
118113
.getSingleResult()
119-
.thenAccept( result -> assertEquals( TENANT_2.getDbName(), result ) )
114+
.thenAccept( result -> assertThat( result ).isEqualTo( TENANT_2.getDbName() ) )
120115
.thenCompose( v -> t2Session.close() ) )
121116
);
122117
}
123118

124119
@Test
125120
public void testTenantSelectionStatelessSessionMutiny(VertxTestContext context) {
126121
TENANT_RESOLVER.setTenantIdentifier( TENANT_1 );
127-
test( context, getMutinySessionFactory().withStatelessSession( t1Session ->
128-
t1Session
129-
.createNativeQuery( "select current_database()" )
130-
.getSingleResult()
131-
.invoke( result -> assertEquals( TENANT_1.getDbName(), result ) )
132-
)
122+
test( context, getMutinySessionFactory()
123+
.withStatelessSession( t1Session -> t1Session
124+
.createNativeQuery( "select current_database()" )
125+
.getSingleResult()
126+
.invoke( result -> assertThat( result ).isEqualTo( TENANT_1.getDbName() ) )
127+
)
133128
.invoke( result -> TENANT_RESOLVER.setTenantIdentifier( TENANT_2 ) )
134129
.chain( () -> getMutinySessionFactory().withStatelessSession( t2Session -> t2Session
135130
.createNativeQuery( "select current_database()" )
136131
.getSingleResult()
137-
.invoke( result -> assertEquals( TENANT_2.getDbName(), result ) ) ) )
132+
.invoke( result -> assertThat( result ).isEqualTo( TENANT_2.getDbName() ) )
133+
) )
138134
);
139135
}
140136

141-
private void assertThatPigsAreEqual( GuineaPig expected, GuineaPig actual) {
142-
assertNotNull( actual );
143-
assertEquals( expected.getId(), actual.getId() );
144-
assertEquals( expected.getName(), actual.getName() );
145-
}
146-
147137
@Entity(name = "GuineaPig")
148138
@Table(name = "Pig")
149139
public static class GuineaPig {

0 commit comments

Comments
 (0)