Skip to content

Commit e418401

Browse files
committed
reset the optimizer after truncating tables or resynchronizing sequences
1 parent 3470e83 commit e418401

File tree

11 files changed

+56
-8
lines changed

11 files changed

+56
-8
lines changed

hibernate-core/src/main/java/org/hibernate/id/enhanced/HiLoOptimizer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ else if ( ! generationState.upperLimit.gt( generationState.value ) ) {
119119
private GenerationState noTenantState;
120120
private Map<String,GenerationState> tenantSpecificState;
121121

122+
@Override
123+
public void reset() {
124+
noTenantState = null;
125+
tenantSpecificState = null;
126+
}
127+
122128
private GenerationState locateGenerationState(String tenantId) {
123129
if ( tenantId == null ) {
124130
if ( noTenantState == null ) {

hibernate-core/src/main/java/org/hibernate/id/enhanced/LegacyHiLoAlgorithmOptimizer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public Serializable generate(AccessCallback callback) {
8080
private GenerationState noTenantState;
8181
private Map<String,GenerationState> tenantSpecificState;
8282

83+
@Override
84+
public void reset() {
85+
noTenantState = null;
86+
tenantSpecificState = null;
87+
}
88+
8389
private GenerationState locateGenerationState(String tenantIdentifier) {
8490
if ( tenantIdentifier == null ) {
8591
if ( noTenantState == null ) {

hibernate-core/src/main/java/org/hibernate/id/enhanced/NoopOptimizer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public NoopOptimizer(Class<?> returnClass, int incrementSize) {
3131

3232
@Override
3333
public Serializable generate(AccessCallback callback) {
34-
// IMPL NOTE : this method is called concurrently and is
34+
// IMPL NOTE: this method is called concurrently and is
3535
// not synchronized. It is very important to work on the
3636
// local variable: the field lastSourceValue is not
3737
// reliable as it might be mutated by multiple threads.
@@ -59,4 +59,9 @@ public boolean applyIncrementSizeToSourceValues() {
5959
public Expression createLowValueExpression(Expression databaseValue, SessionFactoryImplementor sessionFactory) {
6060
return databaseValue;
6161
}
62+
63+
@Override
64+
public void reset() {
65+
lastSourceValue = null;
66+
}
6267
}

hibernate-core/src/main/java/org/hibernate/id/enhanced/Optimizer.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
import java.io.Serializable;
1212

1313
/**
14-
* Performs optimization on an optimizable identifier generator. Typically
14+
* Performs optimization on an optimizable identifier generator. Typically.
1515
* this optimization takes the form of trying to ensure we do not have to
1616
* hit the database on each and every request to get an identifier value.
1717
* <p>
18-
* Optimizers work on constructor injection. They should provide
19-
* a constructor with the following arguments <ol>
20-
* <li>java.lang.Class - The return type for the generated values</li>
21-
* <li>int - The increment size</li>
18+
* Optimizers work on constructor injection. They should provide a
19+
* constructor accepting the following arguments:
20+
* <ol>
21+
* <li>{@code java.lang.Class} - The return type for the generated values</li>
22+
* <li>{@code int} - The increment size</li>
2223
* </ol>
2324
*
2425
* @author Steve Ebersole
@@ -35,6 +36,13 @@ public interface Optimizer {
3536
*/
3637
Serializable generate(AccessCallback callback);
3738

39+
/**
40+
* Reset the optimizer before restarting the underlying database sequence.
41+
*
42+
* @since 7.2
43+
*/
44+
void reset();
45+
3846
/**
3947
* A common means to access the last value obtained from the underlying
4048
* source. This is intended for testing purposes, since accessing the

hibernate-core/src/main/java/org/hibernate/id/enhanced/PooledLoOptimizer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public Serializable generate(AccessCallback callback) {
7878
private GenerationState noTenantState;
7979
private Map<String,GenerationState> tenantSpecificState;
8080

81+
@Override
82+
public void reset() {
83+
noTenantState = null;
84+
tenantSpecificState = null;
85+
}
86+
8187
private GenerationState locateGenerationState(String tenantIdentifier) {
8288
if ( tenantIdentifier == null ) {
8389
if ( noTenantState == null ) {

hibernate-core/src/main/java/org/hibernate/id/enhanced/PooledLoThreadLocalOptimizer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ private GenerationState locateGenerationState(String tenantIdentifier) {
6464
}
6565
}
6666

67+
@Override
68+
public void reset() {
69+
singleTenantState.remove();
70+
multiTenantStates.remove();
71+
}
72+
6773
// for Hibernate testsuite use only
6874
private GenerationState noTenantGenerationState() {
6975
final var noTenantState = locateGenerationState( null );

hibernate-core/src/main/java/org/hibernate/id/enhanced/PooledOptimizer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public PooledOptimizer(Class<?> returnClass, int incrementSize) {
5656
OPTIMIZER_MESSAGE_LOGGER.creatingPooledOptimizer( incrementSize, returnClass.getName() );
5757
}
5858

59-
6059
@Override
6160
public Serializable generate(AccessCallback callback) {
6261
lock.lock();
@@ -99,6 +98,12 @@ else if ( generationState.value.gt( generationState.hiValue ) ) {
9998
private GenerationState noTenantState;
10099
private Map<String,GenerationState> tenantSpecificState;
101100

101+
@Override
102+
public void reset() {
103+
noTenantState = null;
104+
tenantSpecificState = null;
105+
}
106+
102107
private GenerationState locateGenerationState(String tenantIdentifier) {
103108
if ( tenantIdentifier == null ) {
104109
if ( noTenantState == null ) {

hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStructure.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,12 @@ public void registerExtraExportables(Table table, Optimizer optimizer) {
179179
final long max = getMaxPrimaryKey( isolator, primaryKeyColumnName, tableName );
180180
final long current = getNextSequenceValue( isolator, sequenceName);
181181
final long startWith = Math.max( max + adjustment, current );
182+
optimizer.reset();
182183
return new InitCommand( sqlContext.getDialect().getSequenceSupport()
183184
.getRestartSequenceString( sequenceName, startWith ) );
184185
} );
185186
table.addResetCommand( sqlContext -> {
187+
optimizer.reset();
186188
final String sequenceName = sqlContext.format( physicalSequenceName );
187189
return new InitCommand( sqlContext.getDialect().getSequenceSupport()
188190
.getRestartSequenceString( sequenceName, initialValue ) );

hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ protected OptimizerDescriptor determineOptimizationStrategy(Properties params, i
445445
* selected optimizer. This is the hook to achieve that.
446446
*
447447
* @param optimizationStrategy The optimizer strategy (name)
448-
* @param incrementSize The {@link #determineIncrementSize determined increment size}
448+
* @param incrementSize The {@linkplain #determineIncrementSize determined increment size}
449449
* @return The adjusted increment size.
450450
*/
451451
protected int determineAdjustedIncrementSize(OptimizerDescriptor optimizationStrategy, int incrementSize) {

hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ private InitCommand generateResyncCommand(SqlStringGenerationContext context, Dd
716716
getCurrentTableValue( isolator, sequenceTableName, valueColumnName,
717717
segmentColumnName, segmentValue );
718718
if ( max + adjustment > current ) {
719+
optimizer.reset();
719720
final String update =
720721
"update " + sequenceTableName
721722
+ " set " + valueColumnName + " = " + (max + adjustment)
@@ -728,6 +729,7 @@ private InitCommand generateResyncCommand(SqlStringGenerationContext context, Dd
728729
}
729730

730731
private InitCommand generateResetCommand(SqlStringGenerationContext context) {
732+
optimizer.reset();
731733
final String update =
732734
"update " + context.format( physicalTableName )
733735
+ " set " + valueColumnName + " = " + initialValue

0 commit comments

Comments
 (0)