HHH-13565 Avoid allocating ArrayList of ConnectionObserver instances on each Session

This commit is contained in:
Sanne Grinovero 2019-08-13 20:21:03 +01:00
parent 4ef8030f51
commit 4b2f056a63
4 changed files with 21 additions and 14 deletions

View File

@ -34,16 +34,13 @@ import org.hibernate.LockMode;
import org.hibernate.MultiTenancyStrategy;
import org.hibernate.SessionException;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.cache.spi.CacheTransactionSynchronization;
import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.engine.internal.SessionEventListenerManagerImpl;
import org.hibernate.engine.jdbc.LobCreationContext;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.jdbc.spi.JdbcServices;
@ -173,7 +170,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
this.jdbcTimeZone = options.getJdbcTimeZone();
final StatementInspector statementInspector = interpret( options.getStatementInspector() );
this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector );
this.jdbcSessionContext = new JdbcSessionContextImpl( this, statementInspector, fastSessionServices );
this.entityNameResolver = new CoordinatingEntityNameResolver( factory, interceptor );
@ -1201,7 +1198,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
factory = SessionFactoryImpl.deserialize( ois );
fastSessionServices = factory.getFastSessionServices();
jdbcSessionContext = new JdbcSessionContextImpl( this, (StatementInspector) ois.readObject() );
jdbcSessionContext = new JdbcSessionContextImpl( this, (StatementInspector) ois.readObject(), fastSessionServices );
jdbcCoordinator = JdbcCoordinatorImpl.deserialize( ois, this );
cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this );

View File

@ -14,6 +14,7 @@ import org.hibernate.cfg.Environment;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.spi.ConnectionObserver;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.AutoFlushEventListener;
@ -41,6 +42,7 @@ import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -118,6 +120,7 @@ final class FastSessionServices {
private final Dialect dialect;
private final CacheStoreMode defaultCacheStoreMode;
private final CacheRetrieveMode defaultCacheRetrieveMode;
private List<ConnectionObserver> defaultJdbcObservers;
FastSessionServices(SessionFactoryImpl sf) {
Objects.requireNonNull( sf );
@ -165,6 +168,7 @@ final class FastSessionServices {
this.defaultCacheRetrieveMode = determineCacheRetrieveMode( defaultSessionProperties );
this.initialSessionCacheMode = CacheModeHelper.interpretCacheMode( defaultCacheStoreMode, defaultCacheRetrieveMode );
this.discardOnClose = sf.getSessionFactoryOptions().isReleaseResourcesOnCloseEnabled();
this.defaultJdbcObservers = Collections.singletonList( new ConnectionObserverStatsBridge( sf ) );
}
Iterable<ClearEventListener> getClearEventListeners() {
@ -326,4 +330,9 @@ final class FastSessionServices {
private static CacheStoreMode determineCacheStoreMode(Map<String, Object> settings) {
return ( CacheStoreMode ) settings.get( JPA_SHARED_CACHE_STORE_MODE );
}
public Iterable<ConnectionObserver> getDefaultJdbcObservers() {
return defaultJdbcObservers;
}
}

View File

@ -7,8 +7,6 @@
package org.hibernate.internal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.engine.jdbc.spi.ConnectionObserver;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -18,13 +16,13 @@ import org.hibernate.resource.jdbc.spi.JdbcObserver;
* @author Steve Ebersole
*/
public class JdbcObserverImpl implements JdbcObserver {
private final SharedSessionContractImplementor session;
private final transient List<ConnectionObserver> observers;
public JdbcObserverImpl(SharedSessionContractImplementor session) {
private final SharedSessionContractImplementor session;
private final Iterable<ConnectionObserver> observers;
public JdbcObserverImpl(SharedSessionContractImplementor session, FastSessionServices fastSessionServices) {
this.session = session;
this.observers = new ArrayList<>();
this.observers.add( new ConnectionObserverStatsBridge( session.getFactory() ) );
this.observers = fastSessionServices.getDefaultJdbcObservers();
}
@Override

View File

@ -28,12 +28,15 @@ public class JdbcSessionContextImpl implements JdbcSessionContext {
private final transient ServiceRegistry serviceRegistry;
private final transient JdbcObserver jdbcObserver;
public JdbcSessionContextImpl(SharedSessionContractImplementor session, StatementInspector statementInspector) {
public JdbcSessionContextImpl(
SharedSessionContractImplementor session,
StatementInspector statementInspector,
FastSessionServices fastSessionServices) {
this.sessionFactory = session.getFactory();
this.statementInspector = statementInspector;
this.connectionHandlingMode = settings().getPhysicalConnectionHandlingMode();
this.serviceRegistry = sessionFactory.getServiceRegistry();
this.jdbcObserver = new JdbcObserverImpl( session );
this.jdbcObserver = new JdbcObserverImpl( session, fastSessionServices );
if ( this.statementInspector == null ) {
throw new IllegalArgumentException( "StatementInspector cannot be null" );