Skip to content

Commit

Permalink
HHH-13654 Defer initialization of StatefulPersistenceContext#entitySn…
Browse files Browse the repository at this point in the history
…apshotsByKey
  • Loading branch information
Sanne committed Oct 30, 2019
1 parent f4bf113 commit 500819e
Showing 1 changed file with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ public StatefulPersistenceContext(SharedSessionContractImplementor session) {
this.session = session;

entitiesByKey = new HashMap<>( INIT_COLL_SIZE );
entitySnapshotsByKey = new HashMap<>( INIT_COLL_SIZE );

entityEntryContext = new EntityEntryContext( this );
}

Expand Down Expand Up @@ -245,7 +243,7 @@ public void clear() {
entitiesByUniqueKey = null;
entityEntryContext.clear();
parentsByChild = null;
entitySnapshotsByKey.clear();
entitySnapshotsByKey = null;
collectionsByKey = null;
nonlazyCollections = null;
collectionEntries = null;
Expand Down Expand Up @@ -306,12 +304,15 @@ public void afterTransactionCompletion() {
@Override
public Object[] getDatabaseSnapshot(Serializable id, EntityPersister persister) throws HibernateException {
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 ) {
return cached == NO_ROW ? null : (Object[]) cached;
}
else {
final Object[] snapshot = persister.getDatabaseSnapshot( id, session );
if ( entitySnapshotsByKey == null ) {
entitySnapshotsByKey = new HashMap<>( INIT_COLL_SIZE );
}
entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot );
return snapshot;
}
Expand Down Expand Up @@ -370,7 +371,7 @@ private EntityPersister locateProperPersister(EntityPersister persister) {

@Override
public Object[] getCachedDatabaseSnapshot(EntityKey key) {
final Object snapshot = entitySnapshotsByKey.get( key );
final Object snapshot = entitySnapshotsByKey == null ? null : entitySnapshotsByKey.get( key );
if ( snapshot == NO_ROW ) {
throw new IllegalStateException(
"persistence context reported no row snapshot for "
Expand Down Expand Up @@ -412,7 +413,9 @@ public Object removeEntity(EntityKey key) {

// Clear all parent cache
parentsByChild = null;
entitySnapshotsByKey.remove( key );
if ( entitySnapshotsByKey != null ) {
entitySnapshotsByKey.remove( key );
}
if ( nullifiableEntityKeys != null ) {
nullifiableEntityKeys.remove( key );
}
Expand Down Expand Up @@ -1567,13 +1570,18 @@ public void serialize(ObjectOutputStream oos) throws IOException {
}
}

oos.writeInt( entitySnapshotsByKey.size() );
if ( LOG.isTraceEnabled() ) {
LOG.trace( "Starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries" );
if ( entitySnapshotsByKey == null ) {
oos.writeInt( 0 );
}
for ( Map.Entry<EntityKey,Object> entry : entitySnapshotsByKey.entrySet() ) {
entry.getKey().serialize( oos );
oos.writeObject( entry.getValue() );
else {
oos.writeInt( entitySnapshotsByKey.size() );
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 );
Expand Down

0 comments on commit 500819e

Please sign in to comment.