davidzollo commented on code in PR #10428:
URL: https://github.com/apache/seatunnel/pull/10428#discussion_r2774691182


##########
seatunnel-connectors-v2/connector-cdc/connector-cdc-oracle/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/oracle/utils/OracleConnectionUtils.java:
##########
@@ -84,6 +84,39 @@ public static RedoLogOffset 
currentRedoLogOffset(JdbcConnection jdbc) {
         }
     }
 
+    /**
+     * Convert timestamp (milliseconds since epoch) to Oracle SCN.
+     *
+     * @param jdbc JDBC connection
+     * @param timestampMs timestamp in milliseconds since epoch
+     * @return RedoLogOffset with the corresponding SCN
+     */
+    public static RedoLogOffset timestampToScn(JdbcConnection jdbc, long 
timestampMs) {
+        try {
+            LOG.info("Converting timestamp {} to SCN", timestampMs);
+            String sql = "SELECT TIMESTAMP_TO_SCN(?) AS SCN FROM DUAL";
+            return jdbc.prepareQueryAndMap(
+                    sql,
+                    statement -> {
+                        java.sql.Timestamp timestamp = new 
java.sql.Timestamp(timestampMs);
+                        statement.setTimestamp(1, timestamp);

Review Comment:
   Can you have a try for the following code
   
   ```
     /**
        * Convert timestamp (milliseconds since epoch) to Oracle SCN.
        *
        * @param jdbc JDBC connection
        * @param timestampMs timestamp in milliseconds since epoch
        * @return RedoLogOffset with the corresponding SCN
        */
       public static RedoLogOffset timestampToScn(JdbcConnection jdbc, long 
timestampMs) {
           try {
   // Use UTC base time + offset to completely avoid JVM and Session time zone 
interference
               // TO_TIMESTAMP_TZ('1970-01-01 00:00:00 +00:00', 'YYYY-MM-DD 
HH24:MI:SS TZH:TZM') establishes an absolute UTC origin
               // NUMTODSINTERVAL(?, 'SECOND') converts milliseconds to a 
second interval (Oracle division retains decimals by default, with sufficient 
precision)
               String sql = "SELECT TIMESTAMP_TO_SCN(" +
                       "TO_TIMESTAMP_TZ('1970-01-01 00:00:00 +00:00', 
'YYYY-MM-DD HH24:MI:SS TZH:TZM') " +
                       "+ NUMTODSINTERVAL(? / 1000, 'SECOND')" +
                       ") AS SCN FROM DUAL";
               
               return jdbc.prepareQueryAndMap(
                       sql,
                       statement -> {
                                 // Pass Long directly; there's no need for 
setTimestamp, and thus no need for Calendar or time zone conversion             
           
                              statement.setObject(1, timestampMs);
                       },
                       rs -> {
                           if (rs.next()) {
                               final String scn = rs.getString(1);
                               return new RedoLogOffset(Long.parseLong(scn));
                           } else {
                               throw new SeaTunnelException(
                                       "Cannot convert timestamp to SCN. Make 
sure the specified timestamp is valid.");
                           }
                       });
           } catch (SQLException e) {
   
               String msg = "Failed to convert timestamp to SCN";
               if (e.getMessage() != null && 
e.getMessage().contains("ORA-08180")) {
                   msg += ". The timestamp is too old and the redo log may have 
been purged (undo_retention exceeded).";
               }
               throw new SeaTunnelException(msg, e);
           }
       }
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to