From c8b68f58839cfc3f0317de5f7b5a26dbf407344c Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Thu, 26 Nov 2015 16:11:47 +0000 Subject: [PATCH] HHH-10302 Extract helper method for LoadEvent reuse and adapt code formatting to Hibernate style --- .../org/hibernate/event/spi/LoadEvent.java | 2 +- .../org/hibernate/internal/SessionImpl.java | 86 +++++++++---------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/event/spi/LoadEvent.java b/hibernate-core/src/main/java/org/hibernate/event/spi/LoadEvent.java index 017f23e89a..ed186ab395 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/spi/LoadEvent.java +++ b/hibernate-core/src/main/java/org/hibernate/event/spi/LoadEvent.java @@ -105,7 +105,7 @@ public class LoadEvent extends AbstractEvent { this.instanceToLoad = instanceToLoad; this.lockOptions = lockOptions; this.isAssociationFetch = isAssociationFetch; - this.postLoadEvent = new PostLoadEvent(source); + this.postLoadEvent = new PostLoadEvent( source ); } public Serializable getEntityId() { diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 1f5ac20de0..234557ddc8 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -939,22 +939,22 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc public void load(Object object, Serializable id) throws HibernateException { LoadEvent event = loadEvent; loadEvent = null; - if(event == null) { + if ( event == null ) { event = new LoadEvent( id, object, this ); } else { - event.setEntityClassName(null); - event.setEntityId(id); - event.setInstanceToLoad(object); - event.setLockMode(LoadEvent.DEFAULT_LOCK_MODE); - event.setLockScope(LoadEvent.DEFAULT_LOCK_OPTIONS.getScope()); - event.setLockTimeout(LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut()); + event.setEntityClassName( null ); + event.setEntityId( id ); + event.setInstanceToLoad( object ); + event.setLockMode( LoadEvent.DEFAULT_LOCK_MODE ); + event.setLockScope( LoadEvent.DEFAULT_LOCK_OPTIONS.getScope() ); + event.setLockTimeout( LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut() ); } fireLoad( event, LoadEventListener.RELOAD ); - if(loadEvent == null) { - event.setEntityClassName(null); - event.setEntityId(null); - event.setInstanceToLoad(null); - event.setResult(null); + if ( loadEvent == null ) { + event.setEntityClassName( null ); + event.setEntityId( null ); + event.setInstanceToLoad( null ); + event.setResult( null ); loadEvent = event; } } @@ -992,23 +992,14 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc } LoadEvent event = loadEvent; loadEvent = null; - if(event == null) { - event = new LoadEvent( id, entityName, true, this ); - } else { - event.setEntityClassName(entityName); - event.setEntityId(id); - event.setInstanceToLoad(null); - event.setLockMode(LoadEvent.DEFAULT_LOCK_MODE); - event.setLockScope(LoadEvent.DEFAULT_LOCK_OPTIONS.getScope()); - event.setLockTimeout(LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut()); - } + event = recycleEventInstance( event, id, entityName ); fireLoad( event, LoadEventListener.IMMEDIATE_LOAD ); Object result = event.getResult(); - if(loadEvent == null) { - event.setEntityClassName(null); - event.setEntityId(null); - event.setInstanceToLoad(null); - event.setResult(null); + if ( loadEvent == null ) { + event.setEntityClassName( null ); + event.setEntityId( null ); + event.setInstanceToLoad( null ); + event.setResult( null ); loadEvent = event; } return result; @@ -1026,31 +1017,40 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc LoadEvent event = loadEvent; loadEvent = null; - if(event == null) { - event = new LoadEvent( id, entityName, true, this ); - } else { - event.setEntityClassName(entityName); - event.setEntityId(id); - event.setInstanceToLoad(null); - event.setLockMode(LoadEvent.DEFAULT_LOCK_MODE); - event.setLockScope(LoadEvent.DEFAULT_LOCK_OPTIONS.getScope()); - event.setLockTimeout(LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut()); - } + event = recycleEventInstance( event, id, entityName ); fireLoad( event, type ); Object result = event.getResult(); if ( !nullable ) { - UnresolvableObjectException.throwIfNull(result, id, entityName ); + UnresolvableObjectException.throwIfNull( result, id, entityName ); } - if(loadEvent == null) { - event.setEntityClassName(null); - event.setEntityId(null); - event.setInstanceToLoad(null); - event.setResult(null); + if ( loadEvent == null ) { + event.setEntityClassName( null ); + event.setEntityId( null ); + event.setInstanceToLoad( null ); + event.setResult( null ); loadEvent = event; } return result; } + /** + * Helper to avoid creating many new instances of LoadEvent: it's an allocation hot spot. + */ + private LoadEvent recycleEventInstance(final LoadEvent event, final Serializable id, final String entityName) { + if ( event == null ) { + return new LoadEvent( id, entityName, true, this ); + } + else { + event.setEntityClassName( entityName ); + event.setEntityId( id ); + event.setInstanceToLoad( null ); + event.setLockMode( LoadEvent.DEFAULT_LOCK_MODE ); + event.setLockScope( LoadEvent.DEFAULT_LOCK_OPTIONS.getScope() ); + event.setLockTimeout( LoadEvent.DEFAULT_LOCK_OPTIONS.getTimeOut() ); + return event; + } + } + @Override public T load(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException { return this.byId( entityClass ).with( new LockOptions( lockMode ) ).getReference( id );