Skip to content

Commit

Permalink
Fix race in Column.updateSequenceIfRequired()
Browse files Browse the repository at this point in the history
  • Loading branch information
katzyn committed Dec 21, 2023
1 parent 372ea5f commit f892858
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions h2/src/main/org/h2/table/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,25 +447,31 @@ private DbException getDataConversionError(Value value, DbException cause) {
}

private void updateSequenceIfRequired(SessionLocal session, long value) {
if (sequence.getCycle() == Sequence.Cycle.EXHAUSTED) {
return;
}
long current = sequence.getCurrentValue();
long inc = sequence.getIncrement();
if (inc > 0) {
if (value <= current) {
/*
* Synchronization is necessary due to possible race with concurrent
* sessions
*/
synchronized (sequence) {
if (sequence.getCycle() == Sequence.Cycle.EXHAUSTED) {
return;
}
} else if (value >= current) {
return;
}
try {
sequence.modify(value + inc, null, null, null, null, null, null);
} catch (DbException ex) {
if (ex.getErrorCode() == ErrorCode.SEQUENCE_ATTRIBUTES_INVALID_7) {
long current = sequence.getCurrentValue();
long inc = sequence.getIncrement();
if (inc > 0) {
if (value <= current) {
return;
}
} else if (value >= current) {
return;
}
throw ex;
try {
sequence.modify(value + inc, null, null, null, null, null, null);
} catch (DbException ex) {
if (ex.getErrorCode() == ErrorCode.SEQUENCE_ATTRIBUTES_INVALID_7) {
return;
}
throw ex;
}
}
sequence.flush(session);
}
Expand Down

0 comments on commit f892858

Please sign in to comment.