Skip to content

Commit

Permalink
HHH-8954 Avoid LockOptions allocation in LoadEvent unless strictly re…
Browse files Browse the repository at this point in the history
…quired
  • Loading branch information
Sanne authored and brmeyer committed Feb 18, 2014
1 parent 539866b commit 7dc8d9c
Showing 1 changed file with 43 additions and 6 deletions.
Expand Up @@ -25,6 +25,7 @@

import java.io.Serializable;

import org.hibernate.AssertionFailure;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;

Expand All @@ -35,6 +36,25 @@
*/
public class LoadEvent extends AbstractEvent {
public static final LockMode DEFAULT_LOCK_MODE = LockMode.NONE;
//Performance hotspot: avoid allocating unneeded LockOptions
public static final LockOptions DEFAULT_LOCK_OPTIONS = new LockOptions() {
@Override
public LockOptions setLockMode(LockMode lockMode) {
throw new AssertionFailure( "Should not be invoked: DEFAULT_LOCK_OPTIONS needs to be treated as immutable." );
}
@Override
public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) {
throw new AssertionFailure( "Should not be invoked: DEFAULT_LOCK_OPTIONS needs to be treated as immutable." );
}
@Override
public LockOptions setTimeOut(int timeout) {
throw new AssertionFailure( "Should not be invoked: DEFAULT_LOCK_OPTIONS needs to be treated as immutable." );
}
@Override
public LockOptions setScope(boolean scope) {
throw new AssertionFailure( "Should not be invoked: DEFAULT_LOCK_OPTIONS needs to be treated as immutable." );
}
};

private Serializable entityId;
private String entityClassName;
Expand All @@ -44,7 +64,7 @@ public class LoadEvent extends AbstractEvent {
private Object result;

public LoadEvent(Serializable entityId, Object instanceToLoad, EventSource source) {
this(entityId, null, instanceToLoad, new LockOptions(), false, source);
this(entityId, null, instanceToLoad, DEFAULT_LOCK_OPTIONS, false, source);
}

public LoadEvent(Serializable entityId, String entityClassName, LockMode lockMode, EventSource source) {
Expand All @@ -56,7 +76,7 @@ public LoadEvent(Serializable entityId, String entityClassName, LockOptions lock
}

public LoadEvent(Serializable entityId, String entityClassName, boolean isAssociationFetch, EventSource source) {
this(entityId, entityClassName, null, new LockOptions(), isAssociationFetch, source);
this(entityId, entityClassName, null, DEFAULT_LOCK_OPTIONS, isAssociationFetch, source);
}

public boolean isAssociationFetch() {
Expand All @@ -70,7 +90,9 @@ private LoadEvent(
LockMode lockMode,
boolean isAssociationFetch,
EventSource source) {
this(entityId, entityClassName, instanceToLoad, new LockOptions().setLockMode(lockMode), isAssociationFetch, source );
this(entityId, entityClassName, instanceToLoad,
lockMode == DEFAULT_LOCK_MODE ? DEFAULT_LOCK_OPTIONS : new LockOptions().setLockMode(lockMode),
isAssociationFetch, source );
}

private LoadEvent(
Expand Down Expand Up @@ -134,19 +156,34 @@ public LockMode getLockMode() {
}

public void setLockMode(LockMode lockMode) {
this.lockOptions.setLockMode(lockMode);
if ( lockMode != lockOptions.getLockMode() ) {
writingOnLockOptions();
this.lockOptions.setLockMode(lockMode);
}
}

private void writingOnLockOptions() {
if ( lockOptions == DEFAULT_LOCK_OPTIONS ) {
this.lockOptions = new LockOptions();
}
}

public void setLockTimeout(int timeout) {
this.lockOptions.setTimeOut(timeout);
if ( timeout != lockOptions.getTimeOut() ) {
writingOnLockOptions();
this.lockOptions.setTimeOut(timeout);
}
}

public int getLockTimeout() {
return this.lockOptions.getTimeOut();
}

public void setLockScope(boolean cascade) {
this.lockOptions.setScope(cascade);
if ( lockOptions.getScope() != cascade ) {
writingOnLockOptions();
this.lockOptions.setScope(cascade);
}
}

public boolean getLockScope() {
Expand Down

0 comments on commit 7dc8d9c

Please sign in to comment.