You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "1991santhu (via GitHub)" <gi...@apache.org> on 2026/08/06 20:38:42 UTC

[PR] [Fix][Connector-V2][Jdbc] Validate a cached connection before reusing it [seatunnel]

1991santhu opened a new pull request, #11678:
URL: https://github.com/apache/seatunnel/pull/11678

   ### Purpose of this pull request
   
   Closes the stale-connection failure reported in #6638.
   
   `ConnectionPoolManager` borrows a connection from the Hikari pool once per queue index and then holds it for the lifetime of the writer:
   
   ```java
   public Connection getConnection(int index) {
       return connectionMap.computeIfAbsent(index, i -> { ... connectionPool.getConnection() ... });
   }
   ```
   
   Because the connection never goes back to the pool, Hikari's housekeeper never gets to inspect it, and none of the pool's own liveness settings can reach it. On a streaming job the gap between writes can run to hours, and the server or anything on the path can close the socket in the meantime. The cached connection is then handed back dead, and the failure surfaces as a write error *after* the record has already been consumed.
   
   That is the shape in #6638 — the driver reports the last packet as received tens of millions of milliseconds earlier:
   
   ```
   The last packet successfully received from the server was 55,800,019 milliseconds ago
   ```
   
   `autoReconnect=true` does not help, because the connection is never re-borrowed. The issue has been reported independently by five people since April 2024, and @dawner84 identified the root cause directly in January — the connection stays `active` and is never validated ("缺乏探测机制").
   
   ### The change
   
   The cached connection is validated before it is returned, and replaced and closed if it is no longer usable.
   
   The check uses the pool's own configuration rather than forming a second opinion: `connectionTestQuery` when one is set, since it is configured precisely for drivers whose `isValid` cannot be trusted, and `isValid` otherwise. The validation timeout comes from `HikariDataSource.getValidationTimeout()`, falling back to five seconds when it is unset.
   
   Two things I want to flag rather than have a reviewer find them:
   
   1. **`computeIfAbsent` → `compute` lengthens the critical section.** The validation now runs inside the map's per-bin lock. Blocking I/O under that lock is not new — `connectionPool.getConnection()` was already called inside `computeIfAbsent` — so this makes an existing critical section longer rather than introducing a new one. I kept it atomic deliberately: `SimpleJdbcConnectionPoolProviderProxy` binds one `queueIndex` per instance, so there is no same-key contention, and validating outside the lock would reintroduce a check-then-act race where two callers each borrow a connection for one index. That would leak connections, which is the complaint in #6638 in the first place.
   
   2. **`isConnectionValid()` becomes self-healing.** `SimpleJdbcConnectionPoolProviderProxy.isConnectionValid()` calls `getConnection(queueIndex)`, which may now replace a dead connection as a side effect and then report valid. I think that is the better behaviour for a liveness check on a held connection, but it is a change in meaning and worth a maintainer's opinion.
   
   Behaviour on a live connection is unchanged: borrowed once, served from the cache thereafter.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No new or changed configuration option, and no change to the happy path. The observable difference is that a JDBC sink which previously failed on the first write after a long idle period now reconnects and succeeds.
   
   ### How was this patch tested?
   
   New unit tests in `ConnectionPoolManagerTest` (6 cases), covering both directions:
   
   | Test | Covers |
   |---|---|
   | `reusesAConnectionThatIsStillUsable` | live connection borrowed once, then cached — no regression |
   | `replacesAConnectionThatHasGoneStale` | the #6638 case; dead connection replaced and closed, not leaked |
   | `replacesAConnectionThatIsAlreadyClosed` | `isClosed` short-circuits before `isValid` |
   | `usesTheConfiguredTestQueryInsteadOfIsValid` | `connectionTestQuery` is honoured when configured |
   | `replacesTheConnectionWhenTheTestQueryFails` | a throwing test query replaces rather than propagates |
   | `keepsConnectionsSeparatePerQueueIndex` | per-index isolation preserved |
   
   Run on JDK 11:
   
   ```
   ./mvnw -o test -pl seatunnel-connectors-v2/connector-jdbc -Dtest=ConnectionPoolManagerTest
   Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
   ```
   
   I checked that the tests actually fail without the fix rather than assuming it. Reverting only `ConnectionPoolManager.java` and keeping the tests:
   
   ```
   Tests run: 6, Failures: 4, Errors: 0, Skipped: 0
   ```
   
   The four failures are exactly the cases that exercise replacement; the two that still pass are the ones asserting preserved behaviour, which is what should happen.
   
   Full module, same JDK:
   
   ```
   ./mvnw -o test -pl seatunnel-connectors-v2/connector-jdbc
   Tests run: 923, Failures: 0, Errors: 4, Skipped: 6
   ```
   
   The 4 errors are Testcontainers `startContainer` failures because Docker was not running on my machine; they are unrelated to this change.
   
   ### Check list
   
   * [x] If any new Jar binary package adding in your PR, please add License Notice according [New License Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/developer/new-license.md) — no new dependency
   * [x] If necessary, please update the documentation to describe the new feature. https://github.com/apache/seatunnel/tree/dev/docs — no new option, nothing to document
   * [x] If necessary, please update `incompatible-changes.md` to describe the incompatibility caused by this PR — no incompatible change
   * [x] If you are contributing the connector code, please check that the following files are updated — not a new connector, no mapping/dist/label/plugin_config change needed
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connector-V2][Jdbc] Validate a cached connection before reusing it [seatunnel]

Posted by "DanielLeens (via GitHub)" <gi...@apache.org>.
DanielLeens commented on PR #11678:
URL: https://github.com/apache/seatunnel/pull/11678#issuecomment-5363682289

   Thanks for the careful follow-up — happy to confirm both, and I re-verified each one directly against the current head rather than just restating my round-6 write-up.
   
   **1. Fix 2 (`markSuccessfulAutoFlushBoundaryIfNeeded()` / `releaseSavepointSilently()`) — confirmed resolved.**
   
   I re-read `9f038035b`'s diff again just now. It hoists `Connection connection = connectionProvider.getConnection();` once at the top of `markSuccessfulAutoFlushBoundaryIfNeeded()`, uses that same local for `setSavepoint()`, and passes it into `releaseSavepointSilently(connection, previousSavepoint)`, which now takes `Connection` as a parameter instead of calling `getConnection()` internally. That's a byte-for-byte match to what I described as "after" in round 6 — both round-5 Medium findings (the rollback/savepoint-mismatch risk and the doubled per-auto-flush validation cost) are fixed in this single commit, and it's the only commit since round 5. So yes: both blockers, not just Fix 1, are verified resolved on the current head.
   
   **2. `bb6d4dc` — confirmed already-merged content, no new PR-authored logic.**
   
   I diffed `bb6d4dc`'s version of `MultiTableSinkWriterSchemaChangeBroadcastTest.java` against the current `dev` tip and it's an exact match (empty diff) — and `dev`'s history shows that content came from `#11726` ("Tolerate the pre-existing close() race in the schema-change worker-failure test"), merged 2026-08-11. So `bb6d4dc` is purely a cherry-pick of already-reviewed, already-merged upstream content onto this branch; nothing new to review there.
   
   **One more thing worth flagging while we're here:** `mergeStateStatus` is still showing `BLOCKED`, and the reason is concrete, not stale caching — the stray review posted under the `davidzollo` account is still recorded as `CHANGES_REQUESTED` (not dismissed), so GitHub's review-decision logic still counts it even though the authoritative `DanielLeens` copy was corrected to `APPROVED` back in round 5 and reconfirmed in round 6. I only have read-level review controls here, so I can't dismiss it myself — a maintainer with write access on that review will need to dismiss `davidzollo`'s stray `CHANGES_REQUESTED` entry before this can actually merge, independent of anything code-related.
   
   To summarize where this stands: no code changes needed from my side, CI was fully green on `9f038035b` as of round 6, and the only remaining blocker to merging is the stray review dismissal above. Thanks again for tracking this so closely through every round.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connector-V2][Jdbc] Validate a cached connection before reusing it [seatunnel]

Posted by "davidzollo (via GitHub)" <gi...@apache.org>.
davidzollo commented on PR #11678:
URL: https://github.com/apache/seatunnel/pull/11678#issuecomment-5233646516

   Thanks for your contribution! This is a solid addition to the project, and your attention to detail really shows. Contributors like you are what keep this community moving forward. Feel free to keep the PRs coming — we're always glad to have you here!
   
   Please ignore the following message if you have joined in the Slack~
   ```
   To facilitate focused communication among contributors, we’ve created a brand-new dedicated #contributors channel in official Slack workspace.
   Slack workspace link: https://s.apache.org/seatunnel-slack
   
   This channel is designed for technical discussions, feature design brainstorming, roadmap sync and direct communication with project core Contributors. Everyone who contributes code, docs, test cases or community ideas is warmly invited to join, share thoughts and collaborate together under The Apache Way. Welcome to join 😄
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connector-V2][Jdbc] Validate a cached connection before reusing it [seatunnel]

Posted by "DanielLeens (via GitHub)" <gi...@apache.org>.
DanielLeens commented on PR #11678:
URL: https://github.com/apache/seatunnel/pull/11678#issuecomment-5322652174

   Quick follow-up to my last review: I flagged the `unit-test (11, windows-latest)` failure (`MultiTableSinkWriterSchemaChangeBroadcastTest`) as unrelated to this PR's diff and suggested a targeted re-run at the time. Since then, that exact test got a fix in `dev` — apache/seatunnel#11726, "[Test][API] Tolerate the pre-existing close() race in the schema-change worker-failure test" (merged 2026-08-11, after my last check here). This branch is currently `behind_by=55` against `dev`, so a sync now pulls in that fix directly rather than relying on a rerun to dodge the flake. Once synced, CI should be clean; happy to take another look if anything unexpected turns up.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connector-V2][Jdbc] Validate a cached connection before reusing it [seatunnel]

Posted by "davidzollo (via GitHub)" <gi...@apache.org>.
davidzollo merged PR #11678:
URL: https://github.com/apache/seatunnel/pull/11678


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connector-V2][Jdbc] Validate a cached connection before reusing it [seatunnel]

Posted by "SEZ9 (via GitHub)" <gi...@apache.org>.
SEZ9 commented on PR #11678:
URL: https://github.com/apache/seatunnel/pull/11678#issuecomment-5358312506

   Thanks @DanielLeens for the thorough round-5 and round-6 write-ups — the level of detail here (verifying `9f038035b` touches only `JdbcSinkWriter.java` and that `ConnectionPoolManager.java`, `SimpleJdbcConnectionPoolProviderProxy.java`, and `ConnectionPoolManagerTest.java` are byte-identical to the round-4 approved version) makes this easy to follow.
   
   On the account mix-up: understood, no problem. I'll treat `#pullrequestreview-4957017931` as the authoritative round-5 record and will dismiss the stray duplicate at `#pullrequestreview-4957008079` so the review history stays clean.
   
   Good catch on the interaction with the Error Data Bypass / Dead Letter Queue feature that landed in `dev` after your round-4 approval — that's exactly the kind of thing a full re-review of the head (rather than an incremental diff) is meant to surface, and I appreciate you tracing it rather than assuming the merge commit `8add67a` was content-neutral.
   
   Two concrete asks before we move to merge:
   
   1. Your round-6 comment says `9f038035b` fixes both Medium-severity blockers from round 5, but the visible analysis covers Fix 1 (`rollbackIfNeeded()`). Could you confirm explicitly that the second blocker is also verified resolved on the current head, ideally with the same read-the-method-body verification you did for Fix 1?
   2. Please confirm there's nothing else outstanding from your side on `bb6d4dc` (the cherry-picked flake fix) — my understanding from your notes is it's already-merged content and adds no new PR-authored logic, but I'd like that stated for the record.
   
   Once both are confirmed, I'm comfortable proceeding with the merge given your APPROVED review. Thanks again for the careful work here.
   
   <!-- streview-comment:410 -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Fix][Connector-V2][Jdbc] Validate a cached connection before reusing it [seatunnel]

Posted by "1991santhu (via GitHub)" <gi...@apache.org>.
1991santhu commented on PR #11678:
URL: https://github.com/apache/seatunnel/pull/11678#issuecomment-5218522988

   Thanks — the logging point is the one that mattered, and you're right about why.
   
   Replacements now log at WARN, rate limited to one message a minute, with the count since the last message. That's what makes the two cases distinguishable: occasional idle recovery produces an isolated line with a count of 1, while a misconfigured `connectionTestQuery` or a server-side connection limit produces a line every minute with a large count. Only an actual replacement logs — the first borrow for an index doesn't, so a healthy job stays quiet.
   
   Added a note at `isConnectionValid` that it isn't a pure predicate. I'd rather it be stated at the method than left for the next reader to work out from two hops away.
   
   Also pinned the validation timeout in tests, which was only covered by a loose matcher before: the pool's own `validationTimeout` when set, the 5000ms fallback when it isn't, and the one-second floor. That last one is the case worth having — a sub-second setting truncates to zero seconds, and `isValid(0)` means "no timeout" rather than "fail fast", so the floor is load-bearing. I checked the first and third fail if the timeout resolution is bypassed.
   
   On the two you raised that I haven't changed:
   
   The `DsqlConnectionPoolManager` scope gap is real and I'd rather do it separately. It has the same unvalidated cache, and as you say its connections are additionally invalidated out-of-band by the token refresh, so the fix isn't identical and would need its own tests. Happy to open that as a follow-up once this lands.
   
   On prepared statements outliving a swapped connection — agreed on the analysis, and agreed it isn't worth changing here. The wasted flush attempt comes out of the retry budget rather than failing the job, and the alternative would mean invalidating statements from inside the pool manager, which would put it in the business of knowing about its callers.
   
   Statement of what I ran: `ConnectionPoolManagerTest` 9/9, full `connector-jdbc` module 926 tests with 0 failures. The 4 errors in the module run are Testcontainers failing to start containers because Docker wasn't running on my machine, not related to this change.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org