Commit cd1a39af authored by 黎博's avatar 黎博

bug fix

parent a2f3e9ec
......@@ -119,17 +119,17 @@ public class DbSyncController {
/**
* 同步表结构
* @param ip
* @param port
* @param targetIp
* @param targetPort
* @param database
* @param table
* @return
*/
@GetMapping("/table/structure")
public JsonResult handleTableStructure(@RequestParam String ip, @RequestParam String port, @RequestParam String database, @RequestParam String table) {
public JsonResult handleTableStructure(@RequestParam String targetIp, @RequestParam String targetPort, @RequestParam String database, @RequestParam String table) {
try {
databaseSyncService.getSourceDbStructure(ip, port, username, password, database, table);
databaseSyncService.syncDbStructureToDest(ip, port, "qa", "qatest", database, table);
databaseSyncService.syncDbStructureToDest(targetIp, targetPort, "qa", "qatest", database, table);
} catch (Exception e) {
e.printStackTrace();
return JsonResult.buildErrorStateResult("创建表结构失败!", false);
......
......@@ -667,7 +667,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName;
Connection connection = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
try {
Class.forName(driver);
......@@ -682,28 +682,29 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
String tableRedisKey = dbSyncPrefix + dbName + ":" + table + ":create";
if (!redisUtils.hasKey(tableRedisKey)) {
String sql = String.format("SHOW CREATE TABLE %s", table);
statement = connection.createStatement();
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
redisUtils.set(tableRedisKey, resultSet.getString(2), 600);
}
resultSet.close();
statement.clearBatch();
preparedStatement.clearBatch();
}
}
} else {
String tableRedisKey = dbSyncPrefix + dbName + ":" + tableName + ":create";
if (!redisUtils.hasKey(tableRedisKey)) {
String sql = String.format("SHOW CREATE TABLE %s", tableName);
ResultSet resultSet = statement.executeQuery(sql);
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
redisUtils.set(tableRedisKey, resultSet.getString(2), 600);
}
resultSet.close();
}
statement.clearBatch();
preparedStatement.clearBatch();
}
} catch (Exception e) {
e.printStackTrace();
......@@ -713,14 +714,14 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
return true;
}
/**
......@@ -749,10 +750,9 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
if ("all".equals(tableName)) {
String tableListKey = dbSyncPrefix + dbName + ":tableList";
// List<String> tableList = Arrays.asList(redisUtils.get(tableListKey).toString().split(" "));
List<String> tableList = Arrays.asList(redisUtils.get(tableListKey).toString().split(" "));
// 删除key
// redisUtils.del(tableListKey);
List<String> tableList = getTableListByDb(ip, port, username, password, dbName);
redisUtils.del(tableListKey);
for (String table: tableList) {
// 首先删除表
String dropTableSql = "DROP TABLE IF EXISTS " + table;
......@@ -762,6 +762,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
// 然后同步表结构
String tableRedisKey = dbSyncPrefix + dbName + ":" + table + ":create";
String createTableSql = redisUtils.get(tableRedisKey).toString();
redisUtils.del(tableRedisKey);
statement.execute(createTableSql);
statement.clearBatch();
}
......@@ -773,6 +774,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
// 然后同步表结构
String tableRedisKey = dbSyncPrefix + dbName + ":" + tableName + ":create";
String createTableSql = redisUtils.get(tableRedisKey).toString();
redisUtils.del(tableRedisKey);
statement.execute(createTableSql);
statement.clearBatch();
}
......@@ -791,7 +793,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
e.printStackTrace();
}
}
return false;
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment