Skip to content

Commit

Permalink
Retry if failed
Browse files Browse the repository at this point in the history
  • Loading branch information
enums committed Aug 11, 2021
1 parent 4351b3a commit 0141897
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Sources/Heze/Model/HezeMySQLDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ open class HezeMySQLDatabase: HezeObject, HezeDatabaseImpl {
}

public func query(_ sql: HezeSQLStatement) -> [HezeDatabaseRecord]? {
guard mysql.query(statement: sql) else {
guard queryWithRetryIfFailed(sql) else {
HezeLogger.shared.debug("[SQL][Failed] \(sql)")
return nil
}
Expand All @@ -77,7 +77,7 @@ open class HezeMySQLDatabase: HezeObject, HezeDatabaseImpl {

public func query(_ sqls: [HezeSQLStatement]) -> [[HezeDatabaseRecord]]? {
let sql = sqls.joined(separator: " ")
guard mysql.query(statement: sql) else {
guard queryWithRetryIfFailed(sql) else {
HezeLogger.shared.debug("[SQL][Failed] \(sql)")
return nil
}
Expand Down Expand Up @@ -175,5 +175,16 @@ open class HezeMySQLDatabase: HezeObject, HezeDatabaseImpl {
return query(sql) != nil
}

private func queryWithRetryIfFailed(_ statement: String) -> Bool {
if mysql.query(statement: statement) {
return true
}

guard mysql.ping() else {
return false
}

return mysql.query(statement: statement)
}

}

0 comments on commit 0141897

Please sign in to comment.