Skip to content

Commit d47cda8

Browse files
committed
[#2518] Add createSession and createStatelessSession
In Mutiny and Stage classes
1 parent 066917f commit d47cda8

File tree

6 files changed

+381
-11
lines changed

6 files changed

+381
-11
lines changed

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

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
package org.hibernate.reactive.mutiny;
77

8+
import java.lang.invoke.MethodHandles;
9+
import java.util.List;
10+
import java.util.function.BiFunction;
11+
import java.util.function.Function;
12+
813
import org.hibernate.Cache;
914
import org.hibernate.CacheMode;
1015
import org.hibernate.Filter;
@@ -44,10 +49,6 @@
4449
import jakarta.persistence.criteria.CriteriaUpdate;
4550
import jakarta.persistence.metamodel.Attribute;
4651
import jakarta.persistence.metamodel.Metamodel;
47-
import java.lang.invoke.MethodHandles;
48-
import java.util.List;
49-
import java.util.function.BiFunction;
50-
import java.util.function.Function;
5152

5253
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
5354
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
@@ -1971,6 +1972,59 @@ interface Transaction {
19711972
*/
19721973
interface SessionFactory extends AutoCloseable {
19731974

1975+
/**
1976+
* Obtain a new {@link Session reactive session}, the main
1977+
* interaction point between the user's program and Hibernate
1978+
* Reactive.
1979+
* <p>
1980+
* The underlying database connection is obtained lazily
1981+
* when the returned {@link Session} needs to access the
1982+
* database.
1983+
* <p>
1984+
* The client must close the session using {@link Session#close()}.
1985+
*/
1986+
@Incubating
1987+
Session createSession();
1988+
1989+
/**
1990+
* Obtain a new {@link Session reactive session}.
1991+
* <p>
1992+
* The underlying database connection is obtained lazily
1993+
* when the returned {@link Session} needs to access the
1994+
* database.
1995+
* <p>
1996+
* The client must close the session using {@link Session#close()}.
1997+
* @param tenantId the id of the tenant
1998+
*/
1999+
@Incubating
2000+
Session createSession(String tenantId);
2001+
2002+
/**
2003+
* Obtain a new {@link StatelessSession reactive stateless session}.
2004+
* <p>
2005+
* The underlying database connection is obtained lazily
2006+
* when the returned {@link StatelessSession} needs to access the
2007+
* database.
2008+
* <p>
2009+
* The client must close the session using {@link Session#close()}.
2010+
*/
2011+
@Incubating
2012+
StatelessSession createStatelessSession();
2013+
2014+
/**
2015+
* Obtain a new {@link StatelessSession reactive stateless session}.
2016+
* <p>
2017+
* The underlying database connection is obtained lazily
2018+
* when the returned {@link StatelessSession} needs to access the
2019+
* database.
2020+
* <p>
2021+
* The client must close the session using {@link Session#close()}.
2022+
*
2023+
* @param tenantId the id of the tenant
2024+
*/
2025+
@Incubating
2026+
StatelessSession createStatelessSession(String tenantId);
2027+
19742028
/**
19752029
* Obtain a new {@link Session reactive session} {@link Uni}, the main
19762030
* interaction point between the user's program and Hibernate

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.hibernate.reactive.mutiny.Mutiny;
2929
import org.hibernate.reactive.pool.ReactiveConnection;
3030
import org.hibernate.reactive.pool.ReactiveConnectionPool;
31+
import org.hibernate.reactive.session.ReactiveSession;
32+
import org.hibernate.reactive.session.ReactiveStatelessSession;
3133
import org.hibernate.reactive.session.impl.ReactiveSessionImpl;
3234
import org.hibernate.reactive.session.impl.ReactiveStatelessSessionImpl;
3335
import org.hibernate.service.ServiceRegistry;
@@ -88,6 +90,32 @@ public Context getContext() {
8890
return context;
8991
}
9092

93+
@Override
94+
public Mutiny.Session createSession() {
95+
return createSession( getTenantIdentifier( options() ) );
96+
}
97+
98+
@Override
99+
public Mutiny.Session createSession(String tenantId) {
100+
final SessionCreationOptions options = options();
101+
ReactiveConnectionPool pool = delegate.getServiceRegistry().getService( ReactiveConnectionPool.class );
102+
ReactiveSession sessionImpl = new ReactiveSessionImpl( delegate, options, pool.getProxyConnection( tenantId ) );
103+
return new MutinySessionImpl( sessionImpl, this );
104+
}
105+
106+
@Override
107+
public Mutiny.StatelessSession createStatelessSession() {
108+
return createStatelessSession( getTenantIdentifier( options() ) );
109+
}
110+
111+
@Override
112+
public Mutiny.StatelessSession createStatelessSession(String tenantId) {
113+
final SessionCreationOptions options = options();
114+
ReactiveConnectionPool pool = delegate.getServiceRegistry().getService( ReactiveConnectionPool.class );
115+
ReactiveStatelessSession sessionImpl = new ReactiveStatelessSessionImpl( delegate, options, pool.getProxyConnection( tenantId ) );
116+
return new MutinyStatelessSessionImpl( sessionImpl, this );
117+
}
118+
91119
@Override
92120
public Uni<Mutiny.Session> openSession() {
93121
SessionCreationOptions options = options();

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/ReactiveConnectionPool.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,25 @@
3737
*/
3838
@Incubating
3939
public interface ReactiveConnectionPool extends Service {
40+
/**
41+
* Obtain a lazily-initializing reactive connection. The
42+
* actual connection might be made when the returned
43+
* instance if {@link ReactiveConnection} is first used.
44+
*/
45+
ReactiveConnection getProxyConnection();
4046

4147
/**
4248
* Obtain a reactive connection, returning the connection
43-
* via a {@link CompletionStage}.
49+
* via a {@link CompletionStage} and overriding the default
50+
* {@link SqlExceptionHelper} for the pool.
4451
*/
45-
CompletionStage<ReactiveConnection> getConnection();
52+
ReactiveConnection getProxyConnection(SqlExceptionHelper sqlExceptionHelper);
53+
54+
/**
55+
* Obtain a reactive connection for the given tenant id,
56+
* returning the connection via a {@link CompletionStage}.
57+
*/
58+
ReactiveConnection getProxyConnection(String tenantId);
4659

4760
/**
4861
* Obtain a reactive connection, returning the connection
@@ -57,6 +70,12 @@ public interface ReactiveConnectionPool extends Service {
5770
*/
5871
CompletionStage<ReactiveConnection> getConnection(String tenantId);
5972

73+
/**
74+
* Obtain a reactive connection, returning the connection
75+
* via a {@link CompletionStage}.
76+
*/
77+
CompletionStage<ReactiveConnection> getConnection();
78+
6079
/**
6180
* Obtain a reactive connection for the given tenant id,
6281
* returning the connection via a {@link CompletionStage}

0 commit comments

Comments
 (0)