sunrisefromdark commented on code in PR #9012:
URL: https://github.com/apache/inlong/pull/9012#discussion_r1352311020


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/mysql/MySQLJdbcUtils.java:
##########
@@ -52,26 +52,48 @@ public class MySQLJdbcUtils {
      * @throws Exception on get connection error
      */
     public static Connection getConnection(String url, String user, String 
password) throws Exception {
-        if (StringUtils.isBlank(url) || !url.startsWith(MYSQL_JDBC_PREFIX)) {
-            throw new Exception("MySQL JDBC URL was invalid, it should start 
with jdbc:mysql");
-        }
+        ValidationUtils.extractHostAndValidatePortFromJdbcUrl(url, 
MYSQL_JDBC_PREFIX);
+        Connection conn = establishDatabaseConnection(url, user, password);
+        validateConnection(conn, url);
+        return conn;
+    }
 
+    /**
+     * Establishes a database connection using the provided URL, username, and 
password.
+     *
+     * @param url      The JDBC URL
+     * @param user     The username
+     * @param password The user's password
+     * @return A {@link Connection} object representing the database connection
+     * @throws Exception If an error occurs while obtaining the connection
+     */
+    private static Connection establishDatabaseConnection(String url, String 
user, String password) throws Exception {
         Connection conn;
         try {
             Class.forName(MYSQL_DRIVER_CLASS);
             conn = DriverManager.getConnection(url, user, password);
         } catch (Exception e) {
-            String errorMsg = "get MySQL connection error, please check MySQL 
JDBC url, username or password!";
+            String errorMsg = "Failed to get MySQL connection, please check 
MySQL JDBC URL, username, or password!";
             LOGGER.error(errorMsg, e);
-            throw new Exception(errorMsg + " other error msg: " + 
e.getMessage());
-        }
-        if (Objects.isNull(conn)) {
-            throw new Exception("get MySQL connection failed, please contact 
administrator.");
+            throw new Exception(errorMsg + " Other error message: " + 
e.getMessage());
         }
-        LOGGER.info("get MySQL connection success for url={}", url);
         return conn;
     }
 
+    /**
+     * Validates if the database connection was successfully obtained.
+     *
+     * @param conn The database connection
+     * @param url  The JDBC URL
+     * @throws Exception If the connection is null
+     */
+    private static void validateConnection(Connection conn, String url) throws 
Exception {

Review Comment:
   This redundant function has been removed



-- 
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: commits-unsubscr...@inlong.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to