HHH-18009 - Consolidate JdbcObserver and ConnectionObserver into JdbcEventHandler

This commit is contained in:
Steve Ebersole 2024-07-29 07:17:44 -05:00
parent ef58e1540a
commit d8c2ca7824
24 changed files with 194 additions and 448 deletions

View File

@ -24,7 +24,7 @@ import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.SqlStatementLogger;
import org.hibernate.event.spi.EventManager;
import org.hibernate.event.spi.HibernateMonitoringEvent;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
import static org.hibernate.engine.jdbc.JdbcLogging.JDBC_MESSAGE_LOGGER;
@ -257,9 +257,8 @@ public class BatchImpl implements Batch {
);
}
//noinspection deprecation
final JdbcSessionOwner jdbcSessionOwner = jdbcCoordinator.getJdbcSessionOwner();
final JdbcObserver observer = jdbcSessionOwner.getJdbcSessionContext().getObserver();
final JdbcEventHandler eventHandler = jdbcSessionOwner.getJdbcSessionContext().getEventHandler();
try {
getStatementGroup().forEachStatement( (tableName, statementDetails) -> {
final String sql = statementDetails.getSqlString();
@ -275,12 +274,12 @@ public class BatchImpl implements Batch {
final EventManager eventManager = jdbcSessionOwner.getEventManager();
final HibernateMonitoringEvent jdbcBatchExecutionEvent = eventManager.beginJdbcBatchExecutionEvent();
try {
observer.jdbcExecuteBatchStart();
eventHandler.jdbcExecuteBatchStart();
rowCounts = statement.executeBatch();
}
finally {
eventManager.completeJdbcBatchExecutionEvent( jdbcBatchExecutionEvent, sql );
observer.jdbcExecuteBatchEnd();
eventHandler.jdbcExecuteBatchEnd();
}
checkRowCounts( rowCounts, statementDetails );
}

View File

@ -14,7 +14,6 @@ import java.util.Map;
import java.util.StringTokenizer;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.JdbcSettings;
import org.hibernate.dialect.DatabaseVersion;
import org.hibernate.dialect.Dialect;
@ -41,7 +40,7 @@ import org.hibernate.internal.log.ConnectionInfoLogger;
import org.hibernate.jdbc.AbstractReturningWork;
import org.hibernate.jpa.internal.MutableJpaComplianceImpl;
import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
@ -610,6 +609,7 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
* This is a temporary JdbcSessionOwner for the purpose of passing a connection to the Dialect for initialization.
*/
private static class TemporaryJdbcSessionOwner implements JdbcSessionOwner, JdbcSessionContext {
private static final JdbcEventHandler EMPTY_JDBC_EVENT_HANDLER = new JdbcEventHandler();
private final JdbcConnectionAccess jdbcConnectionAccess;
private final JdbcServices jdbcServices;
@ -620,7 +620,6 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
private final PhysicalConnectionHandlingMode connectionHandlingMode;
private final JpaCompliance jpaCompliance;
private final SqlExceptionHelper sqlExceptionHelper;
private static final EmptyJdbcObserver EMPTY_JDBC_OBSERVER = EmptyJdbcObserver.INSTANCE;
TransactionCoordinator transactionCoordinator;
private final EmptyEventManager eventManager;
@ -750,9 +749,9 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
return null;
}
@Override @Deprecated
public JdbcObserver getObserver() {
return EMPTY_JDBC_OBSERVER;
@Override
public JdbcEventHandler getEventHandler() {
return EMPTY_JDBC_EVENT_HANDLER;
}
@Override
@ -784,60 +783,5 @@ public class JdbcEnvironmentInitiator implements StandardServiceInitiator<JdbcEn
public SqlExceptionHelper getSqlExceptionHelper() {
return sqlExceptionHelper;
}
private static class EmptyJdbcObserver implements JdbcObserver{
public static final EmptyJdbcObserver INSTANCE = new EmptyJdbcObserver();
@Override
public void jdbcConnectionAcquisitionStart() {
}
@Override
public void jdbcConnectionAcquisitionEnd(Connection connection) {
}
@Override
public void jdbcConnectionReleaseStart() {
}
@Override
public void jdbcConnectionReleaseEnd() {
}
@Override
public void jdbcPrepareStatementStart() {
}
@Override
public void jdbcPrepareStatementEnd() {
}
@Override
public void jdbcExecuteStatementStart() {
}
@Override
public void jdbcExecuteStatementEnd() {
}
@Override
public void jdbcExecuteBatchStart() {
}
@Override
public void jdbcExecuteBatchEnd() {
}
}
}
}

View File

@ -82,9 +82,7 @@ public class JdbcCoordinatorImpl implements JdbcCoordinator {
JdbcServices jdbcServices) {
this.isUserSuppliedConnection = userSuppliedConnection != null;
final ResourceRegistry resourceRegistry = new ResourceRegistryStandardImpl(
owner.getJdbcSessionContext().getObserver()
);
final ResourceRegistry resourceRegistry = new ResourceRegistryStandardImpl( owner.getJdbcSessionContext().getEventHandler() );
if ( isUserSuppliedConnection ) {
this.logicalConnection = new LogicalConnectionProvidedImpl( userSuppliedConnection, resourceRegistry );
}

View File

@ -16,7 +16,7 @@ import org.hibernate.engine.jdbc.spi.MutationStatementPreparer;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.event.spi.EventManager;
import org.hibernate.event.spi.HibernateMonitoringEvent;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor;
@ -94,21 +94,18 @@ public class MutationStatementPreparerImpl implements MutationStatementPreparer
public PreparedStatement prepareStatement() {
try {
final PreparedStatement preparedStatement;
//noinspection deprecation
final JdbcSessionOwner jdbcSessionOwner = jdbcCoordinator.getJdbcSessionOwner();
final JdbcObserver observer = jdbcSessionOwner
.getJdbcSessionContext()
.getObserver();
final JdbcEventHandler jdbcEventHandler = jdbcSessionOwner.getJdbcSessionContext().getEventHandler();
final EventManager eventManager = jdbcSessionOwner.getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementCreation = eventManager.beginJdbcPreparedStatementCreationEvent();
try {
observer.jdbcPrepareStatementStart();
jdbcEventHandler.jdbcPrepareStatementStart();
preparedStatement = doPrepare();
setStatementTimeout( preparedStatement );
}
finally {
eventManager.completeJdbcPreparedStatementCreationEvent( jdbcPreparedStatementCreation, sql );
observer.jdbcPrepareStatementEnd();
jdbcEventHandler.jdbcPrepareStatementEnd();
}
postProcess( preparedStatement );
return preparedStatement;

View File

@ -78,11 +78,11 @@ public class ResultSetReturnImpl implements ResultSetReturn {
}
private void jdbcExecuteStatementEnd() {
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getObserver().jdbcExecuteStatementEnd();
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getEventHandler().jdbcExecuteStatementEnd();
}
private void jdbcExecuteStatementStart() {
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getObserver().jdbcExecuteStatementStart();
jdbcCoordinator.getJdbcSessionOwner().getJdbcSessionContext().getEventHandler().jdbcExecuteStatementStart();
}
@Override

View File

@ -19,7 +19,7 @@ import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jdbc.spi.StatementPreparer;
import org.hibernate.event.spi.EventManager;
import org.hibernate.event.spi.HibernateMonitoringEvent;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor;
@ -172,7 +172,7 @@ class StatementPreparerImpl implements StatementPreparer {
final PreparedStatement preparedStatement;
final JdbcSessionOwner jdbcSessionOwner = jdbcCoordinator.getJdbcSessionOwner();
final JdbcObserver observer = jdbcSessionOwner.getJdbcSessionContext().getObserver();
final JdbcEventHandler observer = jdbcSessionOwner.getJdbcSessionContext().getEventHandler();
final EventManager eventManager = jdbcSessionOwner.getEventManager();
final HibernateMonitoringEvent jdbcPreparedStatementCreation = eventManager.beginJdbcPreparedStatementCreationEvent();
try {

View File

@ -1,37 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.jdbc.spi;
import java.sql.Connection;
/**
* An observer of logical connection events.
*
* @author Steve Ebersole
*/
public interface ConnectionObserver {
/**
* A physical connection was obtained.
*
* @param connection The physical connection just obtained.
*/
void physicalConnectionObtained(Connection connection);
/**
* A physical connection was released.
*/
void physicalConnectionReleased();
/**
* The logical connection was closed.
*/
void logicalConnectionClosed();
/**
* Notification of a statement being prepared
*/
void statementPrepared();
}

View File

@ -1,32 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.jdbc.spi;
import java.sql.Connection;
/**
* A no-op adapter for {@link ConnectionObserver}.
*
* @author Steve Ebersole
*/
public class ConnectionObserverAdapter implements ConnectionObserver {
@Override
public void physicalConnectionObtained(Connection connection) {
}
@Override
public void physicalConnectionReleased() {
}
@Override
public void logicalConnectionClosed() {
}
@Override
public void statementPrepared() {
}
}

View File

@ -1,16 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.jdbc.spi;
/**
* Additional optional contract for connection observers to indicate that
* they should be released when the physical connection is released.
*
* @author Steve Ebersole
*/
public interface NonDurableConnectionObserver extends ConnectionObserver {
}

View File

@ -15,18 +15,15 @@ import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.JavaServiceLoadable;
@JavaServiceLoadable
@Incubating
/**
* Defines the contract for monitoring low-level events
* involving interactions between the {@link Session} and the database
* or second-level cache.
*
* <p>
* <em>This an incubating API, subject to change.</em>
*
* involving interactions between the {@linkplain org.hibernate.Session}
* and the database or second-level cache.
*
* @apiNote This an incubating API, subject to change.
*/
@JavaServiceLoadable
@Incubating
public interface EventManager {
HibernateMonitoringEvent beginSessionOpenEvent();

View File

@ -99,6 +99,7 @@ import org.hibernate.query.sqm.tree.select.SqmQuerySpec;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.update.SqmUpdateStatement;
import org.hibernate.resource.jdbc.internal.EmptyStatementInspector;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;
@ -279,10 +280,11 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
fastSessionServices.jdbcServices,
fastSessionServices.batchBuilder,
// TODO: this object is deprecated and should be removed
new JdbcObserverImpl(
fastSessionServices.getDefaultJdbcObserver(),
new JdbcEventHandler(
factory.getStatistics(),
sessionEventsManager,
() -> jdbcCoordinator.abortBatch() // since jdbcCoordinator not yet initialized here
// since jdbcCoordinator not yet initialized here
() -> jdbcCoordinator
)
);
}

View File

@ -1,49 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.internal;
import java.io.Serializable;
import java.sql.Connection;
import org.hibernate.engine.jdbc.spi.ConnectionObserver;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.stat.spi.StatisticsImplementor;
/**
* @author Steve Ebersole
*/
public class ConnectionObserverStatsBridge implements ConnectionObserver, Serializable {
private final SessionFactoryImplementor sessionFactory;
public ConnectionObserverStatsBridge(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void physicalConnectionObtained(Connection connection) {
final StatisticsImplementor statistics = sessionFactory.getStatistics();
if ( statistics.isStatisticsEnabled() ) {
statistics.connect();
}
}
@Override
public void physicalConnectionReleased() {
}
@Override
public void logicalConnectionClosed() {
}
@Override
public void statementPrepared() {
final StatisticsImplementor statistics = sessionFactory.getStatistics();
if ( statistics.isStatisticsEnabled() ) {
statistics.prepareStatement();
}
}
}

View File

@ -182,7 +182,6 @@ public final class FastSessionServices {
//Private fields:
private final CacheStoreMode defaultCacheStoreMode;
private final CacheRetrieveMode defaultCacheRetrieveMode;
private final ConnectionObserverStatsBridge defaultJdbcObservers;
private final FormatMapper jsonFormatMapper;
private final FormatMapper xmlFormatMapper;
private final MutationExecutorService mutationExecutorService;
@ -265,7 +264,6 @@ public final class FastSessionServices {
this.defaultCacheRetrieveMode = determineCacheRetrieveMode( defaultSessionProperties );
this.initialSessionCacheMode = CacheModeHelper.interpretCacheMode( defaultCacheStoreMode, defaultCacheRetrieveMode );
this.discardOnClose = sessionFactoryOptions.isReleaseResourcesOnCloseEnabled();
this.defaultJdbcObservers = new ConnectionObserverStatsBridge( sessionFactory );
this.defaultSessionEventListeners = sessionFactoryOptions.getBaselineSessionEventsListenerBuilder();
this.defaultLockOptions = initializeDefaultLockOptions( defaultSessionProperties );
this.initialSessionFlushMode = initializeDefaultFlushMode( defaultSessionProperties );
@ -376,10 +374,6 @@ public final class FastSessionServices {
return cacheStoreMode;
}
public ConnectionObserverStatsBridge getDefaultJdbcObserver() {
return defaultJdbcObservers;
}
public JdbcValuesMappingProducerProvider getJdbcValuesMappingProducerProvider() {
return this.jdbcValuesMappingProducerProvider;
}

View File

@ -1,93 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.internal;
import java.sql.Connection;
import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
/**
* @author Steve Ebersole
*
* @deprecated since {@link JdbcObserver} is deprecated
*/
@Deprecated(since = "5.4", forRemoval = true)
public final class JdbcObserverImpl implements JdbcObserver {
private final ConnectionObserverStatsBridge observer;
private final SessionEventListenerManager eventListenerManager;
private final Runnable abortBatch;
public JdbcObserverImpl(
ConnectionObserverStatsBridge observer,
SessionEventListenerManager eventListenerManager,
Runnable abortBatch) {
this.observer = observer;
this.eventListenerManager = eventListenerManager;
this.abortBatch = abortBatch;
}
@Override
public void jdbcConnectionAcquisitionStart() {
}
@Override
public void jdbcConnectionAcquisitionEnd(Connection connection) {
observer.physicalConnectionObtained( connection );
}
@Override
public void jdbcConnectionReleaseStart() {
}
@Override
public void jdbcConnectionReleaseEnd() {
observer.physicalConnectionReleased();
}
@Override
public void jdbcPrepareStatementStart() {
eventListenerManager.jdbcPrepareStatementStart();
}
@Override
public void jdbcPrepareStatementEnd() {
observer.statementPrepared();
eventListenerManager.jdbcPrepareStatementEnd();
}
@Override
public void jdbcExecuteStatementStart() {
eventListenerManager.jdbcExecuteStatementStart();
}
@Override
public void jdbcExecuteStatementEnd() {
eventListenerManager.jdbcExecuteStatementEnd();
}
@Override
public void jdbcExecuteBatchStart() {
eventListenerManager.jdbcExecuteBatchStart();
}
@Override
public void jdbcExecuteBatchEnd() {
eventListenerManager.jdbcExecuteBatchEnd();
}
@Override
public void jdbcReleaseRegistryResourcesStart() {
abortBatch.run();
}
@Override
public void jdbcReleaseRegistryResourcesEnd() {
}
}

View File

@ -11,7 +11,7 @@ import org.hibernate.engine.jdbc.batch.spi.BatchBuilder;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;
@ -28,7 +28,7 @@ public class JdbcSessionContextImpl implements JdbcSessionContext {
private final JdbcServices jdbcServices;
private final BatchBuilder batchBuilder;
private final transient JdbcObserver jdbcObserver;
private final transient JdbcEventHandler jdbcEventHandler;
public JdbcSessionContextImpl(
SessionFactoryImplementor sessionFactory,
@ -36,13 +36,13 @@ public class JdbcSessionContextImpl implements JdbcSessionContext {
PhysicalConnectionHandlingMode connectionHandlingMode,
JdbcServices jdbcServices,
BatchBuilder batchBuilder,
JdbcObserver jdbcObserver) {
JdbcEventHandler jdbcEventHandler) {
this.sessionFactory = sessionFactory;
this.statementInspector = statementInspector;
this.connectionHandlingMode = connectionHandlingMode;
this.jdbcServices = jdbcServices;
this.batchBuilder = batchBuilder;
this.jdbcObserver = jdbcObserver;
this.jdbcEventHandler = jdbcEventHandler;
if ( statementInspector == null ) {
throw new IllegalArgumentException( "StatementInspector cannot be null" );
@ -94,9 +94,9 @@ public class JdbcSessionContextImpl implements JdbcSessionContext {
return statementInspector;
}
@Override @Deprecated
public JdbcObserver getObserver() {
return jdbcObserver;
@Override
public JdbcEventHandler getEventHandler() {
return jdbcEventHandler;
}
@Override @Deprecated

View File

@ -18,6 +18,8 @@ import org.hibernate.resource.transaction.spi.TransactionStatus;
import org.jboss.logging.Logger;
/**
* Base support for LogicalConnection implementations
*
* @author Steve Ebersole
*/
public abstract class AbstractLogicalConnectionImplementor implements LogicalConnectionImplementor, PhysicalJdbcTransaction {

View File

@ -17,7 +17,7 @@ import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.resource.jdbc.ResourceRegistry;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
@ -41,7 +41,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
private static final Logger log = Logger.getLogger( LogicalConnectionManagedImpl.class );
private final transient JdbcConnectionAccess jdbcConnectionAccess;
private final transient JdbcObserver observer;
private final transient JdbcEventHandler jdbcEventHandler;
private final transient SqlExceptionHelper sqlExceptionHelper;
private final transient PhysicalConnectionHandlingMode connectionHandlingMode;
@ -57,7 +57,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
SqlExceptionHelper sqlExceptionHelper,
ResourceRegistry resourceRegistry) {
this.jdbcConnectionAccess = jdbcConnectionAccess;
this.observer = jdbcSessionContext.getObserver();
this.jdbcEventHandler = jdbcSessionContext.getEventHandler();
this.resourceRegistry = resourceRegistry;
this.connectionHandlingMode = determineConnectionHandlingMode(
@ -67,6 +67,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
this.sqlExceptionHelper = sqlExceptionHelper;
if ( connectionHandlingMode.getAcquisitionMode() == IMMEDIATELY ) {
//noinspection resource
acquireConnectionIfNeeded();
}
@ -121,7 +122,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
private Connection acquireConnectionIfNeeded() {
if ( physicalConnection == null ) {
// todo : is this the right place for these observer calls?
jdbcEventHandler.jdbcConnectionAcquisitionStart();
try {
physicalConnection = jdbcConnectionAccess.obtainConnection();
}
@ -129,7 +130,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
throw sqlExceptionHelper.convert( e, "Unable to acquire JDBC Connection" );
}
finally {
observer.jdbcConnectionAcquisitionEnd( physicalConnection );
jdbcEventHandler.jdbcConnectionAcquisitionEnd( physicalConnection );
}
}
return physicalConnection;
@ -234,6 +235,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
}
}
finally {
jdbcEventHandler.jdbcConnectionReleaseStart();
jdbcConnectionAccess.releaseConnection( localVariableConnection );
}
}
@ -241,7 +243,7 @@ public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImple
throw sqlExceptionHelper.convert( e, "Unable to release JDBC Connection" );
}
finally {
observer.jdbcConnectionReleaseEnd();
jdbcEventHandler.jdbcConnectionReleaseEnd();
}
}

View File

@ -20,7 +20,7 @@ import org.hibernate.JDBCException;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.resource.jdbc.ResourceRegistry;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
/**
* Helps to track statements and resultsets which need being closed.
@ -49,7 +49,7 @@ public final class ResourceRegistryStandardImpl implements ResourceRegistry {
//but in this case the overhead of HashSet is not negligible.
private static final HashMap<ResultSet,Object> EMPTY = new HashMap<>( 1, 0.2f );
private final JdbcObserver jdbcObserver;
private final JdbcEventHandler jdbcEventHandler;
private final HashMap<Statement, HashMap<ResultSet,Object>> xref = new HashMap<>();
private HashMap<ResultSet,Object> unassociatedResultSets;
@ -64,8 +64,8 @@ public final class ResourceRegistryStandardImpl implements ResourceRegistry {
this( null );
}
public ResourceRegistryStandardImpl(JdbcObserver jdbcObserver) {
this.jdbcObserver = jdbcObserver;
public ResourceRegistryStandardImpl(JdbcEventHandler jdbcEventHandler) {
this.jdbcEventHandler = jdbcEventHandler;
}
@Override
@ -244,7 +244,7 @@ public final class ResourceRegistryStandardImpl implements ResourceRegistry {
}
else {
if ( unassociatedResultSets == null ) {
this.unassociatedResultSets = new HashMap<ResultSet,Object>();
this.unassociatedResultSets = new HashMap<>();
}
unassociatedResultSets.put( resultSet, PRESENT );
}
@ -327,8 +327,8 @@ public final class ResourceRegistryStandardImpl implements ResourceRegistry {
public void releaseResources() {
log.trace( "Releasing JDBC resources" );
if ( jdbcObserver != null ) {
jdbcObserver.jdbcReleaseRegistryResourcesStart();
if ( jdbcEventHandler != null ) {
jdbcEventHandler.jdbcReleaseRegistryResourcesStart();
}
xref.forEach( ResourceRegistryStandardImpl::releaseXref );
@ -372,6 +372,10 @@ public final class ResourceRegistryStandardImpl implements ResourceRegistry {
} );
nclobs = null;
}
if ( jdbcEventHandler != null ) {
jdbcEventHandler.jdbcReleaseRegistryResourcesEnd();
}
}
private boolean hasRegistered(final HashMap resource) {

View File

@ -0,0 +1,134 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.resource.jdbc.spi;
import java.sql.Connection;
import java.util.function.Supplier;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.spi.SessionEventListenerManager;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.jboss.logging.Logger;
/**
* Acts as an observer for various events regarding JDBC interactions and doing one or more of -<ol>
* <li>delegating to {@linkplain org.hibernate.stat.spi.StatisticsImplementor}</li>
* <li>delegating to {@linkplain SessionEventListenerManager}</li>
* <li>logging</li>
* </ol>
*
* @since 7.0
*
* @author Steve Ebersole
*/
public class JdbcEventHandler {
private static final Logger log = Logger.getLogger( JdbcEventHandler.class );
private final StatisticsImplementor statistics;
private final SessionEventListenerManager sessionListener;
private final Supplier<JdbcCoordinator> jdbcCoordinatorSupplier;
public JdbcEventHandler(
StatisticsImplementor statistics,
SessionEventListenerManager sessionListener,
Supplier<JdbcCoordinator> jdbcCoordinatorSupplier) {
this.statistics = statistics;
this.sessionListener = sessionListener;
this.jdbcCoordinatorSupplier = jdbcCoordinatorSupplier;
}
/**
* Creates a specialized JdbcEventHandler for use with "temporary Sessions"
*/
public JdbcEventHandler() {
this( null, null, null );
}
public void jdbcConnectionAcquisitionStart() {
// NOTE : Connection acquisition and release events are propagated to
// SessionEventListenerManager via the JdbcConnectionAccess contracts
// which is the more proper place, so here we do nothing
}
public void jdbcConnectionAcquisitionEnd(Connection connection) {
// NOTE : Connection acquisition and release events are propagated to
// SessionEventListenerManager via the JdbcConnectionAccess contracts
// which is the more proper place, so here we do nothing
if ( statistics != null && statistics.isStatisticsEnabled() ) {
statistics.connect();
}
}
public void jdbcConnectionReleaseStart() {
// NOTE : Connection acquisition and release events are propagated to
// SessionEventListenerManager via the JdbcConnectionAccess contracts
// which is the more proper place, so here we do nothing
}
public void jdbcConnectionReleaseEnd() {
// NOTE : Connection acquisition and release events are propagated to
// SessionEventListenerManager via the JdbcConnectionAccess contracts
// which is the more proper place, so here we do nothing
}
public void jdbcPrepareStatementStart() {
if ( sessionListener != null ) {
sessionListener.jdbcPrepareStatementStart();
}
}
public void jdbcPrepareStatementEnd() {
if ( sessionListener != null ) {
sessionListener.jdbcPrepareStatementEnd();
}
if ( statistics != null && statistics.isStatisticsEnabled() ) {
statistics.prepareStatement();
}
}
public void jdbcExecuteStatementStart() {
if ( sessionListener != null ) {
sessionListener.jdbcExecuteStatementStart();
}
}
public void jdbcExecuteStatementEnd() {
if ( sessionListener != null ) {
sessionListener.jdbcExecuteStatementEnd();
}
}
public void jdbcExecuteBatchStart() {
if ( sessionListener != null ) {
sessionListener.jdbcExecuteBatchStart();
}
}
public void jdbcExecuteBatchEnd() {
if ( sessionListener != null ) {
sessionListener.jdbcExecuteBatchEnd();
}
}
public void jdbcReleaseRegistryResourcesStart() {
if ( jdbcCoordinatorSupplier != null ) {
final JdbcCoordinator jdbcCoordinator = jdbcCoordinatorSupplier.get();
if ( jdbcCoordinator != null ) {
log.debug( "Aborting batches as part of ResourceRegistryStandardImpl#releaseResources" );
jdbcCoordinator.abortBatch();
}
}
}
public void jdbcReleaseRegistryResourcesEnd() {
}
}

View File

@ -1,38 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.resource.jdbc.spi;
import java.sql.Connection;
/**
* @deprecated It is no longer possible to plug custom implementations
* of this SPI. It will be removed.
*
* @author Steve Ebersole
*/
@Deprecated(since = "5.4", forRemoval = true)
public interface JdbcObserver {
void jdbcConnectionAcquisitionStart();
void jdbcConnectionAcquisitionEnd(Connection connection);
void jdbcConnectionReleaseStart();
void jdbcConnectionReleaseEnd();
void jdbcPrepareStatementStart();
void jdbcPrepareStatementEnd();
void jdbcExecuteStatementStart();
void jdbcExecuteStatementEnd();
void jdbcExecuteBatchStart();
void jdbcExecuteBatchEnd();
default void jdbcReleaseRegistryResourcesStart() {}
default void jdbcReleaseRegistryResourcesEnd() {}
}

View File

@ -57,11 +57,7 @@ public interface JdbcSessionContext {
StatisticsImplementor getStatistics();
/**
* @deprecated since {@link JdbcObserver} is deprecated
*/
@Deprecated(forRemoval = true)
JdbcObserver getObserver();
JdbcEventHandler getEventHandler();
/**
* Retrieve the session factory for this environment.

View File

@ -25,7 +25,6 @@ public interface LogicalConnectionImplementor extends LogicalConnection {
*
* @return The connection
*/
// todo : expose Connection as here? Or accept(Work) where Work is given access to Connection?
Connection getPhysicalConnection();
PhysicalConnectionHandlingMode getConnectionHandlingMode();

View File

@ -1,57 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.common;
import java.sql.Connection;
import org.hibernate.engine.jdbc.spi.ConnectionObserver;
/**
* @author Steve Ebersole
*/
public class JournalingConnectionObserver implements ConnectionObserver {
private int physicalConnectionObtainedCount = 0;
private int physicalConnectionReleasedCount = 0;
private int logicalConnectionClosedCount = 0;
private int statementPreparedCount = 0;
@Override
public void physicalConnectionObtained(Connection connection) {
physicalConnectionObtainedCount++;
}
@Override
public void physicalConnectionReleased() {
physicalConnectionReleasedCount++;
}
@Override
public void logicalConnectionClosed() {
logicalConnectionClosedCount++;
}
@Override
public void statementPrepared() {
statementPreparedCount++;
}
public int getPhysicalConnectionObtainedCount() {
return physicalConnectionObtainedCount;
}
public int getPhysicalConnectionReleasedCount() {
return physicalConnectionReleasedCount;
}
public int getLogicalConnectionClosedCount() {
return logicalConnectionClosedCount;
}
public int getStatementPreparedCount() {
return statementPreparedCount;
}
}

View File

@ -18,7 +18,7 @@ import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.resource.jdbc.spi.JdbcObserver;
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
@ -65,8 +65,8 @@ public class JdbcCoordinatorTest {
when( sessionContext.getPhysicalConnectionHandlingMode() ).thenReturn(
PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD );
JdbcObserver jdbcObserver = Mockito.mock( JdbcObserver.class );
when( sessionContext.getObserver() ).thenReturn( jdbcObserver );
JdbcEventHandler jdbcEventHandler = Mockito.mock( JdbcEventHandler.class );
when( sessionContext.getEventHandler() ).thenReturn( jdbcEventHandler );
JdbcServices jdbcServices = Mockito.mock( JdbcServices.class );