MartijnVisser commented on code in PR #259:
URL: 
https://github.com/apache/flink-connector-jdbc/pull/259#discussion_r4773114544


##########
flink-connector-jdbc-core/src/main/java/org/apache/flink/connector/jdbc/core/datastream/sink/committer/JdbcCommitter.java:
##########
@@ -64,16 +75,54 @@ public void 
commit(Collection<CommitRequest<JdbcCommitable>> committables)
         if (DeliveryGuarantee.EXACTLY_ONCE != this.deliveryGuarantee && 
!committables.isEmpty()) {
             throw new FlinkRuntimeException("Non XA sink with commitables");
         }
+        if (!committables.isEmpty() && !opened) {
+            try {
+                xaConnectionProvider.open();
+            } catch (Exception e) {
+                // close() skips the provider, so release what open() got 
before it failed
+                try {
+                    xaConnectionProvider.close();
+                } catch (Exception closeException) {
+                    e.addSuppressed(closeException);
+                }
+                ExceptionUtils.rethrowIOException(e);
+            }
+            opened = true;
+        }
 
         for (CommitRequest<JdbcCommitable> request : committables) {
-            
request.getCommittable().getTransaction().orElse(this.jdbcTransaction).commitTx();
+            Xid xid = request.getCommittable().getXid();
+            try {
+                xaConnectionProvider.commit(xid, isRestored(xid));
+            } catch (TransientXaException e) {
+                if (request.getNumberOfRetries() + 1 >= maxCommitAttempts) {
+                    request.signalFailedWithUnknownReason(e);
+                } else {
+                    LOG.warn("failed to commit transaction {} (retrying)", 
xid, e);
+                    request.retryLater();
+                }
+            }
+        }
+    }
+
+    /**
+     * A transaction from before the restored checkpoint may have been 
committed before the failure.
+     * Any other transaction unknown to the database was not committed.
+     */
+    private boolean isRestored(Xid xid) throws IOException {
+        if (!restoredCheckpointId.isPresent()) {
+            return false;
         }
+        TransactionId transactionId =
+                TransactionId.createFromXid(
+                        xid.getFormatId(), xid.getGlobalTransactionId(), 
xid.getBranchQualifier());
+        return transactionId.getCheckpointId() < 
restoredCheckpointId.getAsLong();

Review Comment:
   Added `testFailOnUnknownTransactionOfTheRestoredCheckpoint`. I also ran PIT 
on `JdbcCommitter`: on the previous head both the `<=` and an `isRestored` that 
returns `true` whenever the job was restored survived, and the new test kills 
both. 69d052f4
   



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