BmnRoot
Loading...
Searching...
No Matches
ElogConnection.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- ElogConnection source file -----
3// -------------------------------------------------------------------------
4#include "ElogConnection.h"
5
6#include "TRandom.h"
7#include "TSystem.h"
8
9#include <chrono>
10#include <thread>
11
13
14// ----- Constructor with connection ----------------------
15ElogConnection::ElogConnection(TSQLServer* pSQLServer)
16{
17 elog_db_server = pSQLServer;
18}
19
20ElogConnection* ElogConnection::OpenConnection(const TString& connect_string,
21 const char* db_host,
22 const char* db_user,
23 const char* db_password)
24{
25 if (ElogConnection::mapConnection == nullptr)
27
28 TSQLServer* pSQLServer = nullptr;
29 itSQLServer it = ElogConnection::mapConnection->find(connect_string.Data());
30 if (it != ElogConnection::mapConnection->end())
31 pSQLServer = it->second;
32 else {
33 // try to connect 3 times
34 for (int i = 0; i < 4; i++) {
35 pSQLServer = TSQLServer::Connect(connect_string, db_user, db_password);
36 if (pSQLServer)
37 break;
38 if (i == 3) {
39 cout << "ERROR: database connection was not established (" << db_host << ") for '" << db_user
40 << "' user" << endl;
41 return nullptr;
42 }
43 unsigned int random_integer = pow(10, i) + gRandom->Integer(pow(10, i) * 2 + 1);
44 this_thread::sleep_for(chrono::seconds(random_integer));
45 }
46 // cout<<"Server info: "<<pSQLServer->ServerInfo()<<endl;
47
48 ElogConnection::mapConnection->insert(pairSQLServer(connect_string.Data(), pSQLServer));
49 }
50
51 return new ElogConnection(pSQLServer);
52}
53
54// -------------------------------------------------------------------
56{
57 // get database host and name from global variables in C++ header (elog_db_settings.h)
58 TString conString = TString::Format("pgsql://%s/%s", ELOG_DB_HOST, ELOG_DB_NAME);
59
60 // try to get dynamic database username and password from bash environment
61 const char *db_username = gSystem->Getenv("ELOG_USERNAME"), *db_password = gSystem->Getenv("ELOG_PASSWORD");
62 // if no bash variables then get from global variables in C++ header
63 if ((db_username == nullptr) || (db_password == nullptr)) {
64 db_username = ELOG_DB_USERNAME;
65 db_password = ELOG_DB_PASSWORD;
66 }
67
68 return OpenConnection(conString, ELOG_DB_HOST, db_username, db_password);
69}
70
71// -------------------------------------------------------------------
73 const char* strDBHost,
74 const char* strDBName,
75 const char* strUID,
76 const char* strPassword)
77{
78 char* db_type;
79 switch (database_type) {
80 case MySQL:
81 db_type = (char*)"mysql";
82 break;
83 case PgSQL:
84 db_type = (char*)"pgsql";
85 break;
86 default: {
87 cout << "ERROR: incorrect database type!" << endl;
88 return nullptr;
89 }
90 }
91 TString conString = TString::Format("%s://%s/%s", db_type, strDBHost, strDBName);
92 return OpenConnection(conString, strDBHost, strUID, strPassword);
93}
94
95// -------------------------------------------------------------------
97{
98 // if (elog_db_server)
99 // delete elog_db_server;
100}
int i
Definition P4_F32vec4.h:22
static ElogConnection * Open()
virtual ~ElogConnection()
static mapSQLServer * mapConnection
map< string, TSQLServer * > mapSQLServer
map< string, TSQLServer * >::iterator itSQLServer
const char *const ELOG_DB_PASSWORD
const char *const ELOG_DB_USERNAME
const char *const ELOG_DB_HOST
const char *const ELOG_DB_NAME
map< string, TSQLServer * > mapSQLServer
pair< string, TSQLServer * > pairSQLServer