Skip to content

Commit 6b98842

Browse files
committed
HHH-19774 - Automatic flushing for child session with shared connection/tx
HHH-19808 - Automatic closing for child session with shared connection/tx
1 parent 5a8a3a9 commit 6b98842

File tree

8 files changed

+100
-107
lines changed

8 files changed

+100
-107
lines changed

hibernate-core/src/main/java/org/hibernate/engine/creation/CommonSharedBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public interface CommonSharedBuilder extends CommonBuilder {
3030

3131
/**
3232
* Signifies that the connection from the original session should be used to create the new session.
33+
* Implies that the overall "transaction context" should be shared as well.
3334
*
3435
* @return {@code this}, for method chaining
3536
*/

hibernate-core/src/main/java/org/hibernate/engine/creation/internal/ParentSessionCallbacks.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.engine.creation.internal;
6+
7+
import org.hibernate.engine.creation.CommonSharedBuilder;
8+
9+
/**
10+
* Allows observation of flush and closure events of a parent session from a
11+
* child session which shares connection/transaction with the parent.
12+
*
13+
* @see CommonSharedBuilder#connection()
14+
*
15+
* @author Steve Ebersole
16+
*/
17+
public interface ParentSessionObserver {
18+
/**
19+
* Callback when the parent is flushed. Used to flush the child session.
20+
*/
21+
void onParentFlush();
22+
23+
/**
24+
* Callback when the parent is closed. Used to close the child session.
25+
*
26+
* @apiNote Observation of closure of the parent is different from {@link org.hibernate.SessionBuilder#autoClose}
27+
* which indicates whether the session ought to be closed in response to transaction completion.
28+
*/
29+
void onParentClose();
30+
}

hibernate-core/src/main/java/org/hibernate/engine/creation/internal/SessionCreationOptionsAdaptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public TransactionCompletionCallbacksImplementor getTransactionCompletionCallbac
147147
}
148148

149149
@Override
150-
public void registerParentSessionCallbacks(ParentSessionCallbacks callbacks) {
150+
public void registerParentSessionObserver(ParentSessionObserver callbacks) {
151151
originalSession.getEventListenerManager().addListener( new SessionEventListener() {
152152
@Override
153153
public void flushStart() {

hibernate-core/src/main/java/org/hibernate/engine/creation/internal/SharedSessionBuilderImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public SessionImplementor openSession() {
100100

101101
@Override
102102
@Deprecated(forRemoval = true)
103+
@SuppressWarnings("removal")
103104
public SharedSessionBuilderImplementor tenantIdentifier(String tenantIdentifier) {
104105
tenantIdentifier( (Object) tenantIdentifier );
105106
return this;
@@ -261,10 +262,10 @@ public SharedSessionBuilderImplementor subselectFetchEnabled(boolean subselectFe
261262

262263

263264
@Override
264-
public void registerParentSessionCallbacks(ParentSessionCallbacks callbacks) {
265+
public void registerParentSessionObserver(ParentSessionObserver callbacks) {
265266
original.getEventListenerManager().addListener( new SessionEventListener() {
266267
@Override
267-
public void flushStart() {
268+
public void flushEnd(int numberOfEntities, int numberOfCollections) {
268269
callbacks.onParentFlush();
269270
}
270271

hibernate-core/src/main/java/org/hibernate/engine/creation/internal/SharedSessionCreationOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public interface SharedSessionCreationOptions extends SessionCreationOptions {
3030
/**
3131
* Registers callbacks for the child session to integrate with events of the parent session.
3232
*/
33-
void registerParentSessionCallbacks(ParentSessionCallbacks callbacks);
33+
void registerParentSessionObserver(ParentSessionObserver callbacks);
3434

3535
}

hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.hibernate.bytecode.enhance.spi.interceptor.SessionAssociationMarkers;
2828
import org.hibernate.cache.spi.CacheTransactionSynchronization;
2929
import org.hibernate.dialect.Dialect;
30-
import org.hibernate.engine.creation.internal.ParentSessionCallbacks;
30+
import org.hibernate.engine.creation.internal.ParentSessionObserver;
3131
import org.hibernate.engine.creation.internal.SessionCreationOptions;
3232
import org.hibernate.engine.creation.internal.SessionCreationOptionsAdaptor;
3333
import org.hibernate.engine.creation.internal.SharedSessionBuilderImpl;
@@ -225,15 +225,18 @@ public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreation
225225
jdbcSessionContext = createJdbcSessionContext( statementInspector );
226226
logInconsistentOptions( sharedOptions );
227227
addSharedSessionTransactionObserver( transactionCoordinator );
228-
sharedOptions.registerParentSessionCallbacks( new ParentSessionCallbacks() {
228+
sharedOptions.registerParentSessionObserver( new ParentSessionObserver() {
229229
@Override
230230
public void onParentFlush() {
231231
propagateFlush();
232232
}
233233

234234
@Override
235235
public void onParentClose() {
236-
propagateClose();
236+
// unless explicitly disabled, propagate the closure
237+
if ( sharedOptions.shouldAutoClose() ) {
238+
propagateClose();
239+
}
237240
}
238241
} );
239242
}
@@ -505,13 +508,6 @@ public boolean isClosed() {
505508

506509
@Override
507510
public void close() {
508-
if ( isTransactionCoordinatorShared ) {
509-
// Perform an auto-flush -
510-
// This deals with the natural usage pattern of a child Session
511-
// used with a try-with-resource block
512-
propagateFlush();
513-
}
514-
515511
if ( !closed || waitingForAutoClose ) {
516512
try {
517513
delayedAfterCompletion();
@@ -915,7 +911,7 @@ public void setNativeJdbcParametersIgnored(boolean nativeJdbcParametersIgnored)
915911
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
916912
// dynamic HQL handling
917913

918-
@Override @SuppressWarnings("rawtypes")
914+
@Override @Deprecated @SuppressWarnings({"rawtypes", "deprecation"})
919915
public QueryImplementor createQuery(String queryString) {
920916
return createQuery( queryString, null );
921917
}
@@ -1017,10 +1013,12 @@ public <R> QueryImplementor<R> createQuery(TypedQueryReference<R> typedQueryRefe
10171013
checksBeforeQueryCreation();
10181014
if ( typedQueryReference instanceof SelectionSpecificationImpl<R> specification ) {
10191015
final var query = specification.buildCriteria( getCriteriaBuilder() );
1016+
//noinspection unchecked
10201017
return new SqmQueryImpl<>( (SqmStatement<R>) query, specification.getResultType(), this );
10211018
}
10221019
else if ( typedQueryReference instanceof MutationSpecificationImpl<?> specification ) {
10231020
final var query = specification.buildCriteria( getCriteriaBuilder() );
1021+
//noinspection unchecked
10241022
return new SqmQueryImpl<>( (SqmStatement<R>) query, (Class<R>) specification.getResultType(), this );
10251023
}
10261024
else {
@@ -1039,12 +1037,12 @@ else if ( typedQueryReference instanceof MutationSpecificationImpl<?> specificat
10391037
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10401038
// dynamic native (SQL) query handling
10411039

1042-
@Override
1040+
@Override @Deprecated @SuppressWarnings("deprecation")
10431041
public NativeQueryImplementor<?> createNativeQuery(String sqlString) {
10441042
return createNativeQuery( sqlString, (Class<?>) null );
10451043
}
10461044

1047-
@Override
1045+
@Override @Deprecated @SuppressWarnings("deprecation")
10481046
public NativeQueryImplementor<?> createNativeQuery(String sqlString, String resultSetMappingName) {
10491047
checksBeforeQueryCreation();
10501048
return buildNativeQuery( sqlString, resultSetMappingName, null );
@@ -1146,7 +1144,7 @@ public QueryImplementor<?> getNamedQuery(String queryName) {
11461144
return createNamedQuery( queryName );
11471145
}
11481146

1149-
@Override
1147+
@Override @Deprecated @SuppressWarnings("deprecation")
11501148
public QueryImplementor<?> createNamedQuery(String name) {
11511149
checksBeforeQueryCreation();
11521150
try {
@@ -1253,12 +1251,14 @@ protected <T,Q extends CommonQueryContract> Q buildNamedQuery(
12531251
// first see if it is a named HQL query
12541252
final var namedSqmQueryMemento = getSqmQueryMemento( queryName );
12551253
if ( namedSqmQueryMemento != null ) {
1254+
//noinspection unchecked
12561255
return sqmCreator.apply( (NamedSqmQueryMemento<T>) namedSqmQueryMemento );
12571256
}
12581257

12591258
// otherwise, see if it is a named native query
12601259
final var namedNativeDescriptor = getNativeQueryMemento( queryName );
12611260
if ( namedNativeDescriptor != null ) {
1261+
//noinspection unchecked
12621262
return nativeCreator.apply( (NamedNativeQueryMemento<T>) namedNativeDescriptor );
12631263
}
12641264

@@ -1322,7 +1322,7 @@ protected <T> SqmQueryImplementor<T> createSqmQueryImplementor(Class<T> resultTy
13221322
return query;
13231323
}
13241324

1325-
@Override
1325+
@Override @Deprecated @SuppressWarnings("deprecation")
13261326
public NativeQueryImplementor<?> getNamedNativeQuery(String queryName) {
13271327
final var namedNativeDescriptor = getNativeQueryMemento( queryName );
13281328
if ( namedNativeDescriptor != null ) {
@@ -1333,7 +1333,7 @@ public NativeQueryImplementor<?> getNamedNativeQuery(String queryName) {
13331333
}
13341334
}
13351335

1336-
@Override
1336+
@Override @Deprecated @SuppressWarnings("deprecation")
13371337
public NativeQueryImplementor<?> getNamedNativeQuery(String queryName, String resultSetMapping) {
13381338
final var namedNativeDescriptor = getNativeQueryMemento( queryName );
13391339
if ( namedNativeDescriptor != null ) {
@@ -1590,7 +1590,7 @@ public <T> QueryImplementor<T> createQuery(CriteriaQuery<T> criteriaQuery) {
15901590
}
15911591
}
15921592

1593-
@Override
1593+
@Override @Deprecated @SuppressWarnings("deprecation")
15941594
public QueryImplementor<?> createQuery(@SuppressWarnings("rawtypes") CriteriaUpdate criteriaUpdate) {
15951595
checkOpen();
15961596
try {
@@ -1602,7 +1602,7 @@ public QueryImplementor<?> createQuery(@SuppressWarnings("rawtypes") CriteriaUpd
16021602
}
16031603
}
16041604

1605-
@Override
1605+
@Override @Deprecated @SuppressWarnings("deprecation")
16061606
public QueryImplementor<?> createQuery(@SuppressWarnings("rawtypes") CriteriaDelete criteriaDelete) {
16071607
checkOpen();
16081608
try {

0 commit comments

Comments
 (0)