org.tikv.common.TiDBJDBCClient Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2019 PingCAP, Inc.
*
* Licensed 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,
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tikv.common;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TiDBJDBCClient implements AutoCloseable {
private static final String UNLOCK_TABLES_SQL = "unlock tables";
private static final String SELECT_TIDB_CONFIG_SQL = "select @@tidb_config";
private static final String ENABLE_TABLE_LOCK_KEY = "enable-table-lock";
private static final Boolean ENABLE_TABLE_LOCK_DEFAULT = false;
private static final String DELAY_CLEAN_TABLE_LOCK = "delay-clean-table-lock";
private static final int DELAY_CLEAN_TABLE_LOCK_DEFAULT = 0;
private static final String TIDB_ROW_FORMAT_VERSION_SQL = "select @@tidb_row_format_version";
private static final int TIDB_ROW_FORMAT_VERSION_DEFAULT = 1;
private final Logger logger = LoggerFactory.getLogger(getClass().getName());
private final Connection connection;
public TiDBJDBCClient(Connection connection) {
this.connection = connection;
}
public boolean isEnableTableLock() throws IOException, SQLException {
Map configMap = readConfMapFromTiDB();
Object enableTableLock =
configMap.getOrDefault(ENABLE_TABLE_LOCK_KEY, ENABLE_TABLE_LOCK_DEFAULT);
return (Boolean) enableTableLock;
}
/**
* get enable-table-lock config from tidb
*
* @return Milliseconds
* @throws IOException
* @throws SQLException
*/
public int getDelayCleanTableLock() throws IOException, SQLException {
Map configMap = readConfMapFromTiDB();
Object enableTableLock =
configMap.getOrDefault(DELAY_CLEAN_TABLE_LOCK, DELAY_CLEAN_TABLE_LOCK_DEFAULT);
return (int) enableTableLock;
}
/**
* get row format version from tidb
*
* @return 1 if should not encode and write with new row format.(default) 2 if encode and write
* with new row format.(default on v4.0.0 cluster)
*/
public int getRowFormatVersion() {
try {
List> result = queryTiDBViaJDBC(TIDB_ROW_FORMAT_VERSION_SQL);
if (result.isEmpty()) {
// default set to 1
return TIDB_ROW_FORMAT_VERSION_DEFAULT;
} else {
Object version = result.get(0).get(0);
if (version instanceof String) {
return Integer.parseInt((String) version);
} else if (version instanceof Number) {
return ((Number) version).intValue();
} else {
return TIDB_ROW_FORMAT_VERSION_DEFAULT;
}
}
} catch (Exception ignored) {
return TIDB_ROW_FORMAT_VERSION_DEFAULT;
}
}
public boolean lockTableWriteLocal(String databaseName, String tableName) throws SQLException {
try (Statement tidbStmt = connection.createStatement()) {
String sql = "lock tables `" + databaseName + "`.`" + tableName + "` write local";
int result = tidbStmt.executeUpdate(sql);
return result == 0;
}
}
public boolean unlockTables() throws SQLException {
try (Statement tidbStmt = connection.createStatement()) {
int result = tidbStmt.executeUpdate(UNLOCK_TABLES_SQL);
return result == 0;
}
}
public boolean dropTable(String databaseName, String tableName) throws SQLException {
try (Statement tidbStmt = connection.createStatement()) {
String sql = "drop table if exists `" + databaseName + "`.`" + tableName + "`";
return tidbStmt.execute(sql);
}
}
private Map readConfMapFromTiDB() throws SQLException, IOException {
String configJSON = (String) queryTiDBViaJDBC(SELECT_TIDB_CONFIG_SQL).get(0).get(0);
ObjectMapper objectMapper = new ObjectMapper();
TypeReference> typeRef =
new TypeReference>() {};
return objectMapper.readValue(configJSON, typeRef);
}
public boolean isClosed() throws SQLException {
return connection.isClosed();
}
@Override
public void close() throws Exception {
connection.close();
}
private List> queryTiDBViaJDBC(String query) throws SQLException {
ArrayList> result = new ArrayList<>();
try (Statement tidbStmt = connection.createStatement()) {
ResultSet resultSet = tidbStmt.executeQuery(query);
ResultSetMetaData rsMetaData = resultSet.getMetaData();
while (resultSet.next()) {
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy