Skip to content

[SpannerToSourceDb] Fix JdbcDao connection leak and add rollback on error - #4662

Closed
aasthabharill wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
aasthabharill:pg-ps-1-jdbc-dao
Closed

aasthabharill wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
aasthabharill:pg-ps-1-jdbc-dao

Conversation

@aasthabharill

@aasthabharill aasthabharill commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Part 1 of 7 for migrating v2/spanner-to-sourcedb JDBC reverse-replication writers from string-concatenated SQL to parameterized PreparedStatement execution (split out of combined prototype #4655).

This PR is self-contained and safe to merge on its own: it fixes a real connection-leak bug in JdbcDao.write, adds explicit transaction rollback on failure across all JDBC dialects, and removes static connection-pool mutation from JdbcDaoTest.

Implementation & Why

  • JdbcDao (dbutils/dao/source/JdbcDao.java):
    • Migrated Connection and Statement/PreparedStatement to try-with-resources with explicit connObj.rollback() on Exception (attaching rollback errors as suppressed exceptions).
    • Why: previously, if statement.close() threw on a broken socket or after a failed transactional check, the pooled Connection leaked without being closed or rolled back. Narrowing the catch to Exception avoids attempting network rollbacks on JVM Errors.
    • Extracted bindParameter while keeping existing untyped Oracle LOB (setBlob/setObject) and plain Statement behavior identical for all dialects.
  • Tests (JdbcDaoTest.java):
    • Rewrote JdbcDaoTest to mock IConnectionHelper instead of mutating static JdbcConnectionHelper.connectionPoolMap.
    • Added coverage for setAutoCommit(false) -> executeUpdate -> TransactionalCheck -> commit ordering, Oracle byte[] blob stream verification, rollback on failure (PreparedStatement, raw Statement, commit, TransactionalCheck), post-commit close() error handling, and suppressed exception preservation.

Verification

  • Local: mvn -pl v2/spanner-common,v2/spanner-custom-shard,v2/spanner-to-sourcedb test spotless:check (Checkstyle, Spotless, and unit tests passed).
  • CI: Unit tests + existing MySQL, PostgreSQL, Oracle, and SQL Server reverse-replication ITs verify JdbcDao with zero regressions.

Series Context

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.39%. Comparing base (716a5e0) to head (3c037b1).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4662      +/-   ##
============================================
+ Coverage     59.72%   66.39%   +6.67%     
+ Complexity     8218     3268    -4950     
============================================
  Files          1122      551     -571     
  Lines         72762    32602   -40160     
  Branches       8855     3772    -5083     
============================================
- Hits          43454    21647   -21807     
+ Misses        26405     9883   -16522     
+ Partials       2903     1072    -1831     
Components Coverage Δ
spanner-templates 84.08% <100.00%> (+0.04%) ⬆️
spanner-import-export ∅ <ø> (∅)
spanner-live-forward-migration 87.57% <ø> (-0.09%) ⬇️
spanner-live-reverse-replication 80.04% <100.00%> (+0.09%) ⬆️
spanner-bulk-migration 88.24% <ø> (-0.05%) ⬇️
gcs-spanner-dv 86.48% <ø> (-0.10%) ⬇️
Files with missing lines Coverage Δ
...eport/v2/templates/dbutils/dao/source/JdbcDao.java 100.00% <100.00%> (+40.54%) ⬆️

... and 594 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pull-request-size pull-request-size Bot added size/L and removed size/XL labels Dec 1, 2026
@aasthabharill aasthabharill changed the title [SpannerToSourceDb] Add PreparedStatementValue and fix JdbcDao connection leak [SpannerToSourceDb] Fix JdbcDao connection leak and add rollback on error Dec 1, 2026
@aasthabharill

Copy link
Copy Markdown
Member Author

Closing as superseded by merged PR #4644, which already fixed the JdbcDao.write() connection leak when statement.close() throws. The non-overlapping rollback() and JdbcDaoTest changes are dropped to keep this migration strictly scoped to PostgreSQL prepared statements. Remaining PostgreSQL changes continue in #4664 (and #4665 for expanded IT coverage).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant