Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
6db1ce63
Commit
6db1ce63
authored
Oct 12, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化同步数据库
parent
1058b9e3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
91 deletions
+8
-91
DatabaseSyncServiceImpl.java
...g/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
+8
-91
No files found.
src/main/java/cn/qg/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
View file @
6db1ce63
...
...
@@ -215,102 +215,17 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
*/
@Override
public
boolean
syncDbToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
,
String
namespace
,
boolean
businessData
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
String
url
=
"jdbc:mysql://"
+
ip
+
":"
+
port
;
String
dbRedisKey
=
dbSyncPrefix
+
dbName
;
String
tableListKey
=
dbSyncPrefix
+
dbName
+
":tables"
;
Connection
connection
=
null
;
Connection
newConnection
=
null
;
PreparedStatement
preparedStatement
=
null
;
try
{
Class
.
forName
(
driver
);
// connection = DriverManager.getConnection(url, username, password);
// 删除原有数据库
// String dropDbSql = String.format("DROP DATABASE IF EXISTS %s", dbName);
// log.info("删除原有数据库:{}", dropDbSql);
// preparedStatement = connection.prepareStatement(dropDbSql);
// preparedStatement.execute();
// 重新创建数据库
// String createDbSql = redisUtils.get(dbRedisKey).toString();
// log.info("重新创建数据库:{}", createDbSql);
// preparedStatement = connection.prepareStatement(createDbSql);
// preparedStatement.execute();
// connection.close();
// 重新设置连接,并指定数据库
newConnection
=
DriverManager
.
getConnection
(
url
+
"/"
+
dbName
,
username
,
password
);
newConnection
.
setAutoCommit
(
false
);
String
tableListKey
=
dbSyncPrefix
+
dbName
+
":tables"
;
List
<
String
>
tableList
=
Arrays
.
asList
(
redisUtils
.
get
(
tableListKey
).
toString
().
split
(
"\n"
));
Statement
statement
=
newConnection
.
createStatement
();
// 循环处理每个表
for
(
String
tableName:
tableList
)
{
log
.
info
(
"开始同步表:{}"
,
tableName
);
long
dataStartTime
=
System
.
currentTimeMillis
();
syncSingleTableToDest
(
ip
,
port
,
username
,
password
,
dbName
,
tableName
,
businessData
);
// String createTableKey = dbSyncPrefix + dbName + ":" + tableName + ":create" ;
// String insertTableKey = dbSyncPrefix + dbName + ":" + tableName + ":insert" ;
// String createTableValue = redisUtils.get(createTableKey).toString();
// String insertTableValue = redisUtils.get(insertTableKey).toString();
//
// // 不为空时才执行建表语句
// if (!createTableValue.isEmpty()) {
// statement.execute(createTableValue);
// }
//
// // 不为空时才插入数据
// if (!insertTableValue.isEmpty()) {
// for (String insertSql: insertTableValue.split("\n")) {
// statement.addBatch(insertSql);
// }
// statement.executeBatch();
// statement.clearBatch();
// }
long
dataEndTime
=
System
.
currentTimeMillis
();
log
.
info
(
"{}表同步完成,共花费{}秒"
,
tableName
,
(
dataEndTime
-
dataStartTime
)
/
1000
);
}
// 判断是否需要update
if
(
dbName
.
equals
(
"cash_loan_flow"
))
{
for
(
String
sql:
cashLoanFlowSql
.
split
(
"\n"
))
{
log
.
info
(
"执行update sql: {}"
,
sql
);
statement
.
addBatch
(
sql
);
if
(
tableList
.
size
()
>
0
)
{
for
(
String
tableName:
tableList
)
{
syncSingleTableToDest
(
ip
,
port
,
username
,
password
,
dbName
,
tableName
,
businessData
);
}
statement
.
executeBatch
();
statement
.
clearBatch
();
}
if
(
dbName
.
equals
(
"contract"
))
{
for
(
String
sql:
contractSql
.
split
(
"\n"
))
{
log
.
info
(
"执行update sql: {}"
,
sql
);
statement
.
addBatch
(
sql
);
}
statement
.
executeBatch
();
statement
.
clearBatch
();
}
if
(
dbName
.
equals
(
"payment_center"
))
{
for
(
String
sql:
paymentCenterSql
.
split
(
"\n"
))
{
log
.
info
(
"执行update sql: {}"
,
sql
);
statement
.
addBatch
(
sql
);
}
statement
.
executeBatch
();
statement
.
clearBatch
();
}
newConnection
.
commit
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
if
(
newConnection
!=
null
)
{
newConnection
.
close
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
return
true
;
}
...
...
@@ -482,7 +397,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
// 仅同步表结构,并保留业务数据
if
(
businessData
)
{
log
.
info
(
"
本次
同步{}.{}表,保留业务数据"
,
dbName
,
tableName
);
log
.
info
(
"
开始
同步{}.{}表,保留业务数据"
,
dbName
,
tableName
);
// 如果已有该数据库
if
(
dbList
.
contains
(
dbName
))
{
List
<
String
>
tableList
=
getTableListByDb
(
ip
,
port
,
username
,
password
,
dbName
);
...
...
@@ -629,6 +544,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
info
(
"表{}.{}同步失败."
,
dbName
,
tableName
);
return
false
;
}
finally
{
try
{
...
...
@@ -645,6 +561,7 @@ public class DatabaseSyncServiceImpl implements DatabaseSyncService {
e
.
printStackTrace
();
}
}
log
.
info
(
"表{}.{}同步完成."
,
dbName
,
tableName
);
return
true
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment