-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cpp
340 lines (287 loc) · 11.7 KB
/
Database.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
#include <Core/Utils/Log.h>
#include "Database.h"
#include "Configure.h"
//
// class SqlConnectionHolder
//
//
// class Database
//
Database::Database()
{
m_pConnection = NULL;
m_nMaxConnectionCount = 30;
m_nCacheConnectionCount = 10;
}
Database::~Database()
{
assert(NULL == m_pConnection);
assert(m_nConnectionPool.empty());
}
bool Database::Initialize()
{
assert(NULL == m_pConnection);
int nErrorCode = 0;
std::string strError;
DatabaseConfigure &nDbConfig = Configure::Instance().m_nDatabase;
__ULOG_INFO(__ULOG_FMT("Database", "Connecting to server(%s), user(%s)..."), nDbConfig.m_strUrl.c_str(), nDbConfig.m_strUser.c_str());
m_pConnection = Storage::SqlDriverManager::Instance().Connect(nDbConfig.m_strUrl, nDbConfig.m_strUser, nDbConfig.m_strPassword, nErrorCode, strError);
if( NULL == m_pConnection )
{
__ULOG_ERROR(__ULOG_FMT_ERR("Database", "Connect failed"), (uint32)nErrorCode, strError.c_str());
return false;
}
m_pConnection->SetLobColumns(nDbConfig.m_nLobColumns);
m_pConnection->SetShowSqlLog(nDbConfig.m_bShowSqlLog);
__ULOG_INFO(__ULOG_FMT("Database", "Connected"));
if( m_nMaxConnectionCount <= 0 ) m_nMaxConnectionCount = 20;
else if( m_nMaxConnectionCount > 1000 ) m_nMaxConnectionCount = 1000;
if( m_nCacheConnectionCount <= 0 ) m_nCacheConnectionCount = 5;
if( m_nCacheConnectionCount > m_nMaxConnectionCount ) m_nCacheConnectionCount = m_nMaxConnectionCount;
__ULOG_INFO(__ULOG_FMT("Database", "Maximum connection count: "_SIZE_TFMT_), m_nMaxConnectionCount);
__ULOG_INFO(__ULOG_FMT("Database", "Cache connection count: "_SIZE_TFMT_), m_nCacheConnectionCount);
__ULOG_INFO(__ULOG_FMT("Database", "Multi table enabled: %s"), nDbConfig.m_bMultiTableEnabled ? "yes" : "no");
__ULOG_INFO(__ULOG_FMT("Database", "Multi table time: %s"), nDbConfig.m_nMultiTableTime > 0 ? Utils::String::FormatMicroTime("Y-m-d H:i:s", nDbConfig.m_nMultiTableTime).c_str() : "disabled");
return true;
}
bool Database::Exit()
{
if( NULL != m_pConnection )
{
m_pConnection->Close();
m_pConnection = Storage::SqlDriverManager::Instance().Release(m_pConnection);
}
for ( size_t i = 0;i < m_nResultConnection.size(); i++)
{
if( NULL != m_nResultConnection[i] )
{
m_nResultConnection[i]->Close();
m_nResultConnection[i] = Storage::SqlDriverManager::Instance().Release(m_nResultConnection[i]);
}
}
for( Storage::SqlConnectionPtrList::iterator itr = m_nConnectionPool.begin(); itr != m_nConnectionPool.end(); itr ++ )
{
Storage::SqlConnection *pConnection = (*itr);
if( pConnection->GetRefCount() > 0 )
{
__ULOG_WARNING(__ULOG_FMT("Database", "Cache connection busy(%d)"), pConnection->GetRefCount());
}
pConnection = Storage::SqlDriverManager::Instance().Release(pConnection);
}
m_nConnectionPool.clear();
return true;
}
Storage::SqlConnection *Database::AccuireConnection( int nPrivilegeLevel )
{
Utils::AutoLock __access__(m_nPoolMutex);
for( Storage::SqlConnectionPtrList::iterator itr = m_nConnectionPool.begin(); itr != m_nConnectionPool.end(); itr ++ )
{
Storage::SqlConnection *pConnection = (*itr);
if( pConnection->GetRefCount() == 0 &&
pConnection->GetPrivilegeLevel() == nPrivilegeLevel )
{
pConnection->AddRef();
return pConnection;
}
}
if( m_nConnectionPool.size() >= m_nMaxConnectionCount )
{
__ULOG_WARNING(__ULOG_FMT("Database","Connection is too many ,MaxConnectionCount = " _U64FMT_ ), m_nMaxConnectionCount);
return m_pConnection;
}
DatabaseConfigure &nDbConfig = Configure::Instance().m_nDatabase;
std::string strUserName = nDbConfig.m_strUser;
std::string strPassword = nDbConfig.m_strPassword;
std::string strUrl = nDbConfig.m_strUrl;
if ( strUserName.empty() )
{
__ULOG_TRACE(__ULOG_FMT("Database", "Database usename is empty, allocate new connection failed"));
return NULL;
}
int nErrorCode = 0;
std::string strError;
Storage::SqlConnection *pConnection = Storage::SqlDriverManager::Instance().Connect(strUrl, strUserName, strPassword, nErrorCode, strError);
if( NULL == pConnection )
{
__ULOG_ERROR(__ULOG_FMT_ERR("Database", "Allocate new connection failed"), (uint32)nErrorCode, strError.c_str());
return NULL;
}
// set default attributes
pConnection->SetLobColumns(nDbConfig.m_nLobColumns);
pConnection->SetShowSqlLog(nDbConfig.m_bShowSqlLog);
pConnection->AddRef();
pConnection->SetPrivilegeLevel(nPrivilegeLevel);
m_nConnectionPool.push_back(pConnection);
__ULOG_TRACE(__ULOG_FMT("Database", "Allocated new connection, now "_SIZE_TFMT_" connection(s)"), m_nConnectionPool.size());
return pConnection;
}
Storage::SqlConnection *Database::RecycleConnection( Storage::SqlConnection *pConnection )
{
Utils::AutoLock __access__(m_nPoolMutex);
bool bFound = false;
for( Storage::SqlConnectionPtrList::iterator itr = m_nConnectionPool.begin(); itr != m_nConnectionPool.end(); itr ++ )
{
Storage::SqlConnection *pItem = (*itr);
if( pItem == pConnection )
{
bFound = true;
if( pConnection->GetRefCount() > 0 ) pConnection->ReleaseRef();
if( pConnection->GetRefCount() == 0 && m_nConnectionPool.size() > m_nCacheConnectionCount )
{
pConnection->Close();
pConnection = Storage::SqlDriverManager::Instance().Release(pConnection);
m_nConnectionPool.erase(itr);
__ULOG_TRACE(__ULOG_FMT("Database", "Released a connection, now "_SIZE_TFMT_" connection(s)"), m_nConnectionPool.size());
}
break;
}
}
if( !bFound )
{
//__ULOG_ERROR(__ULOG_FMT("Database", "Recyle a wild connection item"));
//assert(false);
}
return NULL;
}
DatabaseConnectionPool::DatabaseConnectionPool( int nResidentConnectionSize /*= 5*/, int nMaxConnectionSize /*= 10*/ ):m_nResidentConnectionSize(nResidentConnectionSize),
m_nMaxConnectionSize(nMaxConnectionSize)
{
}
DatabaseConnectionPool::~DatabaseConnectionPool()
{
Utils::AutoLock __access__(m_nPoolListMutex);
for(Storage::SqlConnectionPtrList::iterator itr= m_nConnectionList.begin(); itr != m_nConnectionList.end(); )
{
(*itr)->Close();
Storage::SqlDriverManager::Instance().Release(*itr);
itr = m_nConnectionList.erase(itr);
}
}
bool DatabaseConnectionPool::Init( std::string strUrl, std::string strUserName, std::string strPassword, int nPrivilegeLevel )
{
m_nDatabaseUrl = strUrl;
m_nUserName = strUserName;
m_nPassword = strPassword;
m_nPrivilegeLevel = nPrivilegeLevel;
int nErrorCode = 0;
std::string strError;
Storage::SqlConnection *pConnection = Storage::SqlDriverManager::Instance().Connect(m_nDatabaseUrl, m_nUserName, m_nPassword, nErrorCode, strError);
if( NULL == pConnection )
{
__ULOG_ERROR(__ULOG_FMT_ERR("DatabaseConnectionPool", "Allocate new connection failed"), (uint32)nErrorCode, strError.c_str());
return false;
}
DatabaseConfigure &nDbConfig = Configure::Instance().m_nDatabase;
pConnection->SetLobColumns(nDbConfig.m_nLobColumns);
pConnection->SetShowSqlLog(nDbConfig.m_bShowSqlLog);
m_nConnectionList.push_back(pConnection);
return true;
}
Storage::SqlConnection* DatabaseConnectionPool::GetOnceConnection()
{
do
{
Utils::AutoLock __access__(m_nPoolListMutex);
for( Storage::SqlConnectionPtrList::iterator itr = m_nConnectionList.begin(); itr != m_nConnectionList.end(); itr ++ )
{
Storage::SqlConnection *pConnection = (*itr);
if( pConnection->GetRefCount() == 0 )
{
pConnection->AddRef();
return pConnection;
}
}
if( m_nConnectionList.size() >= m_nMaxConnectionSize )
{
__ULOG_WARNING(__ULOG_FMT("Database","Connection is too many ,MaxConnectionCount = " _U64FMT_ ),m_nMaxConnectionSize);
return NULL;
}
} while (false);
int nErrorCode = 0;
std::string strError;
DatabaseConfigure &nDbConfig = Configure::Instance().m_nDatabase;
Storage::SqlConnection *pConnection = Storage::SqlDriverManager::Instance().Connect(m_nDatabaseUrl, m_nUserName, m_nPassword, nErrorCode, strError);
if( NULL == pConnection )
{
__ULOG_ERROR(__ULOG_FMT_ERR("DatabaseConnectionPool", "Allocate new connection failed"), (uint32)nErrorCode, strError.c_str());
return NULL;
}
// set default attributes
pConnection->SetLobColumns(nDbConfig.m_nLobColumns);
pConnection->SetShowSqlLog(nDbConfig.m_bShowSqlLog);
pConnection->AddRef();
pConnection->SetPrivilegeLevel(m_nPrivilegeLevel);
Utils::AutoLock __access__(m_nPoolListMutex);
m_nConnectionList.push_back(pConnection);
return pConnection;
}
void DatabaseConnectionPool::RecyleConnection( Storage::SqlConnection *pConnection )
{
Utils::AutoLock __access__(m_nPoolListMutex);
if( pConnection == NULL )
{
return;
}
bool bFound = false;
for( Storage::SqlConnectionPtrList::iterator itr = m_nConnectionList.begin(); itr != m_nConnectionList.end(); itr ++ )
{
Storage::SqlConnection *pItem = (*itr);
if( pItem == pConnection )
{
bFound = true;
if( pConnection->GetRefCount() > 0 ) pConnection->ReleaseRef();
if( pConnection->GetRefCount() == 0 && m_nConnectionList.size() > m_nResidentConnectionSize )
{
pConnection->Close();
pConnection = Storage::SqlDriverManager::Instance().Release(pConnection);
m_nConnectionList.erase(itr);
__ULOG_TRACE(__ULOG_FMT("DatabaseConnectionPool", "Released a connection, now "_SIZE_TFMT_" connection(s)"), m_nConnectionList.size());
}
break;
}
}
if( !bFound )
{
__ULOG_ERROR(__ULOG_FMT("DatabaseConnectionPool", "Recyle a wild connection item"));
assert(false);
}
}
OnceConnection::OnceConnection(DatabaseConnectionPool *pConnectionPool ):m_lpConnectionPool(pConnectionPool)
{
if( m_lpConnectionPool != NULL )
{
m_lpConnection = pConnectionPool->GetOnceConnection();
}
}
OnceConnection::~OnceConnection()
{
m_lpConnectionPool->RecyleConnection(m_lpConnection);
}
bool DBUtils::CheckTable(const std::string &tableName, DatabaseConnectionPool *connectionPool)
{
OnceConnection onceConnection(connectionPool);
Storage::SqlConnection &sqlConnection = onceConnection.GetConnectionRef();
Json::Value result;
if(!sqlConnection.ShowTableStaus(tableName, result))
{
__ULOG_ERROR(__ULOG_FMT_ERR("DBUtils","check table failed "),sqlConnection.GetErrorCode(),sqlConnection.GetErrorMsg());
return false;
}
return !result.empty();
}
bool DBUtils::CreateTable(const std::string &tableName, DatabaseConnectionPool *connectionPool,
const std::string &baseName)
{
OnceConnection onceConnection(connectionPool);
Storage::SqlConnection &sqlConnection = onceConnection.GetConnectionRef();
bool bCreated =false;
bool bChanged =false;
if( !sqlConnection.SyncTableStructure(tableName, baseName, "id", bCreated, bChanged) )
{
__ULOG_ERROR(__ULOG_FMT_ERR("DBUtils", "Sync multi table(%s) from(%s) failed"),
tableName.c_str(), baseName.c_str(), (uint32)sqlConnection.GetErrorCode(), sqlConnection.GetErrorMsg());
return false;
}
return true;
}