HHH-13654 Defer initialization of StatefulPersistenceContext#entitySnapshotsByKey

This commit is contained in:
Sanne Grinovero 2019-10-29 12:31:05 +00:00
parent f4bf11331b
commit 500819e169

View File

@ -157,8 +157,6 @@ public StatefulPersistenceContext(SharedSessionContractImplementor session) {
this.session = session; this.session = session;
entitiesByKey = new HashMap<>( INIT_COLL_SIZE ); entitiesByKey = new HashMap<>( INIT_COLL_SIZE );
entitySnapshotsByKey = new HashMap<>( INIT_COLL_SIZE );
entityEntryContext = new EntityEntryContext( this ); entityEntryContext = new EntityEntryContext( this );
} }
@ -245,7 +243,7 @@ public void clear() {
entitiesByUniqueKey = null; entitiesByUniqueKey = null;
entityEntryContext.clear(); entityEntryContext.clear();
parentsByChild = null; parentsByChild = null;
entitySnapshotsByKey.clear(); entitySnapshotsByKey = null;
collectionsByKey = null; collectionsByKey = null;
nonlazyCollections = null; nonlazyCollections = null;
collectionEntries = null; collectionEntries = null;
@ -306,12 +304,15 @@ public void afterTransactionCompletion() {
@Override @Override
public Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister) throws HibernateException { public Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister) throws HibernateException {
final EntityKey key = session.generateEntityKey( id, persister ); final EntityKey key = session.generateEntityKey( id, persister );
final Object cached = entitySnapshotsByKey.get( key ); final Object cached = entitySnapshotsByKey == null ? null : entitySnapshotsByKey.get( key );
if ( cached != null ) { if ( cached != null ) {
return cached == NO_ROW ? null : (Object[]) cached; return cached == NO_ROW ? null : (Object[]) cached;
} }
else { else {
final Object[] snapshot = persister.getDatabaseSnapshot( id, session ); final Object[] snapshot = persister.getDatabaseSnapshot( id, session );
if ( entitySnapshotsByKey == null ) {
entitySnapshotsByKey = new HashMap<>( INIT_COLL_SIZE );
}
entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot ); entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot );
return snapshot; return snapshot;
} }
@ -370,7 +371,7 @@ private EntityPersister locateProperPersister(EntityPersister persister) {
@Override @Override
public Object[] getCachedDatabaseSnapshot(EntityKey key) { public Object[] getCachedDatabaseSnapshot(EntityKey key) {
final Object snapshot = entitySnapshotsByKey.get( key ); final Object snapshot = entitySnapshotsByKey == null ? null : entitySnapshotsByKey.get( key );
if ( snapshot == NO_ROW ) { if ( snapshot == NO_ROW ) {
throw new IllegalStateException( throw new IllegalStateException(
"persistence context reported no row snapshot for " "persistence context reported no row snapshot for "
@ -412,7 +413,9 @@ public Object removeEntity(EntityKey key) {
// Clear all parent cache // Clear all parent cache
parentsByChild = null; parentsByChild = null;
entitySnapshotsByKey.remove( key ); if ( entitySnapshotsByKey != null ) {
entitySnapshotsByKey.remove( key );
}
if ( nullifiableEntityKeys != null ) { if ( nullifiableEntityKeys != null ) {
nullifiableEntityKeys.remove( key ); nullifiableEntityKeys.remove( key );
} }
@ -1567,13 +1570,18 @@ public void serialize(ObjectOutputStream oos) throws IOException {
} }
} }
oos.writeInt( entitySnapshotsByKey.size() ); if ( entitySnapshotsByKey == null ) {
if ( LOG.isTraceEnabled() ) { oos.writeInt( 0 );
LOG.trace( "Starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries" );
} }
for ( Map.Entry<EntityKey,Object> entry : entitySnapshotsByKey.entrySet() ) { else {
entry.getKey().serialize( oos ); oos.writeInt( entitySnapshotsByKey.size() );
oos.writeObject( entry.getValue() ); if ( LOG.isTraceEnabled() ) {
LOG.trace( "Starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries" );
}
for ( Map.Entry<EntityKey,Object> entry : entitySnapshotsByKey.entrySet() ) {
entry.getKey().serialize( oos );
oos.writeObject( entry.getValue() );
}
} }
entityEntryContext.serialize( oos ); entityEntryContext.serialize( oos );