From 61a75accf5a6e8d20444726a46b52a211f0bc9e9 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Wed, 13 Nov 2013 14:10:37 +0000 Subject: [PATCH] HHH-8680 Relax synchronized block in TableGenerator#generate --- .../java/org/hibernate/id/enhanced/NoopOptimizer.java | 11 ++++++++--- .../java/org/hibernate/id/enhanced/Optimizer.java | 3 +++ .../org/hibernate/id/enhanced/TableGenerator.java | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/NoopOptimizer.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/NoopOptimizer.java index aba217aa91..8e748a3ee8 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/NoopOptimizer.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/NoopOptimizer.java @@ -30,9 +30,14 @@ import org.hibernate.id.IntegralDataTypeHolder; /** * An optimizer that performs no optimization. The database is hit for * every request. + * + * @deprecated This is the fallback Optimizer chosen when we fail to instantiate one + * of the proper implementations. Using this implementation is probably a performance + * problem. */ -public class NoopOptimizer extends AbstractOptimizer { - private IntegralDataTypeHolder lastSourceValue; +@Deprecated +public final class NoopOptimizer extends AbstractOptimizer { + private volatile IntegralDataTypeHolder lastSourceValue; /** * Constructs a NoopOptimizer @@ -45,7 +50,7 @@ public class NoopOptimizer extends AbstractOptimizer { } @Override - public Serializable generate(AccessCallback callback) { + public synchronized Serializable generate(AccessCallback callback) { // IMPL NOTE : it is incredibly important that the method-local variable be used here to // avoid concurrency issues. IntegralDataTypeHolder value = null; diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/Optimizer.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/Optimizer.java index e161739ca5..b9660c4815 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/Optimizer.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/Optimizer.java @@ -44,6 +44,9 @@ public interface Optimizer { /** * Generate an identifier value accounting for this specific optimization. * + * All known implementors are synchronized. Consider carefully if a new + * implementation could drop this requirement. + * * @param callback Callback to access the underlying value source. * @return The generated identifier value. */ diff --git a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java index 8f5b0b7fbe..6847a1a3af 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java @@ -525,7 +525,7 @@ public class TableGenerator implements PersistentIdentifierGenerator, Configurab } @Override - public synchronized Serializable generate(final SessionImplementor session, Object obj) { + public Serializable generate(final SessionImplementor session, final Object obj) { final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry() .getService( JdbcServices.class ) .getSqlStatementLogger();