ggbocoder commented on code in PR #6538: URL: https://github.com/apache/incubator-seata/pull/6538#discussion_r1692306886
########## server/src/main/java/org/apache/seata/server/storage/db/store/VGroupMappingDataBaseDAO.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.seata.server.storage.db.store; + +import org.apache.seata.common.metadata.namingserver.Instance; +import org.apache.seata.common.util.IOUtil; +import org.apache.seata.config.Configuration; +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.core.store.MappingDO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.apache.seata.common.NamingServerConstants.DEFAULT_VGROUP_MAPPING; +import static org.apache.seata.common.NamingServerConstants.MAPPING_TABLE_NAME; +import static org.apache.seata.common.NamingServerConstants.REGISTRY_NAMINGSERVER_CLUSTER; + + +public class VGroupMappingDataBaseDAO { + private static final Logger LOGGER = LoggerFactory.getLogger(VGroupMappingDataBaseDAO.class); + + protected DataSource vGroupMappingDataSource = null; + + protected final String vMapping; + + protected static final Configuration CONFIG = ConfigurationFactory.getInstance(); + + public VGroupMappingDataBaseDAO(DataSource vGroupMappingDataSource) { + this.vGroupMappingDataSource = vGroupMappingDataSource; + this.vMapping = CONFIG.getConfig(MAPPING_TABLE_NAME, DEFAULT_VGROUP_MAPPING); + } + + public boolean insertMappingDO(MappingDO mappingDO) { + clearMappingDOByVGroup(mappingDO.getVGroup()); + String sql = "INSERT INTO " + vMapping + " (vgroup,namespace, cluster) VALUES (?, ?, ?)"; + Connection conn = null; + PreparedStatement ps = null; + try { + int index = 1; + conn = vGroupMappingDataSource.getConnection(); + conn.setAutoCommit(true); + ps = conn.prepareStatement(sql); + ps.setString(index++, mappingDO.getVGroup()); + ps.setString(index++, mappingDO.getNamespace()); + ps.setString(index++, mappingDO.getCluster()); + + return ps.executeUpdate() > 0; + } catch (SQLException e) { + e.printStackTrace(); + } finally { + IOUtil.close(ps, conn); + } + return false; + } + + public boolean clearMappingDOByVGroup(String vGroup) { + String sql = "DELETE FROM " + vMapping + " WHERE vGroup = ?"; + Connection conn = null; + PreparedStatement ps = null; + try { + conn = vGroupMappingDataSource.getConnection(); + conn.setAutoCommit(true); + ps = conn.prepareStatement(sql); + ps.setString(1, vGroup); + return ps.executeUpdate() > 0; + } catch (SQLException e) { + e.printStackTrace(); + } finally { + IOUtil.close(ps, conn); + } + return false; + } + + public boolean deleteMappingDOByVGroup(String vGroup) { + String sql = "DELETE FROM " + vMapping + " WHERE vGroup = ? and cluster = ?"; + Instance instance = Instance.getInstance(); + Connection conn = null; + PreparedStatement ps = null; + try { + conn = vGroupMappingDataSource.getConnection(); + conn.setAutoCommit(true); + ps = conn.prepareStatement(sql); + ps.setString(1, vGroup); + ps.setString(2, instance.getClusterName()); + return ps.executeUpdate() > 0; + } catch (SQLException e) { + e.printStackTrace(); + } finally { + IOUtil.close(ps, conn); + } + return false; + } + + public List<MappingDO> queryMappingDO() { + String sql = "SELECT vgroup,namespace, cluster FROM " + vMapping + + " WHERE cluster = ?"; + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List<MappingDO> result = new ArrayList<>(); + + try { + conn = vGroupMappingDataSource.getConnection(); + ps = conn.prepareStatement(sql); + ps.setString(1, CONFIG.getConfig(REGISTRY_NAMINGSERVER_CLUSTER)); + rs = ps.executeQuery(); + + while (rs.next()) { + MappingDO mappingDO = new MappingDO(); + mappingDO.setNamespace(rs.getString("namespace")); + mappingDO.setCluster(rs.getString("cluster")); + mappingDO.setVGroup(rs.getString("vGroup")); + result.add(mappingDO); + } + } catch (SQLException e) { + e.printStackTrace(); Review Comment: done ########## server/src/main/java/org/apache/seata/server/storage/db/store/VGroupMappingDataBaseDAO.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.seata.server.storage.db.store; + +import org.apache.seata.common.metadata.namingserver.Instance; +import org.apache.seata.common.util.IOUtil; +import org.apache.seata.config.Configuration; +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.core.store.MappingDO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.apache.seata.common.NamingServerConstants.DEFAULT_VGROUP_MAPPING; +import static org.apache.seata.common.NamingServerConstants.MAPPING_TABLE_NAME; +import static org.apache.seata.common.NamingServerConstants.REGISTRY_NAMINGSERVER_CLUSTER; + + +public class VGroupMappingDataBaseDAO { + private static final Logger LOGGER = LoggerFactory.getLogger(VGroupMappingDataBaseDAO.class); + + protected DataSource vGroupMappingDataSource = null; + + protected final String vMapping; + + protected static final Configuration CONFIG = ConfigurationFactory.getInstance(); + + public VGroupMappingDataBaseDAO(DataSource vGroupMappingDataSource) { + this.vGroupMappingDataSource = vGroupMappingDataSource; + this.vMapping = CONFIG.getConfig(MAPPING_TABLE_NAME, DEFAULT_VGROUP_MAPPING); + } + + public boolean insertMappingDO(MappingDO mappingDO) { + clearMappingDOByVGroup(mappingDO.getVGroup()); + String sql = "INSERT INTO " + vMapping + " (vgroup,namespace, cluster) VALUES (?, ?, ?)"; + Connection conn = null; + PreparedStatement ps = null; + try { + int index = 1; + conn = vGroupMappingDataSource.getConnection(); + conn.setAutoCommit(true); + ps = conn.prepareStatement(sql); + ps.setString(index++, mappingDO.getVGroup()); + ps.setString(index++, mappingDO.getNamespace()); + ps.setString(index++, mappingDO.getCluster()); + + return ps.executeUpdate() > 0; + } catch (SQLException e) { + e.printStackTrace(); + } finally { + IOUtil.close(ps, conn); + } + return false; + } + + public boolean clearMappingDOByVGroup(String vGroup) { + String sql = "DELETE FROM " + vMapping + " WHERE vGroup = ?"; + Connection conn = null; + PreparedStatement ps = null; + try { + conn = vGroupMappingDataSource.getConnection(); + conn.setAutoCommit(true); + ps = conn.prepareStatement(sql); + ps.setString(1, vGroup); + return ps.executeUpdate() > 0; + } catch (SQLException e) { + e.printStackTrace(); Review Comment: done -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
