HHH-15502 Improve NoCachingRegionFactory instantiation performance: Refactored NoCachingTransactionSynchronizationImpl to avoid timestamp creation, Renamed CacheTransactionSynchronization#getCurrentTransactionStartTimestamp method to getCachingTimestamp and removed SharedSessionContractImplementor#getTransactionStartTimestamp method

This commit is contained in:
Andrea Boriero 2022-09-09 15:46:17 +02:00
parent 3dec1ca4dc
commit 6bf0b0ae41
10 changed files with 37 additions and 35 deletions

View File

@ -69,7 +69,7 @@ public class NoCachingRegionFactory implements RegionFactory {
@Override
public CacheTransactionSynchronization createTransactionContext(SharedSessionContractImplementor session) {
return new NoCachingTransactionSynchronizationImpl( this );
return NoCachingTransactionSynchronizationImpl.INSTANCE;
}
@Override

View File

@ -6,14 +6,35 @@
*/
package org.hibernate.cache.internal;
import org.hibernate.cache.spi.AbstractCacheTransactionSynchronization;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.CacheTransactionSynchronization;
/**
* @author Steve Ebersole
*/
public class NoCachingTransactionSynchronizationImpl extends AbstractCacheTransactionSynchronization {
public NoCachingTransactionSynchronizationImpl(RegionFactory regionFactory) {
super( regionFactory );
public class NoCachingTransactionSynchronizationImpl implements CacheTransactionSynchronization {
public static final NoCachingTransactionSynchronizationImpl INSTANCE = new NoCachingTransactionSynchronizationImpl();
private NoCachingTransactionSynchronizationImpl() {
}
@Override
public long getCachingTimestamp() {
throw new UnsupportedOperationException("Method not supported when 2LC is not enabled");
}
@Override
public void transactionJoined() {
}
@Override
public void transactionCompleting() {
}
@Override
public void transactionCompleted(boolean successful) {
}
}

View File

@ -15,7 +15,6 @@ import org.hibernate.HibernateException;
import org.hibernate.cache.spi.QueryKey;
import org.hibernate.cache.spi.QueryResultsCache;
import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.SecondLevelCacheLogger;
import org.hibernate.cache.spi.TimestampsCache;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -53,11 +52,11 @@ public class QueryResultsCacheImpl implements QueryResultsCache {
final List<?> results,
final SharedSessionContractImplementor session) throws HibernateException {
if ( DEBUG_ENABLED ) {
L2CACHE_LOGGER.debugf( "Caching query results in region: %s; timestamp=%s", cacheRegion.getName(), session.getTransactionStartTimestamp() );
L2CACHE_LOGGER.debugf( "Caching query results in region: %s; timestamp=%s", cacheRegion.getName(), session.getCacheTransactionSynchronization().getCachingTimestamp() );
}
final CacheItem cacheItem = new CacheItem(
session.getTransactionStartTimestamp(),
session.getCacheTransactionSynchronization().getCachingTimestamp(),
deepCopy( results )
);

View File

@ -21,7 +21,7 @@ public abstract class AbstractCacheTransactionSynchronization implements CacheTr
}
@Override
public long getCurrentTransactionStartTimestamp() {
public long getCachingTimestamp() {
return lastTransactionCompletionTimestamp;
}

View File

@ -48,8 +48,10 @@ public interface CacheTransactionSynchronization {
*
* @implSpec This "timestamp" need not be related to timestamp in the Java
* Date/millisecond sense. It just needs to be an incrementing value.
*
* An UnsupportedOperationException is thrown if 2LC has not enabled
*/
long getCurrentTransactionStartTimestamp();
long getCachingTimestamp();
/**
* Callback that owning Session has become joined to a resource transaction.

View File

@ -15,7 +15,6 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.hibernate.cache.spi.DomainDataRegion;
import org.hibernate.cache.spi.SecondLevelCacheLogger;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -75,7 +74,7 @@ public abstract class AbstractReadWriteAccess extends AbstractCachedDomainDataAc
return null;
}
boolean readable = item.isReadable( session.getTransactionStartTimestamp() );
boolean readable = item.isReadable( session.getCacheTransactionSynchronization().getCachingTimestamp() );
if ( readable ) {
log.debugf( "Cache hit : region = `%s`, key = `%s`", getRegion().getName(), key );
return item.getValue();
@ -101,11 +100,11 @@ public abstract class AbstractReadWriteAccess extends AbstractCachedDomainDataAc
writeLock.lock();
Lockable item = (Lockable) getStorageAccess().getFromCache( key, session );
boolean writable = item == null || item.isWriteable( session.getTransactionStartTimestamp(), version, getVersionComparator() );
boolean writable = item == null || item.isWriteable( session.getCacheTransactionSynchronization().getCachingTimestamp(), version, getVersionComparator() );
if ( writable ) {
getStorageAccess().putIntoCache(
key,
new Item( value, version, session.getTransactionStartTimestamp() ),
new Item( value, version, session.getCacheTransactionSynchronization().getCachingTimestamp() ),
session
);
return true;

View File

@ -235,11 +235,6 @@ public class SessionDelegatorBaseImpl implements SessionImplementor {
delegate.markForRollbackOnly();
}
@Override
public long getTransactionStartTimestamp() {
return delegate.getTransactionStartTimestamp();
}
@Override
public FlushModeType getFlushMode() {
return delegate.getFlushMode();

View File

@ -183,15 +183,6 @@ public interface SharedSessionContractImplementor
*/
void markForRollbackOnly();
/**
* A "timestamp" at or before the start of the current transaction.
*
* @apiNote This "timestamp" need not be related to timestamp in the Java Date/millisecond
* sense. It just needs to be an incrementing value. See
* {@link CacheTransactionSynchronization#getCurrentTransactionStartTimestamp()}
*/
long getTransactionStartTimestamp();
/**
* The current CacheTransactionContext associated with the Session. This may
* return {@code null} when the Session is not currently part of a transaction.

View File

@ -497,11 +497,6 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
return cacheTransactionSync;
}
@Override
public long getTransactionStartTimestamp() {
return getCacheTransactionSynchronization().getCurrentTransactionStartTimestamp();
}
@Override
public Transaction beginTransaction() {
checkOpen();

View File

@ -274,7 +274,7 @@ public class SessionImpl
}
if ( log.isTraceEnabled() ) {
log.tracef( "Opened Session [%s] at timestamp: %s", getSessionIdentifier(), getTransactionStartTimestamp() );
log.tracef( "Opened Session [%s] at timestamp: %s", getSessionIdentifier(), System.currentTimeMillis() );
}
}