Skip to content
8 changes: 0 additions & 8 deletions hibernate-core/src/main/java/org/hibernate/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -1415,14 +1415,6 @@ public interface Session extends SharedSessionContract, EntityManager {
@Incubating
<E> Collection<E> getManagedEntities(EntityType<E> entityType);

/**
* Obtain a {@link Session} builder with the ability to copy certain
* information from this session.
*
* @return the session builder
*/
SharedSessionBuilder sessionWithOptions();

/**
* Add one or more listeners to the Session
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public interface SessionBuilder extends CommonBuilder {
@Override
SessionBuilder noInterceptor();

@Override
SessionBuilder noSessionInterceptorCreation();

@Override
SessionBuilder noStatementInspector();

@Override
SessionBuilder statementInspector(UnaryOperator<String> operator);

Expand Down Expand Up @@ -169,6 +175,7 @@ public interface SessionBuilder extends CommonBuilder {
*
* @return {@code this}, for method chaining
*/
@Override
SessionBuilder jdbcTimeZone(TimeZone timeZone);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public interface SharedSessionBuilder extends SessionBuilder, CommonSharedBuilde
@Override
SharedSessionBuilder statementInspector(UnaryOperator<String> operator);

@Override
SharedSessionBuilder statementInspector();

@Override
SharedSessionBuilder noStatementInspector();

@Override @Deprecated
SharedSessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode);

Expand Down Expand Up @@ -110,6 +116,9 @@ public interface SharedSessionBuilder extends SessionBuilder, CommonSharedBuilde
@Override
SharedSessionBuilder noInterceptor();

@Override
SharedSessionBuilder noSessionInterceptorCreation();

@Override
SharedSessionBuilder connection(Connection connection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
@Incubating
SharedStatelessSessionBuilder statelessWithOptions();

/**
* Obtain a {@link Session} builder with the ability to copy certain
* information from this session.
*
* @return the session builder
*/
SharedSessionBuilder sessionWithOptions();

/**
* Obtain the tenant identifier associated with this session, as a string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import org.hibernate.engine.creation.CommonSharedBuilder;

import java.sql.Connection;
import java.util.TimeZone;
import java.util.function.UnaryOperator;

/**
Expand All @@ -21,7 +23,7 @@
* @author Steve Ebersole
*/
@Incubating
public interface SharedStatelessSessionBuilder extends CommonSharedBuilder {
public interface SharedStatelessSessionBuilder extends StatelessSessionBuilder, CommonSharedBuilder {
/**
* Open the stateless session.
*/
Expand All @@ -39,6 +41,9 @@ public interface SharedStatelessSessionBuilder extends CommonSharedBuilder {
@Override
SharedStatelessSessionBuilder noInterceptor();

@Override
SharedStatelessSessionBuilder noSessionInterceptorCreation();

SharedStatelessSessionBuilder statementInspector(UnaryOperator<String> operator);

@Override
Expand All @@ -55,4 +60,13 @@ public interface SharedStatelessSessionBuilder extends CommonSharedBuilder {

@Override
SharedStatelessSessionBuilder initialCacheMode(CacheMode cacheMode);

@Override
SharedStatelessSessionBuilder connection(Connection connection);

@Override
SharedStatelessSessionBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode);

@Override
SharedStatelessSessionBuilder jdbcTimeZone(TimeZone timeZone);
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@
* @author Gavin King
*/
public interface StatelessSession extends SharedSessionContract {
/**
* Close the stateless session and release the JDBC connection.
*/
void close();

/**
* Insert a record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.hibernate;

import java.sql.Connection;
import java.util.TimeZone;
import java.util.function.UnaryOperator;

import org.hibernate.engine.creation.CommonBuilder;
Expand Down Expand Up @@ -43,6 +44,9 @@ public interface StatelessSessionBuilder extends CommonBuilder {
@Override
StatelessSessionBuilder statementInspector(UnaryOperator<String> operator);

@Override
StatelessSessionBuilder jdbcTimeZone(TimeZone timeZone);

/**
* Define the tenant identifier to be associated with the opened session.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.hibernate.StatelessSession;

import java.sql.Connection;
import java.util.TimeZone;
import java.util.function.UnaryOperator;

/**
Expand Down Expand Up @@ -59,18 +60,45 @@ public interface CommonBuilder {
CommonBuilder interceptor(Interceptor interceptor);

/**
* Signifies that no {@link Interceptor} should be used.
* Specifies that no {@link Interceptor} should be used.
* <p>
* By default, if no {@code Interceptor} is explicitly specified, the
* By default, if no {@code Interceptor} is explicitly
* {@linkplain #interceptor(Interceptor) specified}, the
* {@code Interceptor} associated with the {@link SessionFactory} is
* inherited by the new session.
* inherited by the new session. Or, if there is no interceptor
* associated with the {@link SessionFactory}, but a session-scoped
* interceptor has been configured, a new session-scoped
* {@code Interceptor} will be created for the new session.
* <p>
* Calling {@link #interceptor(Interceptor)} with null has the same effect.
* Calling {@link #interceptor(Interceptor) interceptor(null)} has the
* same effect.
*
* @return {@code this}, for method chaining
*/
CommonBuilder noInterceptor();

/**
* Specifies that no
* {@linkplain org.hibernate.cfg.SessionEventSettings#SESSION_SCOPED_INTERCEPTOR
* session-scoped interceptor} should be instantiated for the new session.
* <p>
* By default, if no {@link Interceptor} is explicitly
* {@linkplain #interceptor(Interceptor) specified}, and if there
* is no interceptor associated with the {@link SessionFactory}, but
* a session-scoped interceptor has been configured, a new session-scoped
* {@code Interceptor} will be created for the new session.
* <p>
* Note that this operation does not disable use of an interceptor
* associated with the {@link SessionFactory}.
*
* @return {@code this}, for method chaining
*
* @see org.hibernate.cfg.SessionEventSettings#SESSION_SCOPED_INTERCEPTOR
*
* @since 7.2
*/
CommonBuilder noSessionInterceptorCreation();

/**
* Applies the given statement inspection function to the session.
*
Expand Down Expand Up @@ -157,4 +185,12 @@ public interface CommonBuilder {
* @see SharedSessionContract#getCacheMode()
*/
CommonBuilder initialCacheMode(CacheMode cacheMode);

/**
* Specify the {@linkplain org.hibernate.cfg.JdbcSettings#JDBC_TIME_ZONE
* JDBC time zone} for the session.
*
* @return {@code this}, for method chaining
*/
CommonBuilder jdbcTimeZone(TimeZone timeZone);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
package org.hibernate.engine.creation;

import org.hibernate.CacheMode;
import org.hibernate.ConnectionAcquisitionMode;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.Incubating;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.StatelessSession;

import java.sql.Connection;
import java.util.TimeZone;
import java.util.function.UnaryOperator;

/**
Expand Down Expand Up @@ -48,6 +52,7 @@ public interface CommonSharedBuilder extends CommonBuilder {
* Signifies that no SQL {@linkplain org.hibernate.resource.jdbc.spi.StatementInspector statement inspector}
* should be used.
*/
@Override
CommonSharedBuilder noStatementInspector();

@Override
Expand All @@ -56,6 +61,9 @@ public interface CommonSharedBuilder extends CommonBuilder {
@Override
CommonSharedBuilder noInterceptor();

@Override
CommonSharedBuilder noSessionInterceptorCreation();

@Override
CommonSharedBuilder statementInspector(UnaryOperator<String> operator);

Expand All @@ -67,4 +75,13 @@ public interface CommonSharedBuilder extends CommonBuilder {

@Override
CommonSharedBuilder tenantIdentifier(Object tenantIdentifier);

@Override
CommonBuilder connection(Connection connection);

@Override
CommonSharedBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode);

@Override
CommonSharedBuilder jdbcTimeZone(TimeZone timeZone);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.Interceptor;
import org.hibernate.engine.creation.CommonBuilder;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.EmptyInterceptor;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;

import java.sql.Connection;
import java.util.TimeZone;
import java.util.function.UnaryOperator;

/**
Expand All @@ -22,25 +24,62 @@
* @author Steve Ebersole
*/
public abstract class AbstractCommonBuilder<T extends CommonBuilder> implements CommonBuilder {
protected final SessionFactoryImpl sessionFactory;
protected final SessionFactoryImplementor sessionFactory;

protected StatementInspector statementInspector;
protected Interceptor interceptor;
protected boolean explicitNoInterceptor;
protected boolean allowInterceptor = true;
protected boolean allowSessionInterceptorCreation = true;
protected Connection connection;
protected PhysicalConnectionHandlingMode connectionHandlingMode;
protected Object tenantIdentifier;
protected boolean readOnly;
protected CacheMode cacheMode;
protected TimeZone jdbcTimeZone;

public AbstractCommonBuilder(SessionFactoryImpl sessionFactory) {
this.sessionFactory = sessionFactory;

final var options = sessionFactory.getSessionFactoryOptions();
public AbstractCommonBuilder(SessionFactoryImplementor factory) {
sessionFactory = factory;
final var options = factory.getSessionFactoryOptions();
statementInspector = options.getStatementInspector();
cacheMode = options.getInitialSessionCacheMode();
tenantIdentifier = sessionFactory.resolveTenantIdentifier();
connectionHandlingMode = options.getPhysicalConnectionHandlingMode();
jdbcTimeZone = options.getJdbcTimeZone();
tenantIdentifier = factory.resolveTenantIdentifier();
}

Interceptor configuredInterceptor() {
// If we were explicitly asked for no interceptor, always return null.
if ( !allowInterceptor ) {
return null;
}

// NOTE: DO NOT return EmptyInterceptor.INSTANCE from here as a "default for the Session".
// We "filter" that one out here. The interceptor returned here should represent the
// explicitly configured Interceptor (if there is one). Return null from here instead;
// Session will handle it.

if ( interceptor != null && interceptor != EmptyInterceptor.INSTANCE ) {
return interceptor;
}

final var options = sessionFactory.getSessionFactoryOptions();

// prefer the SessionFactory-scoped interceptor, prefer that to any Session-scoped interceptor prototype
final var optionsInterceptor = options.getInterceptor();
if ( optionsInterceptor != null && optionsInterceptor != EmptyInterceptor.INSTANCE ) {
return optionsInterceptor;
}

if ( allowSessionInterceptorCreation ) {
// then check the Session-scoped interceptor prototype
final var statelessInterceptorImplementorSupplier =
options.getStatelessInterceptorImplementorSupplier();
if ( statelessInterceptorImplementorSupplier != null ) {
return statelessInterceptorImplementorSupplier.get();
}
}

return null;
}

protected abstract T getThis();
Expand All @@ -64,15 +103,21 @@ public T interceptor(Interceptor interceptor) {
}
else {
this.interceptor = interceptor;
this.explicitNoInterceptor = false;
this.allowInterceptor = true;
}
return getThis();
}

@Override
public T noInterceptor() {
this.interceptor = null;
this.explicitNoInterceptor = true;
this.allowInterceptor = false;
return getThis();
}

@Override
public T noSessionInterceptorCreation() {
this.allowSessionInterceptorCreation = false;
return getThis();
}

Expand Down Expand Up @@ -110,4 +155,10 @@ public T noStatementInspector() {
this.statementInspector = null;
return getThis();
}

@Override
public T jdbcTimeZone(TimeZone timeZone) {
jdbcTimeZone = timeZone;
return getThis();
}
}
Loading