kezhenxu94 commented on code in PR #10406: URL: https://github.com/apache/dolphinscheduler/pull/10406#discussion_r894987247
########## dolphinscheduler-registry/dolphinscheduler-registry-plugins/dolphinscheduler-registry-mysql/src/main/java/org/apache/dolphinscheduler/plugin/registry/mysql/MysqlOperator.java: ########## @@ -0,0 +1,325 @@ +/* + * 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.dolphinscheduler.plugin.registry.mysql; + +import org.apache.dolphinscheduler.plugin.registry.mysql.model.DataType; +import org.apache.dolphinscheduler.plugin.registry.mysql.model.MysqlRegistryData; +import org.apache.dolphinscheduler.plugin.registry.mysql.model.MysqlRegistryLock; + +import org.apache.commons.lang3.StringUtils; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLIntegrityConstraintViolationException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +/** + * Used to CRUD from mysql + */ +public class MysqlOperator implements AutoCloseable { + + private static final Logger logger = LoggerFactory.getLogger(MysqlOperator.class); + + private final HikariDataSource dataSource; + + public MysqlOperator(MysqlRegistryProperties.MysqlDatasourceProperties datasourceProperties) { + HikariConfig hikariConfig = new HikariConfig(); + hikariConfig.setDriverClassName(datasourceProperties.getDriverClassName()); + hikariConfig.setJdbcUrl(datasourceProperties.getUrl()); + hikariConfig.setUsername(datasourceProperties.getUsername()); + hikariConfig.setPassword(datasourceProperties.getPassword()); + hikariConfig.setMaximumPoolSize(datasourceProperties.getMaximumPoolSize()); + hikariConfig.setConnectionTimeout(datasourceProperties.getConnectionTimeout()); + hikariConfig.setIdleTimeout(datasourceProperties.getIdleTimeout()); + hikariConfig.setPoolName("MysqlRegistryDataSourcePool"); + + this.dataSource = new HikariDataSource(hikariConfig); + } + + public void healthCheck() throws SQLException { + String sql = "select 1 from t_ds_mysql_registry_data"; + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + // if no exception, the healthCheck success + preparedStatement.executeQuery(); + } + } + + public List<MysqlRegistryData> queryAllMysqlRegistryData() throws SQLException { + String sql = "select id, `key`, data, type, create_time, last_update_time from t_ds_mysql_registry_data"; + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + ResultSet resultSet = preparedStatement.executeQuery(); Review Comment: Also put `resultSet` in the try-with-resource block, (tho the result set will be closed when the statement/connection is closed)? ```suggestion try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) { ``` -- 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]
