-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_connection.cpp
More file actions
79 lines (66 loc) · 1.93 KB
/
Copy pathdatabase_connection.cpp
File metadata and controls
79 lines (66 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "database_connection.hpp"
#include <chrono>
namespace db {
DatabaseConnection::DatabaseConnection(const MySQLConfig &config)
: mysql_(nullptr), config_(config), is_connected_(false) {
// 初始化MySQL
mysql_ = mysql_init(nullptr);
if (!mysql_) {
LOG_ERROR << "Failed to initialize MySQL";
return;
}
}
DatabaseConnection::~DatabaseConnection() {
disconnect();
if (mysql_) {
LOG_INFO << "Closing MySQL connection";
mysql_close(mysql_);
}
}
bool DatabaseConnection::connect() {
if (is_connected_) {
return true;
}
if (!mysql_) {
LOG_ERROR << "MySQL connection is null";
return false;
}
// 设置字符集
if (mysql_options(mysql_, MYSQL_SET_CHARSET_NAME, "utf8mb4")) {
LOG_ERROR << "Failed to set charset option: " << mysql_error(mysql_);
return false;
}
// 设置自动重连选项
bool reconnect_flag = 1;
if (mysql_options(mysql_, MYSQL_OPT_RECONNECT, &reconnect_flag)) {
LOG_ERROR << "Failed to set reconnect option: " << mysql_error(mysql_);
return false;
}
// 连接到MySQL服务器
if (!mysql_real_connect(mysql_, config_.host.c_str(),
config_.username.c_str(), config_.password.c_str(),
config_.database.c_str(), config_.port, nullptr, 0)) {
LOG_ERROR << "Can't connect to MySQL server: " << mysql_error(mysql_);
return false;
}
// 设置自动提交
if (mysql_autocommit(mysql_, 1)) {
LOG_ERROR << "Failed to set autocommit: " << mysql_error(mysql_);
return false;
}
LOG_INFO << "Connected to MySQL server successfully with utf8mb4 charset";
is_connected_ = true;
return true;
}
void DatabaseConnection::disconnect() {
if (is_connected_) {
is_connected_ = false;
LOG_INFO << "MySQL connection is now disconnected";
}
}
bool DatabaseConnection::reconnect() {
LOG_INFO << "Reconnecting to MySQL server...";
disconnect();
return connect();
}
} // namespace db