rkhachatryan commented on a change in pull request #10847:
URL: https://github.com/apache/flink/pull/10847#discussion_r554110075



##########
File path: 
flink-connectors/flink-connector-jdbc/src/main/java/org/apache/flink/connector/jdbc/xa/XaFacadeImpl.java
##########
@@ -0,0 +1,453 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.connector.jdbc.xa;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.function.ThrowingRunnable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.concurrent.NotThreadSafe;
+import javax.sql.XAConnection;
+import javax.sql.XADataSource;
+import javax.transaction.xa.XAException;
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+import static java.util.Optional.empty;
+import static java.util.Optional.of;
+import static javax.transaction.xa.XAException.XAER_NOTA;
+import static javax.transaction.xa.XAException.XAER_RMFAIL;
+import static javax.transaction.xa.XAException.XA_HEURCOM;
+import static javax.transaction.xa.XAException.XA_HEURHAZ;
+import static javax.transaction.xa.XAException.XA_HEURMIX;
+import static javax.transaction.xa.XAException.XA_HEURRB;
+import static javax.transaction.xa.XAException.XA_RBBASE;
+import static javax.transaction.xa.XAException.XA_RBTIMEOUT;
+import static javax.transaction.xa.XAException.XA_RBTRANSIENT;
+import static javax.transaction.xa.XAResource.TMENDRSCAN;
+import static javax.transaction.xa.XAResource.TMNOFLAGS;
+import static javax.transaction.xa.XAResource.TMSTARTRSCAN;
+
+/** Default {@link XaFacade} implementation. */
+@NotThreadSafe
+@Internal
+class XaFacadeImpl implements XaFacade {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(XaFacadeImpl.class);
+    private static final Set<Integer> TRANSIENT_ERR_CODES =
+            new HashSet<>(Arrays.asList(XA_RBTRANSIENT, XAER_RMFAIL));
+    private static final Set<Integer> HEUR_ERR_CODES =
+            new HashSet<>(Arrays.asList(XA_HEURRB, XA_HEURCOM, XA_HEURHAZ, 
XA_HEURMIX));
+    private static final int MAX_RECOVER_CALLS = 100;
+
+    private final Supplier<XADataSource> dataSourceSupplier;
+    private final Integer timeoutSec;
+    private transient XAResource xaResource;
+    private transient Connection connection;
+    private transient XAConnection xaConnection;
+
+    /** @return a non-serializable instance. */
+    static XaFacadeImpl fromXaDataSource(XADataSource ds) {
+        return new XaFacadeImpl(() -> ds, empty());
+    }
+
+    XaFacadeImpl(Supplier<XADataSource> dataSourceSupplier, Optional<Integer> 
timeoutSec) {
+        this.dataSourceSupplier = 
Preconditions.checkNotNull(dataSourceSupplier);
+        this.timeoutSec = timeoutSec.orElse(null);
+    }
+
+    @Override
+    public void open() throws SQLException, XAException {
+        Preconditions.checkState(!isOpen(), "already connected");
+        XADataSource ds = dataSourceSupplier.get();
+        xaConnection = ds.getXAConnection();
+        xaResource = xaConnection.getXAResource();
+        if (timeoutSec != null) {
+            xaResource.setTransactionTimeout(timeoutSec);
+        }
+        connection = xaConnection.getConnection();
+        connection.setReadOnly(false);
+        connection.setAutoCommit(false);
+        Preconditions.checkState(!connection.getAutoCommit());
+    }
+
+    @Override
+    public void close() throws SQLException {
+        if (connection != null) {
+            connection.close();
+            connection = null;
+        }
+        if (xaConnection != null) {
+            xaConnection.close();
+            xaConnection = null;
+        }
+        xaResource = null;
+    }
+
+    @Override
+    public Connection getConnection() {
+        Preconditions.checkNotNull(connection);
+        return connection;
+    }
+
+    @Override
+    public boolean isConnectionValid() {
+        return isOpen();
+    }
+
+    @Override
+    public Connection getOrEstablishConnection() throws SQLException, 
ClassNotFoundException {
+        if (!isOpen()) {
+            try {
+                open();
+            } catch (XAException e) {
+                throw new SQLException(e);
+            }
+        }
+        return connection;
+    }
+
+    @Override
+    public void closeConnection() {
+        try {
+            close();
+        } catch (SQLException e) {
+            LOG.warn("Connection close failed.", e);
+        }
+    }
+
+    @Override
+    public Connection reestablishConnection() {
+        throw new UnsupportedOperationException();

Review comment:
       I'm afraid not: a potential ongoing XA transaction should be resumed (or 
at least aborted). 
   This would require either passing `Xid` from the outside or storing current 
`Xid` in this class. I'd rather avoid both of these options because they either 
complicate the caller or duplicate `Xid` tracking.




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

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


Reply via email to