View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.core.consistencycheck.table.calculator;
19  
20  import lombok.SneakyThrows;
21  import lombok.extern.slf4j.Slf4j;
22  
23  import java.sql.SQLException;
24  import java.sql.SQLFeatureNotSupportedException;
25  import java.sql.Statement;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  import java.util.concurrent.atomic.AtomicReference;
28  
29  /**
30   * Abstract single table inventory calculator.
31   */
32  @Slf4j
33  public abstract class AbstractSingleTableInventoryCalculator implements SingleTableInventoryCalculator {
34      
35      private final AtomicBoolean canceling = new AtomicBoolean(false);
36      
37      private final AtomicReference<Statement> currentStatement = new AtomicReference<>();
38      
39      protected final void setCurrentStatement(final Statement statement) {
40          currentStatement.set(statement);
41      }
42      
43      @SneakyThrows(SQLException.class)
44      @Override
45      public void cancel() {
46          canceling.set(true);
47          Statement statement = currentStatement.get();
48          if (null == statement || statement.isClosed()) {
49              log.info("cancel, statement is null or closed");
50              return;
51          }
52          try {
53              statement.cancel();
54          } catch (final SQLFeatureNotSupportedException ex) {
55              log.info("cancel is not supported: {}", ex.getMessage());
56              // CHECKSTYLE:OFF
57          } catch (final SQLException | RuntimeException ex) {
58              // CHECKSTYLE:ON
59              log.info("cancel failed: {}", ex.getMessage());
60          }
61      }
62      
63      /**
64       * Is canceling.
65       *
66       * @return is canceling or not
67       */
68      public final boolean isCanceling() {
69          return canceling.get();
70      }
71  }