Skip to content

Commit 0855351

Browse files
SanneDavideD
authored andcommitted
[hibernate#950] Return Uni or CompletionStage for all openSession methods
1 parent d896c83 commit 0855351

12 files changed

+214
-352
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java

+7-53
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.hibernate.CacheMode;
2424
import org.hibernate.Filter;
2525
import org.hibernate.FlushMode;
26+
import org.hibernate.HibernateException;
2627
import org.hibernate.Incubating;
2728
import org.hibernate.LockMode;
2829
import org.hibernate.collection.internal.AbstractPersistentCollection;
@@ -1515,62 +1516,15 @@ interface Transaction {
15151516
*/
15161517
interface SessionFactory extends AutoCloseable {
15171518

1518-
/**
1519-
* Obtain a new {@link Session reactive session}, the main
1520-
* interaction point between the user's program and Hibernate
1521-
* Reactive.
1522-
* <p>
1523-
* The underlying database connection is obtained lazily when
1524-
* the returned {@link Session} needs to access the database.
1525-
* <p>
1526-
* The client must explicitly close the session by calling
1527-
* {@link Session#close()}.
1528-
*
1529-
* @see #withSession(Function)
1530-
*/
1531-
Session openSession();
1519+
Uni<Session> openSession();
15321520

1533-
/**
1534-
* Obtain a new {@link Session reactive session} for a
1535-
* specified tenant.
1536-
* <p>
1537-
* The underlying database connection is obtained lazily when
1538-
* the returned {@link Session} needs to access the database.
1539-
* <p>
1540-
* The client must explicitly close the session by calling
1541-
* {@link Session#close()}.
1542-
*
1543-
* @param tenantId the id of the tenant
1544-
*
1545-
* @see #withSession(Function)
1546-
*/
1547-
Session openSession(String tenantId);
1521+
Uni<Mutiny.Session> openSession(String tenantId);
15481522

1549-
/**
1550-
* Obtain a {@link StatelessSession reactive stateless session}.
1551-
* <p>
1552-
* The underlying database connection is obtained lazily when
1553-
* the returned {@link StatelessSession} needs to access the
1554-
* database.
1555-
* <p>
1556-
* The client must explicitly close the session by calling
1557-
* {@link StatelessSession#close()}.
1558-
*/
1559-
StatelessSession openStatelessSession();
1523+
Session getCurrentContextualSession();
15601524

1561-
/**
1562-
* Obtain a {@link StatelessSession reactive stateless session}.
1563-
* <p>
1564-
* The underlying database connection is obtained lazily when
1565-
* the returned {@link StatelessSession} needs to access the
1566-
* database.
1567-
* <p>
1568-
* The client must explicitly close the session by calling
1569-
* {@link StatelessSession#close()}.
1570-
*
1571-
* @param tenantId the id of the tenant
1572-
*/
1573-
StatelessSession openStatelessSession(String tenantId);
1525+
Uni<StatelessSession> openStatelessSession() throws HibernateException;
1526+
1527+
Uni<StatelessSession> openStatelessSession(String tenantId);
15741528

15751529
/**
15761530
* Perform work using a {@link Session reactive session}.

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

+17-46
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,24 @@ public Context getContext() {
7676
return context;
7777
}
7878

79-
@Override
80-
public Mutiny.Session openSession() {
81-
SessionCreationOptions options = options();
82-
return new MutinySessionImpl(
83-
new ReactiveSessionImpl( delegate, options, proxyConnection( options.getTenantIdentifier() ) ),
84-
this
85-
);
79+
public Mutiny.Session getCurrentContextualSession() {
80+
Mutiny.Session current = context.get( contextKeyForSession );
81+
if ( current != null && current.isOpen() ) {
82+
return current;
83+
}
84+
return null;
8685
}
8786

8887
@Override
89-
public Mutiny.Session openSession(String tenantId) {
90-
Objects.requireNonNull( tenantId, "parameter 'tenantId' is required" );
91-
return new MutinySessionImpl(
92-
new ReactiveSessionImpl( delegate, options( tenantId ), proxyConnection( tenantId ) ),
93-
this
94-
);
95-
}
96-
97-
Uni<Mutiny.Session> newSession() throws HibernateException {
88+
public Uni<Mutiny.Session> openSession() throws HibernateException {
9889
SessionCreationOptions options = options();
9990
return uni( () -> connection( options.getTenantIdentifier() ) )
10091
.chain( reactiveConnection -> create( reactiveConnection, () -> new ReactiveSessionImpl( delegate, options, reactiveConnection ) ) )
10192
.map( s -> new MutinySessionImpl(s, this) );
10293
}
10394

104-
Uni<Mutiny.Session> newSession(String tenantId) throws HibernateException {
95+
@Override
96+
public Uni<Mutiny.Session> openSession(String tenantId) throws HibernateException {
10597
return uni( () -> connection( tenantId ) )
10698
.chain( reactiveConnection -> create( reactiveConnection, () -> new ReactiveSessionImpl( delegate, options( tenantId ), reactiveConnection ) ) )
10799
.map( s -> new MutinySessionImpl(s, this) );
@@ -116,29 +108,15 @@ private <S> Uni<S> create(ReactiveConnection connection, Supplier<S> supplier) {
116108
}
117109

118110
@Override
119-
public Mutiny.StatelessSession openStatelessSession() {
120-
SessionCreationOptions options = options();
121-
return new MutinyStatelessSessionImpl(
122-
new ReactiveStatelessSessionImpl( delegate, options, proxyConnection( options.getTenantIdentifier() ) ),
123-
this
124-
);
125-
}
126-
127-
public Mutiny.StatelessSession openStatelessSession(String tenantId) {
128-
return new MutinyStatelessSessionImpl(
129-
new ReactiveStatelessSessionImpl( delegate, options( tenantId ), proxyConnection( tenantId ) ),
130-
this
131-
);
132-
}
133-
134-
Uni<Mutiny.StatelessSession> newStatelessSession() throws HibernateException {
111+
public Uni<Mutiny.StatelessSession> openStatelessSession() throws HibernateException {
135112
SessionCreationOptions options = options();
136113
return uni( () -> connection( options.getTenantIdentifier() ) )
137114
.chain( reactiveConnection -> create( reactiveConnection, () -> new ReactiveStatelessSessionImpl( delegate, options, reactiveConnection ) ) )
138115
.map( s -> new MutinyStatelessSessionImpl(s, this) );
139116
}
140117

141-
Uni<Mutiny.StatelessSession> newStatelessSession(String tenantId) {
118+
@Override
119+
public Uni<Mutiny.StatelessSession> openStatelessSession(String tenantId) {
142120
return uni( () -> connection( tenantId ) )
143121
.chain( reactiveConnection -> create( reactiveConnection, () -> new ReactiveStatelessSessionImpl( delegate, options( tenantId ), reactiveConnection ) ) )
144122
.map( s -> new MutinyStatelessSessionImpl( s, this ) );
@@ -160,33 +138,26 @@ private CompletionStage<ReactiveConnection> connection(String tenantId) {
160138
: connectionPool.getConnection( tenantId );
161139
}
162140

163-
private ReactiveConnection proxyConnection(String tenantId) {
164-
assertUseOnEventLoop();
165-
return tenantId==null
166-
? connectionPool.getProxyConnection()
167-
: connectionPool.getProxyConnection( tenantId );
168-
}
169-
170141
@Override
171142
public <T> Uni<T> withSession(Function<Mutiny.Session, Uni<T>> work) {
172143
Objects.requireNonNull( work, "parameter 'work' is required" );
173144
Mutiny.Session current = context.get( contextKeyForSession );
174145
if ( current != null && current.isOpen() ) {
175146
return work.apply( current );
176147
}
177-
return withSession( newSession(), work, contextKeyForSession );
148+
return withSession( openSession(), work, contextKeyForSession );
178149
}
179150

180151
@Override
181152
public <T> Uni<T> withSession(String tenantId, Function<Mutiny.Session, Uni<T>> work) {
182153
Objects.requireNonNull( tenantId, "parameter 'tenantId' is required" );
183154
Objects.requireNonNull( work, "parameter 'work' is required" );
184-
Context.Key<Mutiny.Session> key = new MultitenantKey( contextKeyForSession, tenantId );
155+
Context.Key<Mutiny.Session> key = new MultitenantKey<>( contextKeyForSession, tenantId );
185156
Mutiny.Session current = context.get(key);
186157
if ( current!=null && current.isOpen() ) {
187158
return work.apply( current );
188159
}
189-
return withSession( newSession( tenantId ), work, key );
160+
return withSession( openSession( tenantId ), work, key );
190161
}
191162

192163
@Override
@@ -196,7 +167,7 @@ public <T> Uni<T> withStatelessSession(Function<Mutiny.StatelessSession, Uni<T>>
196167
if ( current != null && current.isOpen() ) {
197168
return work.apply( current );
198169
}
199-
return withSession( newStatelessSession(), work, contextKeyForStatelessSession );
170+
return withSession( openStatelessSession(), work, contextKeyForStatelessSession );
200171
}
201172

202173
@Override
@@ -208,7 +179,7 @@ public <T> Uni<T> withStatelessSession(String tenantId, Function<Mutiny.Stateles
208179
if ( current != null && current.isOpen() ) {
209180
return work.apply( current );
210181
}
211-
return withSession( newStatelessSession(tenantId), work, key );
182+
return withSession( openStatelessSession( tenantId), work, key );
212183
}
213184

214185
private<S extends Mutiny.Closeable, T> Uni<T> withSession(

hibernate-reactive-core/src/main/java/org/hibernate/reactive/stage/Stage.java

+4-53
Original file line numberDiff line numberDiff line change
@@ -1516,62 +1516,13 @@ interface Transaction {
15161516
*/
15171517
interface SessionFactory extends AutoCloseable {
15181518

1519-
/**
1520-
* Obtain a new {@link Session reactive session}, the main
1521-
* interaction point between the user's program and Hibernate
1522-
* Reactive.
1523-
* <p>
1524-
* The underlying database connection is obtained lazily when
1525-
* the returned {@link Session} needs to access the database.
1526-
* <p>
1527-
* The client must explicitly close the session by calling
1528-
* {@link Session#close()}.
1529-
*
1530-
* @see #withSession(Function)
1531-
*/
1532-
Session openSession();
1519+
CompletionStage<Session> openSession();
15331520

1534-
/**
1535-
* Obtain a new {@link Session reactive session} for a
1536-
* specified tenant.
1537-
* <p>
1538-
* The underlying database connection is obtained lazily when
1539-
* the returned {@link Session} needs to access the database.
1540-
* <p>
1541-
* The client must explicitly close the session by calling
1542-
* {@link Session#close()}.
1543-
*
1544-
* @param tenantId the id of the tenant
1545-
*
1546-
* @see #withSession(Function)
1547-
*/
1548-
Session openSession(String tenantId);
1521+
CompletionStage<Session> openSession(String tenantId);
15491522

1550-
/**
1551-
* Obtain a {@link StatelessSession reactive stateless session}.
1552-
* <p>
1553-
* The underlying database connection is obtained lazily when
1554-
* the returned {@link StatelessSession} needs to access the
1555-
* database.
1556-
* <p>
1557-
* The client must explicitly close the session by calling
1558-
* {@link StatelessSession#close()}.
1559-
*/
1560-
StatelessSession openStatelessSession();
1523+
CompletionStage<StatelessSession> openStatelessSession();
15611524

1562-
/**
1563-
* Obtain a {@link StatelessSession reactive stateless session}.
1564-
* <p>
1565-
* The underlying database connection is obtained lazily when
1566-
* the returned {@link StatelessSession} needs to access the
1567-
* database.
1568-
* <p>
1569-
* The client must explicitly close the session by calling
1570-
* {@link StatelessSession#close()}.
1571-
*
1572-
* @param tenantId the id of the tenant
1573-
*/
1574-
StatelessSession openStatelessSession(String tenantId);
1525+
CompletionStage<StatelessSession> openStatelessSession(String tenantId);
15751526

15761527
/**
15771528
* Perform work using a {@link Session reactive session}.

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

+11-46
Original file line numberDiff line numberDiff line change
@@ -68,59 +68,30 @@ public Context getContext() {
6868
}
6969

7070
@Override
71-
public Stage.Session openSession() {
72-
SessionCreationOptions options = options();
73-
return new StageSessionImpl(
74-
new ReactiveSessionImpl( delegate, options, proxyConnection( options.getTenantIdentifier() ) ),
75-
this
76-
);
77-
}
78-
79-
@Override
80-
public Stage.Session openSession(String tenantId) {
81-
return new StageSessionImpl(
82-
new ReactiveSessionImpl( delegate, options( tenantId ), proxyConnection( tenantId ) ),
83-
this
84-
);
85-
}
86-
87-
public Stage.StatelessSession openStatelessSession() {
88-
SessionCreationOptions options = options();
89-
return new StageStatelessSessionImpl(
90-
new ReactiveStatelessSessionImpl( delegate, options, proxyConnection( options.getTenantIdentifier() ) ),
91-
this
92-
);
93-
}
94-
95-
@Override
96-
public Stage.StatelessSession openStatelessSession(String tenantId) {
97-
return new StageStatelessSessionImpl(
98-
new ReactiveStatelessSessionImpl( delegate, options( tenantId ), proxyConnection( tenantId ) ),
99-
this
100-
);
101-
}
102-
103-
CompletionStage<Stage.Session> newSession() {
71+
public CompletionStage<Stage.Session> openSession() {
10472
SessionCreationOptions options = options();
10573
return stage( v -> connection( options.getTenantIdentifier() )
10674
.thenCompose( connection -> create( connection, () -> new ReactiveSessionImpl( delegate, options, connection ) ) )
10775
.thenApply( s -> new StageSessionImpl(s, this) ) );
10876
}
10977

110-
CompletionStage<Stage.Session> newSession(String tenantId) {
78+
@Override
79+
public CompletionStage<Stage.Session> openSession(String tenantId) {
11180
return stage( v -> connection( tenantId )
11281
.thenCompose( connection -> create( connection, () -> new ReactiveSessionImpl( delegate, options( tenantId ), connection ) ) )
11382
.thenApply( s -> new StageSessionImpl(s, this) ) );
11483
}
11584

116-
CompletionStage<Stage.StatelessSession> newStatelessSession() {
85+
@Override
86+
public CompletionStage<Stage.StatelessSession> openStatelessSession() {
11787
SessionCreationOptions options = options();
11888
return stage( v -> connection( options.getTenantIdentifier() )
11989
.thenCompose( connection -> create( connection, () -> new ReactiveStatelessSessionImpl( delegate, options, connection ) ) )
12090
.thenApply( s -> new StageStatelessSessionImpl(s, this) ) );
12191
}
12292

123-
CompletionStage<Stage.StatelessSession> newStatelessSession(String tenantId) {
93+
@Override
94+
public CompletionStage<Stage.StatelessSession> openStatelessSession(String tenantId) {
12495
return stage( v -> connection( tenantId )
12596
.thenCompose( connection -> create( connection, () -> new ReactiveStatelessSessionImpl( delegate, options( tenantId ), connection ) ) )
12697
.thenApply( s -> new StageStatelessSessionImpl( s, this ) ) );
@@ -157,20 +128,14 @@ private CompletionStage<ReactiveConnection> connection(String tenantId) {
157128
: connectionPool.getConnection( tenantId );
158129
}
159130

160-
private ReactiveConnection proxyConnection(String tenantId) {
161-
return tenantId==null
162-
? connectionPool.getProxyConnection()
163-
: connectionPool.getProxyConnection( tenantId );
164-
}
165-
166131
@Override
167132
public <T> CompletionStage<T> withSession(Function<Stage.Session, CompletionStage<T>> work) {
168133
Objects.requireNonNull( work, "parameter 'work' is required" );
169134
Stage.Session current = context.get( contextKeyForSession );
170135
if ( current!=null && current.isOpen() ) {
171136
return work.apply( current );
172137
}
173-
return withSession( newSession(), work, contextKeyForSession );
138+
return withSession( openSession(), work, contextKeyForSession );
174139
}
175140

176141
@Override
@@ -182,7 +147,7 @@ public <T> CompletionStage<T> withSession(String tenantId, Function<Stage.Sessio
182147
if ( current!=null && current.isOpen() ) {
183148
return work.apply( current );
184149
}
185-
return withSession( newSession( tenantId ), work, key );
150+
return withSession( openSession( tenantId ), work, key );
186151
}
187152

188153
@Override
@@ -192,7 +157,7 @@ public <T> CompletionStage<T> withStatelessSession(Function<Stage.StatelessSessi
192157
if ( current!=null && current.isOpen() ) {
193158
return work.apply( current );
194159
}
195-
return withSession( newStatelessSession(), work, contextKeyForStatelessSession );
160+
return withSession( openStatelessSession(), work, contextKeyForStatelessSession );
196161
}
197162

198163
@Override
@@ -204,7 +169,7 @@ public <T> CompletionStage<T> withStatelessSession(String tenantId, Function<Sta
204169
if ( current != null && current.isOpen() ) {
205170
return work.apply( current );
206171
}
207-
return withSession( newStatelessSession(tenantId), work, key );
172+
return withSession( openStatelessSession( tenantId), work, key );
208173
}
209174

210175
private <S extends Stage.Closeable, T> CompletionStage<T> withSession(

0 commit comments

Comments
 (0)