HHH-8682 org.hibernate.engine.spi.EntityKey consumes a lot of memory

This commit is contained in:
Sanne Grinovero 2013-11-13 21:47:03 +00:00 committed by Steve Ebersole
parent 15adff22ce
commit 220a27af64
8 changed files with 207 additions and 78 deletions

View File

@ -0,0 +1,70 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.engine.internal;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.entity.EntityEssentials;
import org.hibernate.type.Type;
public class EssentialEntityPersisterDetails implements EntityEssentials {
private final Type identifierType;
private final boolean isBatchLoadable;
private final String entityName;
private final String rootEntityName;
public EssentialEntityPersisterDetails(Type identifierType, boolean isBatchLoadable, String entityName, String rootEntityName) {
this.identifierType = identifierType;
this.isBatchLoadable = isBatchLoadable;
this.entityName = entityName;
this.rootEntityName = rootEntityName;
}
@Override
public Type getIdentifierType() {
return identifierType;
}
@Override
public boolean isBatchLoadable() {
return isBatchLoadable;
}
@Override
public String getEntityName() {
return entityName;
}
@Override
public String getRootEntityName() {
return rootEntityName;
}
@Override
public SessionFactoryImplementor getFactory() {
return null;
}
}

View File

@ -1440,6 +1440,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
LOG.trace( "Serializing persistent-context" ); LOG.trace( "Serializing persistent-context" );
} }
final StatefulPersistenceContext rtn = new StatefulPersistenceContext( session ); final StatefulPersistenceContext rtn = new StatefulPersistenceContext( session );
SessionFactoryImplementor sfi = session==null ? null : session.getFactory();
// during deserialization, we need to reconnect all proxies and // during deserialization, we need to reconnect all proxies and
// collections to this session, as well as the EntityEntry and // collections to this session, as well as the EntityEntry and
@ -1457,7 +1458,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
} }
rtn.entitiesByKey = new HashMap<EntityKey,Object>( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); rtn.entitiesByKey = new HashMap<EntityKey,Object>( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
for ( int i = 0; i < count; i++ ) { for ( int i = 0; i < count; i++ ) {
rtn.entitiesByKey.put( EntityKey.deserialize( ois, session ), ois.readObject() ); rtn.entitiesByKey.put( EntityKey.deserialize( ois, sfi ), ois.readObject() );
} }
count = ois.readInt(); count = ois.readInt();
@ -1483,7 +1484,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
null null
); );
for ( int i = 0; i < count; i++ ) { for ( int i = 0; i < count; i++ ) {
final EntityKey ek = EntityKey.deserialize( ois, session ); final EntityKey ek = EntityKey.deserialize( ois, sfi );
final Object proxy = ois.readObject(); final Object proxy = ois.readObject();
if ( proxy instanceof HibernateProxy ) { if ( proxy instanceof HibernateProxy ) {
( (HibernateProxy) proxy ).getHibernateLazyInitializer().setSession( session ); ( (HibernateProxy) proxy ).getHibernateLazyInitializer().setSession( session );
@ -1503,7 +1504,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
} }
rtn.entitySnapshotsByKey = new HashMap<EntityKey,Object>( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count ); rtn.entitySnapshotsByKey = new HashMap<EntityKey,Object>( count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count );
for ( int i = 0; i < count; i++ ) { for ( int i = 0; i < count; i++ ) {
rtn.entitySnapshotsByKey.put( EntityKey.deserialize( ois, session ), ois.readObject() ); rtn.entitySnapshotsByKey.put( EntityKey.deserialize( ois, sfi ), ois.readObject() );
} }
rtn.entityEntryContext = EntityEntryContext.deserialize( ois, rtn ); rtn.entityEntryContext = EntityEntryContext.deserialize( ois, rtn );
@ -1544,7 +1545,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
} }
rtn.nullifiableEntityKeys = new HashSet<EntityKey>(); rtn.nullifiableEntityKeys = new HashSet<EntityKey>();
for ( int i = 0; i < count; i++ ) { for ( int i = 0; i < count; i++ ) {
rtn.nullifiableEntityKeys.add( EntityKey.deserialize( ois, session ) ); rtn.nullifiableEntityKeys.add( EntityKey.deserialize( ois, sfi ) );
} }
} }

View File

@ -194,7 +194,7 @@ public final class EntityEntry implements Serializable {
if ( getId() == null ) { if ( getId() == null ) {
throw new IllegalStateException( "cannot generate an EntityKey when id is null."); throw new IllegalStateException( "cannot generate an EntityKey when id is null.");
} }
cachedEntityKey = new EntityKey( getId(), getPersister(), tenantId ); cachedEntityKey = new EntityKey( getId(), getPersister() );
} }
return cachedEntityKey; return cachedEntityKey;
} }

View File

@ -1,7 +1,7 @@
/* /*
* Hibernate, Relational Persistence for Idiomatic Java * Hibernate, Relational Persistence for Idiomatic Java
* *
* Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as * Copyright (c) 2008-2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution * indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are * statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc. * distributed under license by Red Hat Inc.
@ -29,29 +29,31 @@ import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.engine.internal.EssentialEntityPersisterDetails;
import org.hibernate.internal.util.compare.EqualsHelper; import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.persister.entity.EntityEssentials;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper; import org.hibernate.pretty.MessageHelper;
import org.hibernate.type.Type; import org.hibernate.type.Type;
/** /**
* Uniquely identifies of an entity instance in a particular session by identifier. * Uniquely identifies of an entity instance in a particular Session by identifier.
* Note that it's only safe to be used within the scope of a Session: it doesn't consider for example the tenantId
* as part of the equality definition.
* <p/> * <p/>
* Information used to determine uniqueness consists of the entity-name and the identifier value (see {@link #equals}). * Information used to determine uniqueness consists of the entity-name and the identifier value (see {@link #equals}).
* <p/>
* Performance considerations: lots of instances of this type are created at runtime. Make sure each one is as small as possible
* by storing just the essential needed.
* *
* @author Gavin King * @author Gavin King
* @author Sanne Grinovero
*/ */
public final class EntityKey implements Serializable { public final class EntityKey implements Serializable {
private final Serializable identifier; private final Serializable identifier;
private final String entityName;
private final String rootEntityName;
private final String tenantId;
private final int hashCode; private final int hashCode;
private final EntityEssentials persister;
private final Type identifierType;
private final boolean isBatchLoadable;
private final SessionFactoryImplementor factory;
/** /**
* Construct a unique identifier for an entity class instance. * Construct a unique identifier for an entity class instance.
@ -62,61 +64,42 @@ public final class EntityKey implements Serializable {
* *
* @param id The entity id * @param id The entity id
* @param persister The entity persister * @param persister The entity persister
* @param tenantId The tenant identifier of the session to which this key belongs
*/ */
public EntityKey(Serializable id, EntityPersister persister, String tenantId) { public EntityKey(Serializable id, EntityPersister persister) {
this.persister = persister;
if ( id == null ) { if ( id == null ) {
throw new AssertionFailure( "null identifier" ); throw new AssertionFailure( "null identifier" );
} }
this.identifier = id; this.identifier = id;
this.rootEntityName = persister.getRootEntityName();
this.entityName = persister.getEntityName();
this.tenantId = tenantId;
this.identifierType = persister.getIdentifierType();
this.isBatchLoadable = persister.isBatchLoadable();
this.factory = persister.getFactory();
this.hashCode = generateHashCode(); this.hashCode = generateHashCode();
} }
/** /**
* Used to reconstruct an EntityKey during deserialization. * Used to reconstruct an EntityKey during deserialization. Note that this constructor
* is used only in very specific situations: the SessionFactory isn't actually available
* and so both equals and hashcode implementations can't be implemented correctly.
* *
* @param identifier The identifier value * @param identifier The identifier value
* @param rootEntityName The root entity name * @param fakePersister Is a placeholder for the EntityPersister, only providing essential methods needed for this purpose.
* @param entityName The specific entity name * @param hashCode The hashCode needs to be provided as it can't be calculated correctly without the SessionFactory.
* @param identifierType The type of the identifier value
* @param batchLoadable Whether represented entity is eligible for batch loading
* @param factory The session factory
* @param tenantId The entity's tenant id (from the session that loaded it).
*/ */
private EntityKey( private EntityKey(Serializable identifier, EntityEssentials fakePersister, int hashCode) {
Serializable identifier, this.persister = fakePersister;
String rootEntityName, if ( identifier == null ) {
String entityName, throw new AssertionFailure( "null identifier" );
Type identifierType, }
boolean batchLoadable,
SessionFactoryImplementor factory,
String tenantId) {
this.identifier = identifier; this.identifier = identifier;
this.rootEntityName = rootEntityName; this.hashCode = hashCode;
this.entityName = entityName;
this.identifierType = identifierType;
this.isBatchLoadable = batchLoadable;
this.factory = factory;
this.tenantId = tenantId;
this.hashCode = generateHashCode();
} }
private int generateHashCode() { private int generateHashCode() {
int result = 17; int result = 17;
result = 37 * result + rootEntityName.hashCode(); result = 37 * result + persister.getIdentifierType().getHashCode( identifier, persister.getFactory() );
result = 37 * result + identifierType.getHashCode( identifier, factory );
return result; return result;
} }
public boolean isBatchLoadable() { public boolean isBatchLoadable() {
return isBatchLoadable; return persister.isBatchLoadable();
} }
public Serializable getIdentifier() { public Serializable getIdentifier() {
@ -124,7 +107,7 @@ public final class EntityKey implements Serializable {
} }
public String getEntityName() { public String getEntityName() {
return entityName; return persister.getEntityName();
} }
@Override @Override
@ -132,14 +115,27 @@ public final class EntityKey implements Serializable {
if ( this == other ) { if ( this == other ) {
return true; return true;
} }
if ( other == null || getClass() != other.getClass() ) { if ( other == null || EntityKey.class != other.getClass() ) {
return false; return false;
} }
final EntityKey otherKey = (EntityKey) other; final EntityKey otherKey = (EntityKey) other;
return otherKey.rootEntityName.equals( this.rootEntityName ) return samePersistentType( otherKey )
&& identifierType.isEqual( otherKey.identifier, this.identifier, factory ) && sameIdentifier( otherKey );
&& EqualsHelper.equals( tenantId, otherKey.tenantId );
}
private boolean sameIdentifier(final EntityKey otherKey) {
return persister.getIdentifierType().isEqual( otherKey.identifier, this.identifier, persister.getFactory() );
}
private boolean samePersistentType(final EntityKey otherKey) {
if ( otherKey.persister == persister ) {
return true;
}
else {
return EqualsHelper.equals( otherKey.persister.getRootEntityName(), persister.getRootEntityName() );
}
} }
@Override @Override
@ -150,7 +146,7 @@ public final class EntityKey implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "EntityKey" + return "EntityKey" +
MessageHelper.infoString( factory.getEntityPersister( entityName ), identifier, factory ); MessageHelper.infoString( this.persister, identifier, persister.getFactory() );
} }
/** /**
@ -162,12 +158,12 @@ public final class EntityKey implements Serializable {
* @throws IOException Thrown by Java I/O * @throws IOException Thrown by Java I/O
*/ */
public void serialize(ObjectOutputStream oos) throws IOException { public void serialize(ObjectOutputStream oos) throws IOException {
oos.writeObject( persister.getIdentifierType() );
oos.writeBoolean( isBatchLoadable() );
oos.writeObject( identifier ); oos.writeObject( identifier );
oos.writeObject( rootEntityName ); oos.writeObject( persister.getEntityName() );
oos.writeObject( entityName ); oos.writeObject( persister.getRootEntityName() );
oos.writeObject( identifierType ); oos.writeInt( hashCode );
oos.writeBoolean( isBatchLoadable );
oos.writeObject( tenantId );
} }
/** /**
@ -175,24 +171,28 @@ public final class EntityKey implements Serializable {
* Session/PersistenceContext for increased performance. * Session/PersistenceContext for increased performance.
* *
* @param ois The stream from which to read the entry. * @param ois The stream from which to read the entry.
* @param session The session being deserialized. * @param sessionFactory The SessionFactory owning the Session being deserialized.
* *
* @return The deserialized EntityEntry * @return The deserialized EntityEntry
* *
* @throws IOException Thrown by Java I/O * @throws IOException Thrown by Java I/O
* @throws ClassNotFoundException Thrown by Java I/O * @throws ClassNotFoundException Thrown by Java I/O
*/ */
public static EntityKey deserialize( public static EntityKey deserialize(ObjectInputStream ois, SessionFactoryImplementor sessionFactory) throws IOException, ClassNotFoundException {
ObjectInputStream ois, final Type identifierType = (Type) ois.readObject();
SessionImplementor session) throws IOException, ClassNotFoundException { final boolean isBatchLoadable = ois.readBoolean();
return new EntityKey( final Serializable id = (Serializable) ois.readObject();
(Serializable) ois.readObject(), final String entityName = (String) ois.readObject();
(String) ois.readObject(), final String rootEntityName = (String) ois.readObject();
(String) ois.readObject(), final int hashCode = ois.readInt();
(Type) ois.readObject(), if ( sessionFactory != null) {
ois.readBoolean(), final EntityPersister entityPersister = sessionFactory.getEntityPersister( entityName );
(session == null ? null : session.getFactory()), return new EntityKey(id, entityPersister);
(String) ois.readObject() }
); else {
//This version will produce an EntityKey which is technically unable to satisfy the equals contract!
final EntityEssentials fakePersister = new EssentialEntityPersisterDetails(identifierType, isBatchLoadable, entityName, rootEntityName);
return new EntityKey(id, fakePersister, hashCode);
}
} }
} }

View File

@ -324,7 +324,7 @@ public abstract class AbstractSessionImpl
@Override @Override
public EntityKey generateEntityKey(Serializable id, EntityPersister persister) { public EntityKey generateEntityKey(Serializable id, EntityPersister persister) {
return new EntityKey( id, persister, getTenantIdentifier() ); return new EntityKey( id, persister );
} }
@Override @Override

View File

@ -0,0 +1,52 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.persister.entity;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.Type;
/**
* This the internal contract exposed by EntityPersister to EntityKey instances.
* The purpose is to keep the number of fields for EntityKey to a minimum amount
* and still be able to set it the properties listed here without having to create
* a complete EntityPersister implementation.
*
* @see org.hibernate.persister.entity.EntityPersister
* @see org.hibernate.engine.spi.EntityKey
*
* @author Sanne Grinovero
*/
public interface EntityEssentials {
Type getIdentifierType();
boolean isBatchLoadable();
String getEntityName();
String getRootEntityName();
SessionFactoryImplementor getFactory();
}

View File

@ -63,7 +63,7 @@ import org.hibernate.type.VersionType;
* @see org.hibernate.persister.spi.PersisterFactory * @see org.hibernate.persister.spi.PersisterFactory
* @see org.hibernate.persister.spi.PersisterClassResolver * @see org.hibernate.persister.spi.PersisterClassResolver
*/ */
public interface EntityPersister extends OptimisticCacheSource, EntityDefinition { public interface EntityPersister extends OptimisticCacheSource, EntityDefinition, EntityEssentials {
/** /**
* The property name of the "special" identifier property in HQL * The property name of the "special" identifier property in HQL

View File

@ -29,6 +29,7 @@ import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityEssentials;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.Type; import org.hibernate.type.Type;
@ -82,11 +83,11 @@ public final class MessageHelper {
* *
* @param persister The persister for the entity * @param persister The persister for the entity
* @param id The entity id value * @param id The entity id value
* @param factory The session factory * @param factory The session factory - Could be null!
* @return An info string, in the form [FooBar#1] * @return An info string, in the form [FooBar#1]
*/ */
public static String infoString( public static String infoString(
EntityPersister persister, EntityEssentials persister,
Object id, Object id,
SessionFactoryImplementor factory) { SessionFactoryImplementor factory) {
StringBuilder s = new StringBuilder(); StringBuilder s = new StringBuilder();
@ -110,7 +111,12 @@ public final class MessageHelper {
s.append( id ); s.append( id );
} }
else { else {
s.append( idType.toLoggableString( id, factory ) ); if ( factory != null ) {
s.append( idType.toLoggableString( id, factory ) );
}
else {
s.append( "<not loggable>" );
}
} }
} }
s.append( ']' ); s.append( ']' );