umustafi commented on code in PR #3812:
URL: https://github.com/apache/gobblin/pull/3812#discussion_r1376739533


##########
gobblin-runtime/src/main/java/org/apache/gobblin/util/MySQLStoreUtils.java:
##########
@@ -0,0 +1,70 @@
+package org.apache.gobblin.util;
+
+import com.zaxxer.hikari.HikariDataSource;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import javax.sql.DataSource;
+import org.slf4j.Logger;
+
+
+/**
+ * MySQL based implementations of stores require common functionality that can 
be stored in a utility class. The
+ * functionality includes executing prepared statements on a data source 
object and executing SQL queries at fixed
+ * intervals. 
+ */
+public class MySQLStoreUtils {
+  private final DataSource dataSource;
+  private final Logger log;
+
+  public MySQLStoreUtils(DataSource dataSource, Logger log) {
+    this.dataSource = dataSource;
+    this.log = log;
+  }
+
+  /** `j.u.Function` variant for an operation that may @throw IOException or 
SQLException: preserves method signature checked exceptions */
+  @FunctionalInterface
+  public interface CheckedFunction<T, R> {
+    R apply(T t) throws IOException, SQLException;
+  }
+
+  /** Abstracts recurring pattern around resource management and exception 
re-mapping. */
+  public <T> T withPreparedStatement(String sql, 
CheckedFunction<PreparedStatement, T> f, boolean shouldCommit)
+      throws IOException {
+    try (Connection connection = dataSource.getConnection();
+        PreparedStatement statement = connection.prepareStatement(sql)) {
+      T result = f.apply(statement);
+      if (shouldCommit) {
+        connection.commit();
+      }
+      statement.close();
+      return result;
+    } catch (SQLException e) {
+      log.warn("Received SQL exception that can result from invalid 
connection. Checking if validation query is set {} "
+          + "Exception is {}", ((HikariDataSource) 
dataSource).getConnectionTestQuery(), e);
+      throw new IOException(e);
+    }
+  }
+
+  public void runSqlCommandWithInterval(String sqlCommand, long interval, 
TimeUnit timeUnit) {

Review Comment:
   Added a java doc to explain some of these. Let me know if I should clarify 
more. 



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