HHH-9268 The NoopOptimizer used for default id generation doesn't need synchronization

This commit is contained in:
Sanne Grinovero 2014-07-02 00:51:52 +02:00
parent 75aac4f3ab
commit 98771c247f
1 changed files with 9 additions and 8 deletions

View File

@ -31,13 +31,10 @@ import org.hibernate.id.IntegralDataTypeHolder;
* An optimizer that performs no optimization. The database is hit for * An optimizer that performs no optimization. The database is hit for
* every request. * every request.
* *
* @deprecated This is the fallback Optimizer chosen when we fail to instantiate one * Using this implementation is probably not the most efficient choice.
* of the proper implementations. Using this implementation is probably a performance
* problem.
*/ */
@Deprecated
public final class NoopOptimizer extends AbstractOptimizer { public final class NoopOptimizer extends AbstractOptimizer {
private volatile IntegralDataTypeHolder lastSourceValue; private IntegralDataTypeHolder lastSourceValue;
/** /**
* Constructs a NoopOptimizer * Constructs a NoopOptimizer
@ -50,9 +47,13 @@ public final class NoopOptimizer extends AbstractOptimizer {
} }
@Override @Override
public synchronized Serializable generate(AccessCallback callback) { public Serializable generate(AccessCallback callback) {
// IMPL NOTE : it is incredibly important that the method-local variable be used here to // IMPL NOTE : this method is called concurrently and is
// avoid concurrency issues. // not synchronized. It is very important to work on the
// local variable: the field lastSourceValue is not
// reliable as it might be mutated by multipled threads.
// The lastSourceValue field is only accessed by tests,
// so this is not a concern.
IntegralDataTypeHolder value = null; IntegralDataTypeHolder value = null;
while ( value == null || value.lt( 1 ) ) { while ( value == null || value.lt( 1 ) ) {
value = callback.getNextValue(); value = callback.getNextValue();