Repository navigation
Fix JDBC lost-create under SERIALIZABLE isolation - #5094
Conversation
jbonofre
left a comment
There was a problem hiding this comment.
That's a good catch!
However, the exception is thrown from persistEntity, but the two callers reach it differently:
- in
writeEntity,persistEntityruns outsidewithRetries, soRetryOnConcurrencyExceptionpropagates cleanly topersistNewEntity's catch (that's good). - in
writeEntities, the whole callback runs insidewithRetries(viarunWithinTransaction). SowithRetriesintercepts the exception first atDatasourceOperations:
if (e.getCause() instanceof SQLException
&& !(e instanceof EntityAlreadyExistsException)) { // no RetryOnConcurrency carve-out
sqlException = (SQLException) e.getCause(); // unwraps to the 23505
} else {
throw e; // ← pass-through it never takes
}
- Since 23505 is non-retryable (only 40001 is) and
maxRetriesdefaults to 1,withRetriesre-wraps it into a newSQLException, whichwriteEntitiesthen turns into a genericRuntimeException. Then,createEntitiesIfNotExist's newcatch(RetryOnConcurrencyException)never runs. The caller gets an opaque error instead ofENTITY_ALREADY_EXISTS.EntityAlreadyExistsExceptionworks in this path only because it has the explicit carve-outRetryOnConcurrencyExceptionlacks.
I suggest to add the same carve-out:
&& !(e instanceof EntityAlreadyExistsException)
&& !(e instanceof RetryOnConcurrencyException)) {
| // existingEntity can be null. We cannot distinguish a same-id idempotent retry from a | ||
| // genuine name collision in that case, so we must report a concurrency conflict rather | ||
| // than fabricate the entity we were trying to create. | ||
| if (existingEntity != null) { |
There was a problem hiding this comment.
This is still not 100% correct, I think. The existingEntity is loaded in a different Tx, so there's no guarantee that it is the root cause of the conflict in current Tx 🤷
That said, I think the tradeoff is that the changed code is more correct than the previous code 😅 I think it's a reasonable change.
There was a problem hiding this comment.
actually yes, conflict resolution is still approximate. we're essentially saying "something with this name already exists" rather than proving it's the exact conflicting row....after this gets in, we no longer fabricate the new entity as a successful create when the real conflict is invisible :)
|
but +1 to @jbonofre 's comment above |
fe67d36 to
9ac6b0d
Compare
|
@jbonofre got it, I updated DatasourceOperations.withRetries to skip unwrapping for RetryOnConcurrencyException, and added withRetries_propagatesRetryOnConcurrencyExceptionWithoutUnwrapping to make sure it passes through untouched. should be good now. thanks! |
| if (e.getCause() instanceof SQLException | ||
| && !(e instanceof EntityAlreadyExistsException)) { | ||
| && !(e instanceof EntityAlreadyExistsException) | ||
| && !(e instanceof RetryOnConcurrencyException)) { |
There was a problem hiding this comment.
This is becoming complicated. Should we have a simple catch-and-rethrow clauses for those poss-through exceptions above line 391?
There was a problem hiding this comment.
Pushed the changes. Thanks for the nudge!
| // Pass-through domain exceptions unchanged. Do not unwrap their SQLException cause into the | ||
| // retry path (that would rewrap them as a generic SQLException and lose the typed signal | ||
| // upper layers need, e.g. ENTITY_ALREADY_EXISTS mapping). | ||
| if (e instanceof EntityAlreadyExistsException) { |
There was a problem hiding this comment.
This is not quite what I meant 😅 We catch RuntimeException and then have ifs on the sub-type... This is not nice. Why not have a separate catch(EntityAlreadyExistsException | RetryOnConcurrencyException) above line 391?
There was a problem hiding this comment.
Makes sense 😅 - switched to a dedicated catch (EntityAlreadyExistsException | RetryOnConcurrencyException e) { throw e; } clause above the broad catch and dropped the instanceof checks entirely.
Also added AtomicOperationMetaStoreManagerCreateConflictTest covering the RetryOnConcurrencyException → ENTITY_ALREADY_EXISTS mapping for both createEntityIfNotExists and createEntitiesIfNotExist. PTAL, thanks!
Use explicit early rethrows for EntityAlreadyExistsException and RetryOnConcurrencyException instead of growing negated conditions, per review feedback. Behavior is unchanged; add a matching pass-through test for EntityAlreadyExistsException.
48e2028 to
a90873c
Compare
|
I believe @jbonofre 's concerns have been addressed... merging. |
No description provided.