haibo-duan commented on code in PR #5274:
URL: https://github.com/apache/inlong/pull/5274#discussion_r933393133


##########
inlong-manager/manager-service/src/main/java/org/apache/inlong/manager/service/resource/sink/mysql/MySQLResourceOperator.java:
##########
@@ -1,137 +1,137 @@
-/*
- * 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.inlong.manager.service.resource.mysql;
-
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.inlong.manager.common.consts.InlongConstants;
-import org.apache.inlong.manager.common.enums.SinkStatus;
-import org.apache.inlong.manager.common.enums.SinkType;
-import org.apache.inlong.manager.common.exceptions.WorkflowException;
-import org.apache.inlong.manager.common.pojo.sink.SinkInfo;
-import org.apache.inlong.manager.common.pojo.sink.mysql.MySQLColumnInfo;
-import org.apache.inlong.manager.common.pojo.sink.mysql.MySQLSinkDTO;
-import org.apache.inlong.manager.common.pojo.sink.mysql.MySQLTableInfo;
-import org.apache.inlong.manager.dao.entity.StreamSinkFieldEntity;
-import org.apache.inlong.manager.dao.mapper.StreamSinkFieldEntityMapper;
-import org.apache.inlong.manager.service.resource.SinkResourceOperator;
-import org.apache.inlong.manager.service.sink.StreamSinkService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import java.sql.Connection;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * MySQL's resource operator.
- */
-@Service
-public class MySQLResourceOperator implements SinkResourceOperator {
-
-    private static final Logger LOG = 
LoggerFactory.getLogger(MySQLResourceOperator.class);
-
-    @Autowired
-    private StreamSinkService sinkService;
-
-    @Autowired
-    private StreamSinkFieldEntityMapper fieldEntityMapper;
-
-    @Override
-    public Boolean accept(SinkType sinkType) {
-        return SinkType.MYSQL == sinkType;
-    }
-
-    @Override
-    public void createSinkResource(SinkInfo sinkInfo) {
-        LOG.info("begin to create MySQL resources sinkId={}", 
sinkInfo.getId());
-        if 
(SinkStatus.CONFIG_SUCCESSFUL.getCode().equals(sinkInfo.getStatus())) {
-            LOG.warn("MySQL resource [" + sinkInfo.getId() + "] already 
success, skip to create");
-            return;
-        } else if 
(InlongConstants.DISABLE_CREATE_RESOURCE.equals(sinkInfo.getEnableCreateResource()))
 {
-            LOG.warn("create resource was disabled, skip to create for [" + 
sinkInfo.getId() + "]");
-            return;
-        }
-        this.createTable(sinkInfo);
-    }
-
-    /**
-     * Create MySQL table by SinkInfo.
-     *
-     * @param sinkInfo {@link SinkInfo}
-     */
-    private void createTable(SinkInfo sinkInfo) {
-        LOG.info("begin to create MySQL table for sinkId={}", 
sinkInfo.getId());
-        List<StreamSinkFieldEntity> fieldList = 
fieldEntityMapper.selectBySinkId(sinkInfo.getId());
-        if (CollectionUtils.isEmpty(fieldList)) {
-            LOG.warn("no MySQL fields found, skip to create table for 
sinkId={}", sinkInfo.getId());
-        }
-        // set columns
-        List<MySQLColumnInfo> columnList = new ArrayList<>();
-        for (StreamSinkFieldEntity field : fieldList) {
-            MySQLColumnInfo columnInfo = new MySQLColumnInfo();
-            columnInfo.setName(field.getFieldName());
-            columnInfo.setType(field.getFieldType());
-            columnInfo.setComment(field.getFieldComment());
-            columnList.add(columnInfo);
-        }
-
-        MySQLSinkDTO mySQLSink = 
MySQLSinkDTO.getFromJson(sinkInfo.getExtParams());
-        MySQLTableInfo tableInfo = MySQLSinkDTO.getTableInfo(mySQLSink, 
columnList);
-        String url = mySQLSink.getJdbcUrl();
-        String user = mySQLSink.getUsername();
-        String password = mySQLSink.getPassword();
-
-        String dbName = tableInfo.getDbName();
-        String tableName = tableInfo.getTableName();
-        Connection conn = null;
-        try {
-            conn = MySQLJdbcUtils.getConnection(url, user, password);
-            // 1. create database if not exists
-            MySQLJdbcUtils.createDb(conn, dbName);
-            // 2. table not exists, create it
-            MySQLJdbcUtils.createTable(conn, tableInfo);
-            // 3. table exists, add columns - skip the exists columns
-            MySQLJdbcUtils.addColumns(conn, dbName, tableName, columnList);
-            // 4. update the sink status to success
-            String info = "success to create MySQL resource";
-            sinkService.updateStatus(sinkInfo.getId(), 
SinkStatus.CONFIG_SUCCESSFUL.getCode(), info);
-            LOG.info(info + " for sinkInfo={}", sinkInfo);
-            // 5. close connection.
-            conn.close();
-        } catch (Throwable e) {
-            String errMsg = "create MySQL table failed: " + e.getMessage();
-            LOG.error(errMsg, e);
-            sinkService.updateStatus(sinkInfo.getId(), 
SinkStatus.CONFIG_FAILED.getCode(), errMsg);
-            throw new WorkflowException(errMsg);
-        } finally {
-            try {
-                if (null != conn) {
-                    conn.close();
-                    conn = null;
-                }
-            } catch (Throwable e) {
-                String errMsg = "close MySQL connection failed: " + 
e.getMessage();
-                throw new WorkflowException(errMsg);
-            }
-        }
-        LOG.info("success create MySQL table for data sink [" + 
sinkInfo.getId() + "]");
-    }
-
-}
+/*
+ * 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.inlong.manager.service.resource.sink.mysql;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.inlong.manager.common.consts.InlongConstants;
+import org.apache.inlong.manager.common.enums.SinkStatus;
+import org.apache.inlong.manager.common.enums.SinkType;
+import org.apache.inlong.manager.common.exceptions.WorkflowException;
+import org.apache.inlong.manager.common.pojo.sink.SinkInfo;
+import org.apache.inlong.manager.common.pojo.sink.mysql.MySQLColumnInfo;
+import org.apache.inlong.manager.common.pojo.sink.mysql.MySQLSinkDTO;
+import org.apache.inlong.manager.common.pojo.sink.mysql.MySQLTableInfo;
+import org.apache.inlong.manager.dao.entity.StreamSinkFieldEntity;
+import org.apache.inlong.manager.dao.mapper.StreamSinkFieldEntityMapper;
+import org.apache.inlong.manager.service.resource.sink.SinkResourceOperator;
+import org.apache.inlong.manager.service.sink.StreamSinkService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * MySQL's resource operator.
+ */
+@Service
+public class MySQLResourceOperator implements SinkResourceOperator {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(MySQLResourceOperator.class);
+
+    @Autowired
+    private StreamSinkService sinkService;
+
+    @Autowired
+    private StreamSinkFieldEntityMapper fieldEntityMapper;
+
+    @Override
+    public Boolean accept(SinkType sinkType) {
+        return SinkType.MYSQL == sinkType;
+    }
+
+    @Override
+    public void createSinkResource(SinkInfo sinkInfo) {
+        LOG.info("begin to create MySQL resources sinkId={}", 
sinkInfo.getId());
+        if 
(SinkStatus.CONFIG_SUCCESSFUL.getCode().equals(sinkInfo.getStatus())) {
+            LOG.warn("MySQL resource [" + sinkInfo.getId() + "] already 
success, skip to create");
+            return;
+        } else if 
(InlongConstants.DISABLE_CREATE_RESOURCE.equals(sinkInfo.getEnableCreateResource()))
 {
+            LOG.warn("create resource was disabled, skip to create for [" + 
sinkInfo.getId() + "]");
+            return;
+        }
+        this.createTable(sinkInfo);
+    }
+
+    /**
+     * Create MySQL table by SinkInfo.
+     *
+     * @param sinkInfo {@link SinkInfo}
+     */
+    private void createTable(SinkInfo sinkInfo) {
+        LOG.info("begin to create MySQL table for sinkId={}", 
sinkInfo.getId());
+        List<StreamSinkFieldEntity> fieldList = 
fieldEntityMapper.selectBySinkId(sinkInfo.getId());
+        if (CollectionUtils.isEmpty(fieldList)) {
+            LOG.warn("no MySQL fields found, skip to create table for 
sinkId={}", sinkInfo.getId());
+        }
+        // set columns
+        List<MySQLColumnInfo> columnList = new ArrayList<>();
+        for (StreamSinkFieldEntity field : fieldList) {
+            MySQLColumnInfo columnInfo = new MySQLColumnInfo();
+            columnInfo.setName(field.getFieldName());
+            columnInfo.setType(field.getFieldType());
+            columnInfo.setComment(field.getFieldComment());
+            columnList.add(columnInfo);
+        }
+
+        MySQLSinkDTO mySQLSink = 
MySQLSinkDTO.getFromJson(sinkInfo.getExtParams());
+        MySQLTableInfo tableInfo = MySQLSinkDTO.getTableInfo(mySQLSink, 
columnList);
+        String url = mySQLSink.getJdbcUrl();
+        String user = mySQLSink.getUsername();
+        String password = mySQLSink.getPassword();
+
+        String dbName = tableInfo.getDbName();
+        String tableName = tableInfo.getTableName();
+        Connection conn = null;
+        try {
+            conn = MySQLJdbcUtils.getConnection(url, user, password);

Review Comment:
   It is better to replace the finally block with the try-with-resource 
statement.



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