Skip to content

Fix JDBC lost-create under SERIALIZABLE isolation - #5094

Merged
dimas-b merged 3 commits into
apache:mainfrom
iprithv:fix-jdbc-lost-create-concurrency
Aug 10, 2026
Merged

dimas-b merged 3 commits into
apache:mainfrom
iprithv:fix-jdbc-lost-create-concurrency

Conversation

@iprithv

@iprithv iprithv commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good catch!

However, the exception is thrown from persistEntity, but the two callers reach it differently:

  1. in writeEntity, persistEntity runs outside withRetries, so RetryOnConcurrencyException propagates cleanly to persistNewEntity's catch (that's good).
  2. in writeEntities, the whole callback runs inside withRetries (via runWithinTransaction). So withRetries intercepts the exception first at DatasourceOperations:
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
}
  1. Since 23505 is non-retryable (only 40001 is) and maxRetries defaults to 1, withRetries re-wraps it into a new SQLException, which writeEntities then turns into a generic RuntimeException. Then, createEntitiesIfNotExist's new catch(RetryOnConcurrencyException) never runs. The caller gets an opaque error instead of ENTITY_ALREADY_EXISTS. EntityAlreadyExistsException works in this path only because it has the explicit carve-out RetryOnConcurrencyException lacks.

I suggest to add the same carve-out:

    && !(e instanceof EntityAlreadyExistsException)
    && !(e instanceof RetryOnConcurrencyException)) {

dimas-b
dimas-b previously approved these changes Jul 20, 2026
// 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) {

@dimas-b dimas-b Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Jul 20, 2026
@dimas-b

dimas-b commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

but +1 to @jbonofre 's comment above

@iprithv
iprithv force-pushed the fix-jdbc-lost-create-concurrency branch from fe67d36 to 9ac6b0d Compare July 22, 2026 21:59
@iprithv
iprithv requested review from dimas-b and jbonofre July 22, 2026 22:06
@iprithv

iprithv commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@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!

@vigneshio

vigneshio commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

LGTM... A small unit test that persistNewEntity / createEntitiesIfNotExist map RetryOnConcurrencyException -> ENTITY_ALREADY_EXISTS would lock the layer @jbonofre was worried about end-to-end. Not blocking.. cc: @dimas-b

dimas-b
dimas-b previously approved these changes Aug 6, 2026

@dimas-b dimas-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall.

if (e.getCause() instanceof SQLException
&& !(e instanceof EntityAlreadyExistsException)) {
&& !(e instanceof EntityAlreadyExistsException)
&& !(e instanceof RetryOnConcurrencyException)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is becoming complicated. Should we have a simple catch-and-rethrow clauses for those poss-through exceptions above line 391?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@iprithv
iprithv force-pushed the fix-jdbc-lost-create-concurrency branch from 48e2028 to a90873c Compare August 10, 2026 21:33
@dimas-b

dimas-b commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

I believe @jbonofre 's concerns have been addressed... merging.

@dimas-b
dimas-b merged commit 6e6c96e into apache:main Aug 10, 2026
25 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants