HHH-12626 Avoid high CPU contention by not allocating Session UUIDs eagerly

This commit is contained in:
Sanne Grinovero 2018-05-24 20:49:45 +01:00
parent dc29e45af3
commit d5244de697
2 changed files with 21 additions and 8 deletions

View File

@ -106,7 +106,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
private transient SessionFactoryImpl factory; private transient SessionFactoryImpl factory;
private final String tenantIdentifier; private final String tenantIdentifier;
private final UUID sessionIdentifier; private UUID sessionIdentifier;
private transient JdbcConnectionAccess jdbcConnectionAccess; private transient JdbcConnectionAccess jdbcConnectionAccess;
private transient JdbcSessionContext jdbcSessionContext; private transient JdbcSessionContext jdbcSessionContext;
@ -140,8 +140,6 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreationOptions options) { public AbstractSharedSessionContract(SessionFactoryImpl factory, SessionCreationOptions options) {
this.factory = factory; this.factory = factory;
this.sessionIdentifier = StandardRandomStrategy.INSTANCE.generateUUID( null );
this.cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this ); this.cacheTransactionSync = factory.getCache().getRegionFactory().createTransactionContext( this );
this.flushMode = options.getInitialSessionFlushMode(); this.flushMode = options.getInitialSessionFlushMode();
@ -269,6 +267,10 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
@Override @Override
public UUID getSessionIdentifier() { public UUID getSessionIdentifier() {
if ( this.sessionIdentifier == null ) {
//Lazily initialized: otherwise all the UUID generations will cause of significant amount of contention.
this.sessionIdentifier = StandardRandomStrategy.INSTANCE.generateUUID( null );
}
return sessionIdentifier; return sessionIdentifier;
} }
@ -1072,7 +1074,10 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
@SuppressWarnings("unused") @SuppressWarnings("unused")
private void writeObject(ObjectOutputStream oos) throws IOException { private void writeObject(ObjectOutputStream oos) throws IOException {
log.trace( "Serializing " + getClass().getSimpleName() + " [" ); if ( log.isTraceEnabled() ) {
log.trace( "Serializing " + getClass().getSimpleName() + " [" );
}
if ( !jdbcCoordinator.isReadyForSerialization() ) { if ( !jdbcCoordinator.isReadyForSerialization() ) {
// throw a more specific (helpful) exception message when this happens from Session, // throw a more specific (helpful) exception message when this happens from Session,
@ -1103,7 +1108,9 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException, SQLException { private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException, SQLException {
log.trace( "Deserializing " + getClass().getSimpleName() ); if ( log.isTraceEnabled() ) {
log.trace( "Deserializing " + getClass().getSimpleName() );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Step 1 :: read back non-transient state... // Step 1 :: read back non-transient state...

View File

@ -419,7 +419,9 @@ public final class SessionImpl
} }
public void closeWithoutOpenChecks() throws HibernateException { public void closeWithoutOpenChecks() throws HibernateException {
log.tracef( "Closing session [%s]", getSessionIdentifier() ); if ( TRACE_ENABLED ) {
log.tracef( "Closing session [%s]", getSessionIdentifier() );
}
// todo : we want this check if usage is JPA, but not native Hibernate usage // todo : we want this check if usage is JPA, but not native Hibernate usage
if ( getSessionFactory().getSessionFactoryOptions().isJpaBootstrap() ) { if ( getSessionFactory().getSessionFactoryOptions().isJpaBootstrap() ) {
@ -3953,7 +3955,9 @@ public final class SessionImpl
* @throws IOException Indicates a general IO stream exception * @throws IOException Indicates a general IO stream exception
*/ */
private void writeObject(ObjectOutputStream oos) throws IOException { private void writeObject(ObjectOutputStream oos) throws IOException {
log.tracef( "Serializing Session [%s]", getSessionIdentifier() ); if ( TRACE_ENABLED ) {
log.tracef( "Serializing Session [%s]", getSessionIdentifier() );
}
oos.defaultWriteObject(); oos.defaultWriteObject();
@ -3972,7 +3976,9 @@ public final class SessionImpl
* @throws ClassNotFoundException Indicates a class resolution issue * @throws ClassNotFoundException Indicates a class resolution issue
*/ */
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException, SQLException { private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException, SQLException {
log.tracef( "Deserializing Session [%s]", getSessionIdentifier() ); if ( TRACE_ENABLED ) {
log.tracef( "Deserializing Session [%s]", getSessionIdentifier() );
}
ois.defaultReadObject(); ois.defaultReadObject();