Skip to content

Commit a5db652

Browse files
committed
HHH-19772 add noSessionInterceptorCreation()
1 parent ca6d1e3 commit a5db652

10 files changed

+80
-12
lines changed

hibernate-core/src/main/java/org/hibernate/SessionBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public interface SessionBuilder extends CommonBuilder {
3333
@Override
3434
SessionBuilder noInterceptor();
3535

36+
@Override
37+
SessionBuilder noSessionInterceptorCreation();
38+
39+
@Override
40+
SessionBuilder noStatementInspector();
41+
3642
@Override
3743
SessionBuilder statementInspector(UnaryOperator<String> operator);
3844

hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ public interface SharedSessionBuilder extends SessionBuilder, CommonSharedBuilde
116116
@Override
117117
SharedSessionBuilder noInterceptor();
118118

119+
@Override
120+
SharedSessionBuilder noSessionInterceptorCreation();
121+
119122
@Override
120123
SharedSessionBuilder connection(Connection connection);
121124

hibernate-core/src/main/java/org/hibernate/SharedStatelessSessionBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public interface SharedStatelessSessionBuilder extends StatelessSessionBuilder,
4141
@Override
4242
SharedStatelessSessionBuilder noInterceptor();
4343

44+
@Override
45+
SharedStatelessSessionBuilder noSessionInterceptorCreation();
46+
4447
SharedStatelessSessionBuilder statementInspector(UnaryOperator<String> operator);
4548

4649
@Override

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ public interface CommonBuilder {
6060
CommonBuilder interceptor(Interceptor interceptor);
6161

6262
/**
63-
* Signifies that no {@link Interceptor} should be used.
63+
* Specifies that no {@link Interceptor} should be used.
6464
* <p>
65-
* By default, if no {@code Interceptor} is explicitly specified, the
65+
* By default, if no {@code Interceptor} is explicitly
66+
* {@linkplain #interceptor(Interceptor) specified}, the
6667
* {@code Interceptor} associated with the {@link SessionFactory} is
67-
* inherited by the new session.
68+
* inherited by the new session. Or, if there is no interceptor
69+
* associated with the {@link SessionFactory}, but a session-scoped
70+
* interceptor has been configured, a new session-scoped
71+
* {@code Interceptor} will be created for the new session.
6872
* <p>
6973
* Calling {@link #interceptor(Interceptor) interceptor(null)} has the
7074
* same effect.
@@ -73,6 +77,28 @@ public interface CommonBuilder {
7377
*/
7478
CommonBuilder noInterceptor();
7579

80+
/**
81+
* Specifies that no
82+
* {@linkplain org.hibernate.cfg.SessionEventSettings#SESSION_SCOPED_INTERCEPTOR
83+
* session-scoped interceptor} should be instantiated for the new session.
84+
* <p>
85+
* By default, if no {@link Interceptor} is explicitly
86+
* {@linkplain #interceptor(Interceptor) specified}, and if there
87+
* is no interceptor associated with the {@link SessionFactory}, but
88+
* a session-scoped interceptor has been configured, a new session-scoped
89+
* {@code Interceptor} will be created for the new session.
90+
* <p>
91+
* Note that this operation does not disable use of an interceptor
92+
* associated with the {@link SessionFactory}.
93+
*
94+
* @return {@code this}, for method chaining
95+
*
96+
* @see org.hibernate.cfg.SessionEventSettings#SESSION_SCOPED_INTERCEPTOR
97+
*
98+
* @since 7.2
99+
*/
100+
CommonBuilder noSessionInterceptorCreation();
101+
76102
/**
77103
* Applies the given statement inspection function to the session.
78104
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public interface CommonSharedBuilder extends CommonBuilder {
6161
@Override
6262
CommonSharedBuilder noInterceptor();
6363

64+
@Override
65+
CommonSharedBuilder noSessionInterceptorCreation();
66+
6467
@Override
6568
CommonSharedBuilder statementInspector(UnaryOperator<String> operator);
6669

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public abstract class AbstractCommonBuilder<T extends CommonBuilder> implements
2828

2929
protected StatementInspector statementInspector;
3030
protected Interceptor interceptor;
31-
protected boolean explicitNoInterceptor;
31+
protected boolean allowInterceptor = true;
32+
protected boolean allowSessionInterceptorCreation = true;
3233
protected Connection connection;
3334
protected PhysicalConnectionHandlingMode connectionHandlingMode;
3435
protected Object tenantIdentifier;
@@ -48,7 +49,7 @@ public AbstractCommonBuilder(SessionFactoryImplementor factory) {
4849

4950
Interceptor configuredInterceptor() {
5051
// If we were explicitly asked for no interceptor, always return null.
51-
if ( explicitNoInterceptor ) {
52+
if ( !allowInterceptor ) {
5253
return null;
5354
}
5455

@@ -69,11 +70,13 @@ Interceptor configuredInterceptor() {
6970
return optionsInterceptor;
7071
}
7172

72-
// then check the Session-scoped interceptor prototype
73-
final var statelessInterceptorImplementorSupplier =
74-
options.getStatelessInterceptorImplementorSupplier();
75-
if ( statelessInterceptorImplementorSupplier != null ) {
76-
return statelessInterceptorImplementorSupplier.get();
73+
if ( allowSessionInterceptorCreation ) {
74+
// then check the Session-scoped interceptor prototype
75+
final var statelessInterceptorImplementorSupplier =
76+
options.getStatelessInterceptorImplementorSupplier();
77+
if ( statelessInterceptorImplementorSupplier != null ) {
78+
return statelessInterceptorImplementorSupplier.get();
79+
}
7780
}
7881

7982
return null;
@@ -100,15 +103,21 @@ public T interceptor(Interceptor interceptor) {
100103
}
101104
else {
102105
this.interceptor = interceptor;
103-
this.explicitNoInterceptor = false;
106+
this.allowInterceptor = true;
104107
}
105108
return getThis();
106109
}
107110

108111
@Override
109112
public T noInterceptor() {
110113
this.interceptor = null;
111-
this.explicitNoInterceptor = true;
114+
this.allowInterceptor = false;
115+
return getThis();
116+
}
117+
118+
@Override
119+
public T noSessionInterceptorCreation() {
120+
this.allowSessionInterceptorCreation = false;
112121
return getThis();
113122
}
114123

hibernate-core/src/main/java/org/hibernate/engine/creation/spi/SessionBuilderImplementor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public interface SessionBuilderImplementor extends SessionBuilder {
3737
@Override
3838
SessionBuilderImplementor noInterceptor();
3939

40+
@Override
41+
SessionBuilderImplementor noSessionInterceptorCreation();
42+
4043
@Override
4144
SessionBuilderImplementor statementInspector(UnaryOperator<String> operator);
4245

hibernate-core/src/main/java/org/hibernate/engine/creation/spi/SharedSessionBuilderImplementor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public interface SharedSessionBuilderImplementor extends SharedSessionBuilder, S
3030
@Override
3131
SharedSessionBuilderImplementor noInterceptor();
3232

33+
@Override
34+
SharedSessionBuilderImplementor noSessionInterceptorCreation();
35+
3336
@Override
3437
SharedSessionBuilderImplementor statementInspector(UnaryOperator<String> operator);
3538

hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public SessionBuilderImplementor noInterceptor() {
5959
return this;
6060
}
6161

62+
@Override
63+
public SessionBuilderImplementor noSessionInterceptorCreation() {
64+
delegate.noSessionInterceptorCreation();
65+
return this;
66+
}
67+
6268
@Override @Deprecated
6369
public SessionBuilderImplementor statementInspector(StatementInspector statementInspector) {
6470
delegate.statementInspector( statementInspector );

hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public SharedSessionBuilder noInterceptor() {
9494
return this;
9595
}
9696

97+
@Override
98+
public SharedSessionBuilder noSessionInterceptorCreation() {
99+
delegate.noSessionInterceptorCreation();
100+
return this;
101+
}
102+
97103
@Override @Deprecated
98104
public SharedSessionBuilder statementInspector(StatementInspector statementInspector) {
99105
delegate.statementInspector( statementInspector );

0 commit comments

Comments
 (0)