Skip to content

Commit c99e2ee

Browse files
committed
HHH-19559 some related refactoring
1 parent 67363e9 commit c99e2ee

File tree

8 files changed

+88
-88
lines changed

8 files changed

+88
-88
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/internal/JdbcEnvironmentInitiator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,5 +806,13 @@ public boolean isActive() {
806806
public SqlExceptionHelper getSqlExceptionHelper() {
807807
return sqlExceptionHelper;
808808
}
809+
810+
@Override
811+
public void afterObtainConnection(Connection connection) {
812+
}
813+
814+
@Override
815+
public void beforeReleaseConnection(Connection connection) {
816+
}
809817
}
810818
}

hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/JdbcCoordinatorImpl.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,20 @@ public JdbcCoordinatorImpl(
7676
Connection userSuppliedConnection,
7777
JdbcSessionOwner owner,
7878
JdbcServices jdbcServices) {
79+
this.owner = owner;
80+
this.jdbcServices = jdbcServices;
7981
this.isUserSuppliedConnection = userSuppliedConnection != null;
82+
this.logicalConnection = createLogicalConnection( userSuppliedConnection, owner );
83+
}
8084

85+
private static LogicalConnectionImplementor createLogicalConnection(
86+
Connection userSuppliedConnection,
87+
JdbcSessionOwner owner) {
8188
final ResourceRegistry resourceRegistry =
8289
new ResourceRegistryStandardImpl( owner.getJdbcSessionContext().getEventHandler() );
83-
if ( isUserSuppliedConnection ) {
84-
this.logicalConnection = new LogicalConnectionProvidedImpl( userSuppliedConnection, resourceRegistry );
85-
}
86-
else {
87-
this.logicalConnection = new LogicalConnectionManagedImpl(
88-
owner.getJdbcConnectionAccess(),
89-
owner.getJdbcSessionContext(),
90-
owner.getSqlExceptionHelper(),
91-
resourceRegistry
92-
);
93-
}
94-
this.owner = owner;
95-
this.jdbcServices = jdbcServices;
90+
return userSuppliedConnection == null
91+
? new LogicalConnectionManagedImpl( owner, resourceRegistry )
92+
: new LogicalConnectionProvidedImpl( userSuppliedConnection, resourceRegistry );
9693
}
9794

9895
private JdbcCoordinatorImpl(
@@ -405,7 +402,7 @@ public JdbcResourceTransaction getResourceLocalTransaction() {
405402
*/
406403
@Override
407404
public void serialize(ObjectOutputStream oos) throws IOException {
408-
if ( ! isReadyForSerialization() ) {
405+
if ( !isReadyForSerialization() ) {
409406
throw new HibernateException( "Cannot serialize Session while connected" );
410407
}
411408
oos.writeBoolean( isUserSuppliedConnection );
@@ -423,21 +420,13 @@ public void serialize(ObjectOutputStream oos) throws IOException {
423420
* @throws IOException Trouble accessing the stream
424421
* @throws ClassNotFoundException Trouble reading the stream
425422
*/
426-
public static JdbcCoordinatorImpl deserialize(
427-
ObjectInputStream ois,
428-
JdbcSessionOwner owner) throws IOException, ClassNotFoundException {
423+
public static JdbcCoordinatorImpl deserialize(ObjectInputStream ois, JdbcSessionOwner owner)
424+
throws IOException, ClassNotFoundException {
429425
final boolean isUserSuppliedConnection = ois.readBoolean();
430-
final LogicalConnectionImplementor logicalConnection;
431-
if ( isUserSuppliedConnection ) {
432-
logicalConnection = LogicalConnectionProvidedImpl.deserialize( ois );
433-
}
434-
else {
435-
logicalConnection = LogicalConnectionManagedImpl.deserialize(
436-
ois,
437-
owner.getJdbcConnectionAccess(),
438-
owner.getJdbcSessionContext()
439-
);
440-
}
426+
final var logicalConnection =
427+
isUserSuppliedConnection
428+
? LogicalConnectionProvidedImpl.deserialize( ois )
429+
: LogicalConnectionManagedImpl.deserialize( ois, owner );
441430
return new JdbcCoordinatorImpl( logicalConnection, isUserSuppliedConnection, owner );
442431
}
443432
}

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55
package org.hibernate.engine.spi;
66

7-
import java.sql.Connection;
8-
import java.sql.SQLException;
97
import java.util.Set;
108
import java.util.UUID;
119
import jakarta.persistence.TransactionRequiredException;
@@ -574,31 +572,4 @@ default boolean isStatelessSession() {
574572
*/
575573
@Incubating
576574
Object loadFromSecondLevelCache(EntityPersister persister, EntityKey entityKey, Object instanceToLoad, LockMode lockMode);
577-
578-
/**
579-
* Called after a non-contextual JDBC connection is obtained.
580-
* <p>
581-
* Sets the schema to the schema belonging to the current tenant if:
582-
* <ol>
583-
* <li>{@value org.hibernate.cfg.MultiTenancySettings#SET_TENANT_SCHEMA} is enabled, and
584-
* <li>this session has an active tenant id.
585-
* </ol>
586-
*
587-
* @param connection A JDBC connection which was just acquired
588-
*
589-
* @since 7.1
590-
*/
591-
void afterObtainConnection(Connection connection) throws SQLException;
592-
593-
/**
594-
* Called right before non-contextual a JDBC connection is released.
595-
* <p>
596-
* Unset the schema which was set by {@link #afterObtainConnection},
597-
* if any.
598-
* .
599-
* @param connection The JDBC connection which is about to be released
600-
*
601-
* @since 7.1
602-
*/
603-
void beforeReleaseConnection(Connection connection) throws SQLException;
604575
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,13 @@ public Connection obtainConnection() throws SQLException {
4444
}
4545

4646
final EventMonitor eventMonitor = session.getEventMonitor();
47-
final DiagnosticEvent jdbcConnectionAcquisitionEvent = eventMonitor.beginJdbcConnectionAcquisitionEvent();
47+
final DiagnosticEvent connectionAcquisitionEvent = eventMonitor.beginJdbcConnectionAcquisitionEvent();
4848
try {
4949
listener.jdbcConnectionAcquisitionStart();
5050
return connectionProvider.getConnection( tenantIdentifier );
5151
}
5252
finally {
53-
eventMonitor.completeJdbcConnectionAcquisitionEvent(
54-
jdbcConnectionAcquisitionEvent,
55-
session,
56-
tenantIdentifier
57-
);
53+
eventMonitor.completeJdbcConnectionAcquisitionEvent( connectionAcquisitionEvent, session, tenantIdentifier );
5854
listener.jdbcConnectionAcquisitionEnd();
5955
}
6056
}
@@ -66,13 +62,13 @@ public void releaseConnection(Connection connection) throws SQLException {
6662
}
6763

6864
final EventMonitor eventMonitor = session.getEventMonitor();
69-
final DiagnosticEvent jdbcConnectionReleaseEvent = eventMonitor.beginJdbcConnectionReleaseEvent();
65+
final DiagnosticEvent connectionReleaseEvent = eventMonitor.beginJdbcConnectionReleaseEvent();
7066
try {
7167
listener.jdbcConnectionReleaseStart();
7268
connectionProvider.releaseConnection( tenantIdentifier, connection );
7369
}
7470
finally {
75-
eventMonitor.completeJdbcConnectionReleaseEvent( jdbcConnectionReleaseEvent, session, tenantIdentifier );
71+
eventMonitor.completeJdbcConnectionReleaseEvent( connectionReleaseEvent, session, tenantIdentifier );
7672
listener.jdbcConnectionReleaseEnd();
7773
}
7874
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,14 @@ public NonContextualJdbcConnectionAccess(
4141
@Override
4242
public Connection obtainConnection() throws SQLException {
4343
final EventMonitor eventMonitor = session.getEventMonitor();
44-
final DiagnosticEvent jdbcConnectionAcquisitionEvent = eventMonitor.beginJdbcConnectionAcquisitionEvent();
44+
final DiagnosticEvent connectionAcquisitionEvent = eventMonitor.beginJdbcConnectionAcquisitionEvent();
4545
final Connection connection;
4646
try {
4747
listener.jdbcConnectionAcquisitionStart();
4848
connection = connectionProvider.getConnection();
4949
}
5050
finally {
51-
eventMonitor.completeJdbcConnectionAcquisitionEvent(
52-
jdbcConnectionAcquisitionEvent,
53-
session,
54-
null
55-
);
51+
eventMonitor.completeJdbcConnectionAcquisitionEvent( connectionAcquisitionEvent, session, null );
5652
listener.jdbcConnectionAcquisitionEnd();
5753
}
5854

@@ -81,13 +77,13 @@ public void releaseConnection(Connection connection) throws SQLException {
8177
}
8278

8379
final EventMonitor eventMonitor = session.getEventMonitor();
84-
final DiagnosticEvent jdbcConnectionReleaseEvent = eventMonitor.beginJdbcConnectionReleaseEvent();
80+
final DiagnosticEvent connectionReleaseEvent = eventMonitor.beginJdbcConnectionReleaseEvent();
8581
try {
8682
listener.jdbcConnectionReleaseStart();
8783
connectionProvider.closeConnection( connection );
8884
}
8985
finally {
90-
eventMonitor.completeJdbcConnectionReleaseEvent( jdbcConnectionReleaseEvent, session, null );
86+
eventMonitor.completeJdbcConnectionReleaseEvent( connectionReleaseEvent, session, null );
9187
listener.jdbcConnectionReleaseEnd();
9288
}
9389
}

hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionManagedImpl.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hibernate.resource.jdbc.ResourceRegistry;
2121
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
2222
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
23+
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
2324
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
2425

2526
import static org.hibernate.ConnectionAcquisitionMode.IMMEDIATELY;
@@ -56,21 +57,17 @@ public LogicalConnectionManagedImpl(
5657
SqlExceptionHelper sqlExceptionHelper,
5758
ResourceRegistry resourceRegistry) {
5859
this.jdbcConnectionAccess = jdbcConnectionAccess;
59-
this.jdbcEventHandler = jdbcSessionContext.getEventHandler();
60-
this.resourceRegistry = resourceRegistry;
61-
62-
this.connectionHandlingMode = determineConnectionHandlingMode(
63-
jdbcSessionContext.getPhysicalConnectionHandlingMode(),
64-
jdbcConnectionAccess );
65-
6660
this.sqlExceptionHelper = sqlExceptionHelper;
61+
this.resourceRegistry = resourceRegistry;
62+
jdbcEventHandler = jdbcSessionContext.getEventHandler();
6763

64+
connectionHandlingMode = determineConnectionHandlingMode( jdbcSessionContext, jdbcConnectionAccess );
6865
if ( connectionHandlingMode.getAcquisitionMode() == IMMEDIATELY ) {
6966
//noinspection resource
7067
acquireConnectionIfNeeded();
7168
}
7269

73-
this.providerDisablesAutoCommit = jdbcSessionContext.doesConnectionProviderDisableAutoCommit();
70+
providerDisablesAutoCommit = jdbcSessionContext.doesConnectionProviderDisableAutoCommit();
7471
if ( providerDisablesAutoCommit ) {
7572
log.connectionProviderDisablesAutoCommitEnabled();
7673
}
@@ -89,9 +86,19 @@ public LogicalConnectionManagedImpl(
8986
);
9087
}
9188

89+
public LogicalConnectionManagedImpl(JdbcSessionOwner owner, ResourceRegistry resourceRegistry) {
90+
this(
91+
owner.getJdbcConnectionAccess(),
92+
owner.getJdbcSessionContext(),
93+
owner.getSqlExceptionHelper(),
94+
resourceRegistry
95+
);
96+
}
97+
9298
private PhysicalConnectionHandlingMode determineConnectionHandlingMode(
93-
PhysicalConnectionHandlingMode connectionHandlingMode,
99+
JdbcSessionContext jdbcSessionContext,
94100
JdbcConnectionAccess jdbcConnectionAccess) {
101+
final var connectionHandlingMode = jdbcSessionContext.getPhysicalConnectionHandlingMode();
95102
return connectionHandlingMode.getReleaseMode() == AFTER_STATEMENT
96103
&& !jdbcConnectionAccess.supportsAggressiveRelease()
97104
? DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION
@@ -175,7 +182,7 @@ public void beforeTransactionCompletion() {
175182
public void afterTransaction() {
176183
super.afterTransaction();
177184
if ( connectionHandlingMode.getReleaseMode() != ON_CLOSE ) {
178-
// NOTE : we check for !ON_CLOSE here (rather than AFTER_TRANSACTION) to also catch:
185+
// NOTE: we check for !ON_CLOSE here (rather than AFTER_TRANSACTION) to also catch:
179186
// - AFTER_STATEMENT cases that were circumvented due to held resources
180187
// - BEFORE_TRANSACTION_COMPLETION cases that were circumvented because a rollback occurred
181188
// (we don't get a beforeTransactionCompletion event on rollback).
@@ -241,11 +248,17 @@ public void serialize(ObjectOutputStream oos) throws IOException {
241248
public static LogicalConnectionManagedImpl deserialize(
242249
ObjectInputStream ois,
243250
JdbcConnectionAccess jdbcConnectionAccess,
244-
JdbcSessionContext jdbcSessionContext) throws IOException {
251+
JdbcSessionContext jdbcSessionContext)
252+
throws IOException {
245253
final boolean isClosed = ois.readBoolean();
246254
return new LogicalConnectionManagedImpl( jdbcConnectionAccess, jdbcSessionContext, isClosed );
247255
}
248256

257+
public static LogicalConnectionManagedImpl deserialize(ObjectInputStream ois, JdbcSessionOwner owner)
258+
throws IOException {
259+
return deserialize( ois, owner.getJdbcConnectionAccess(), owner.getJdbcSessionContext() );
260+
}
261+
249262
@Override
250263
public Connection close() {
251264
if ( !closed ) {

hibernate-core/src/main/java/org/hibernate/resource/jdbc/spi/JdbcSessionOwner.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import org.hibernate.event.monitor.spi.EventMonitor;
1111
import org.hibernate.resource.transaction.spi.TransactionCoordinator;
1212

13+
import java.sql.Connection;
14+
import java.sql.SQLException;
15+
1316
import static java.lang.reflect.InvocationHandler.invokeDefault;
1417
import static java.lang.reflect.Proxy.newProxyInstance;
1518

@@ -78,6 +81,33 @@ default SqlExceptionHelper getSqlExceptionHelper() {
7881
return getJdbcSessionContext().getJdbcServices().getSqlExceptionHelper();
7982
}
8083

84+
/**
85+
* Called after a managed JDBC connection is obtained.
86+
* <p>
87+
* Sets the schema to the schema belonging to the current tenant if:
88+
* <ol>
89+
* <li>{@value org.hibernate.cfg.MultiTenancySettings#SET_TENANT_SCHEMA} is enabled, and
90+
* <li>this session has an active tenant id.
91+
* </ol>
92+
*
93+
* @param connection A JDBC connection which was just acquired
94+
*
95+
* @since 7.1
96+
*/
97+
void afterObtainConnection(Connection connection) throws SQLException;
98+
99+
/**
100+
* Called right before a managed JDBC connection is released.
101+
* <p>
102+
* Unset the schema which was set by {@link #afterObtainConnection},
103+
* if any.
104+
* .
105+
* @param connection The JDBC connection which is about to be released
106+
*
107+
* @since 7.1
108+
*/
109+
void beforeReleaseConnection(Connection connection) throws SQLException;
110+
81111
/**
82112
* Obtain a reference to the {@link EventMonitor}.
83113
*

hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventMonitor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,7 @@ public void completeCollectionRemoveEvent(
780780
}
781781

782782
private String getSessionIdentifier(SharedSessionContractImplementor session) {
783-
if ( session == null ) {
784-
return null;
785-
}
786-
return session.getSessionIdentifier().toString();
783+
return session == null ? null : session.getSessionIdentifier().toString();
787784
}
788785

789786
private String getEntityName(EntityPersister persister) {

0 commit comments

Comments
 (0)