Skip to content

Use prepared statement for all record for Oracle Reverse Replication - #4628

Open
dhwanilpatel wants to merge 6 commits into
GoogleCloudPlatform:mainfrom
dhwanilpatel:oracle-rev-prepared-stmt
Open

dhwanilpatel wants to merge 6 commits into
GoogleCloudPlatform:mainfrom
dhwanilpatel:oracle-rev-prepared-stmt

Conversation

@dhwanilpatel

@dhwanilpatel dhwanilpatel commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Use prepare statement for all records for oracle and enable prepareStatement caching. It provided 2x througput upto 10K events/s during load test.

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.57895% with 49 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.09%. Comparing base (2a90bcc) to head (975b53b).
⚠️ Report is 28 commits behind head on main.

Files with missing lines Patch % Lines
...om/custom/CustomTransformationWithOracleForIT.java 0.00% 23 Missing ⚠️
...v2/templates/source/oracle/OracleDMLGenerator.java 87.65% 9 Missing and 11 partials ⚠️
...port/v2/templates/source/oracle/OracleJdbcDao.java 92.30% 3 Missing and 1 partial ⚠️
.../java/com/custom/CustomTransformationForDLQIT.java 80.00% 0 Missing and 1 partial ⚠️
...es/source/oracle/OracleSpToSrcSourceConnector.java 94.11% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #4628      +/-   ##
============================================
+ Coverage     59.75%   66.09%   +6.33%     
+ Complexity     7926     4121    -3805     
============================================
  Files          1123      570     -553     
  Lines         72705    34905   -37800     
  Branches       8842     4003    -4839     
============================================
- Hits          43447    23071   -20376     
+ Misses        26362    10669   -15693     
+ Partials       2896     1165    -1731     
Components Coverage Δ
spanner-templates 85.51% <81.57%> (+1.48%) ⬆️
spanner-import-export ∅ <ø> (∅)
spanner-live-forward-migration 87.66% <ø> (+<0.01%) ⬆️
spanner-live-reverse-replication 82.97% <81.57%> (+3.03%) ⬆️
spanner-bulk-migration 88.29% <ø> (+<0.01%) ⬆️
gcs-spanner-dv 86.58% <ø> (ø)
Files with missing lines Coverage Δ
...eport/v2/templates/dbutils/dao/source/JdbcDao.java 100.00% <100.00%> (+40.54%) ⬆️
.../java/com/custom/CustomTransformationForDLQIT.java 71.01% <80.00%> (+8.89%) ⬆️
...es/source/oracle/OracleSpToSrcSourceConnector.java 86.53% <94.11%> (+78.20%) ⬆️
...port/v2/templates/source/oracle/OracleJdbcDao.java 92.30% <92.30%> (ø)
...v2/templates/source/oracle/OracleDMLGenerator.java 79.41% <87.65%> (+79.41%) ⬆️
...om/custom/CustomTransformationWithOracleForIT.java 0.00% <0.00%> (ø)

... and 582 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.
@dhwanilpatel
dhwanilpatel force-pushed the oracle-rev-prepared-stmt branch from e7186f7 to bce3ea4 Compare November 24, 2026 13:42
@dhwanilpatel
dhwanilpatel marked this pull request as ready for review November 25, 2026 08:46
@dhwanilpatel
dhwanilpatel requested a review from a team as a code owner November 25, 2026 08:46
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes Oracle reverse replication by transitioning from raw DML string generation to parameterized statements. By utilizing bind variables, the system can now leverage Oracle's implicit statement cache, significantly reducing the overhead of SQL parsing and improving throughput. The changes include a new Oracle-specific DAO for robust type handling and improved connection configuration to manage statement cache sizes.

Highlights

  • Prepared Statement Optimization: Implemented parameterized DML generation for Oracle to leverage implicit statement caching, resulting in a 2x throughput improvement during load testing.
  • OracleJdbcDao Implementation: Introduced a dedicated OracleJdbcDao to handle type-specific parameter binding, including support for typed NULLs, streamed LOBs, and fixed-width character types.
  • Implicit Statement Cache Configuration: Added automatic configuration of the Oracle implicit statement cache (defaulting to 100) to optimize cursor reuse and reduce soft parses.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the Oracle DML generation and database access layers to use parameterized queries instead of inline literals. It introduces OracleJdbcDao to handle Oracle-specific JDBC bindings (such as typed NULLs, CLOB/NCLOB streaming, and fixed-width CHAR blank-padding) and adds a ColumnBinder helper in OracleDMLGenerator to manage query placeholders and parameters. Additionally, it enables implicit statement caching by default in OracleSpToSrcSourceConnector to improve performance. I have no feedback to provide as there are no review comments.

@dhwanilpatel dhwanilpatel added the addition New feature or request label Nov 25, 2026

@bharadwaj-aditya bharadwaj-aditya 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.

The code looks a lot simpler but do we still need any type specific handling or the concept of rawValues ?

That should probably completely disappear right ?

String legacySys = "'" + source + "_v1'";
// With rawValues the value is bound as is (e.g. Oracle). Otherwise enclose it in single
// quotes since the DML generator injects custom responses directly as raw values.
String legacySys = rawValues ? source + "_v1" : "'" + source + "_v1'";

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.

i'm not sure why this variation in flow is required. Do we need the rawValues flag here ?

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.

CustomTransformation is shared across different source's IT (Mysql/PG/Oracle). In Oracle we are using prepared statement which will bind the value as it is from CustomTransformation, while other source's are using inline values, so they need sql literals for it.

Hence we have added this flag for passing rawValues for oracle and sql literals for other. Once other sources are also starts using prepared statement, we can remove this rawValues flag.

}

/** Binds a prepared statement parameter. Sources can override this for type specific binding. */
protected void bindParameter(java.sql.PreparedStatement pstmt, int paramIdx, Object param)

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.

is there any value in the refactor on this method ?

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.

As discussed in [https://github.com//pull/4628#discussion_r4717510296], we might need specific bind handling to few types, respactive JDBCDAO per source will need this method for doing it.

Like for oracle this method will be overriden for bounding different types in this loop: setFixedCHAR for CHAR/NCHAR, setNString/setNClob for national types, streamed setClob for large CLOBs, and typed setNull.

This might require for other source as well if when we do prepared statement, hence I abstracted out this method.

* </ul>
*/
private static Object toBindValueByType(String columnType, String colValue) {
if (NUMERIC_TYPES.contains(columnType)) {

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.

prepared statements should handle the type behaviour out of the box. Is this required in oracle ?

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.

The SQL only carries type info for non-character types (TO_NUMBER(?), TO_DATE(?), TO_TIMESTAMP(?), TO_BINARY_*, TO_*INTERVAL). Character, LOB and binary columns use a plain ? in USING (SELECT ? AS col FROM DUAL), and Oracle's driver binds by the Java setter, not the target column. A SQL cast can't fix this, because the issues happen at bind time:

  • CLOBs over 4000 bytes fail as VARCHAR2 binds (ORA-01461), so they must be streamed.
  • National types need the national character set form, or characters are lost on non-Unicode databases. CHAR keys need blank-padded comparison (setFixedCHAR) for MERGE/DELETE to match.
  • NUMBER is bound as BigDecimal to avoid dependence on the session decimal separator.
  • NULLs are typed to match non-null rows, so cached statements stay shared.
if (columnType == null) {
return "";
}
return columnType.replaceAll("\\([^)]*\\)", "").trim().replaceAll("\\s+", " ").toLowerCase();

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 logic seems quite brittle, don't we get the type and the modifier as separate fields ?

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.

This is needed for few of the types where precision is in between types like (TIMESTAMP(6) WITH TIMEZONE, INTERVAL DAY(2) TO SECOND(6)). This is needed for removing those precision from type.

@bharadwaj-aditya bharadwaj-aditya 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.

Left a few comments around edge cases in typed NULL binding (for HEXTORAW(?) on CLOB/NCLOB, national-charset FORM_NCHAR handling in OracleJdbcDao, and BIND_MISMATCH on nullable numeric columns), implicitStatementCacheSize short-name handling in ojdbc8, and custom transformation edge cases (BINARY_FLOAT/BINARY_DOUBLE infinity and source-only DATE columns).

toCustomTransformationBindValue(
normalizeSourceType(sourceType), customTransformationResponse.get(colName)));
return getColumnValueByType(
sourceType, sourceDbTimezoneOffset, getCustomTransformationSpannerType(colName));

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 applies the source-timezone conversion (AT TIME ZONE '') to custom transformation timestamps, so users with a non-UTC offset will see their returned values shifted.

Before this PR, custom values were used as-is. What is the decision here? We need to follow the same for MySQL. Can we skip the conversion for custom values here?

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.

We should write the custom transformation responses as is. If the response is invalid, the custom transformation function will need to change to handle that.

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.

Thanks for catching this. Fixed it for custom transformation where it will pass all the value as it is in plain ? with no TO_TIMESTAMP / AT TIME ZONE '<offset>', etc.

@bharadwaj-aditya bharadwaj-aditya 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.

Largely looks ok. But we need to be sure we don't edit the custom transformation response and write it as is.

@dhwanilpatel

Copy link
Copy Markdown
Contributor Author

Largely looks ok. But we need to be sure we don't edit the custom transformation response and write it as is.

Yes, the custom transformation response is no longer edited.

  • Each value is bound as returned through a plain ?, with no conversion or SQL functions.
  • The only per-column logic left is the JDBC bind method, which doesn't change the value but is needed for a valid value to reach Oracle intact:
    • streamed setClob for large CLOBs (VARCHAR2 binds over 4000 bytes fail with ORA-01461);
    • the national character set form for NCHAR/NVARCHAR2 (otherwise characters are lost on non-Unicode databases);
    • setFixedCHAR for CHAR keys (blank-padded match in MERGE/DELETE);
    • typed NULLs.
  • If a value isn't valid for the column, the transformer needs to return the correct object (e.g. java.sql.Timestamp for TIMESTAMP, byte[] for RAW).

@bharadwaj-aditya bharadwaj-aditya 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

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

Labels

addition New feature or request size/XXL

3 participants