ATLAS Offline Software
IOVDbConn.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // IOVDbConn.cxx - IOVDbSvc helper class to manage connections
6 // Richard Hawkings, started 24/11/08
7 
8 #include "GaudiKernel/MsgStream.h"
9 
10 #include "CoolApplication/DatabaseSvcFactory.h"
11 #include "CoolKernel/IDatabaseSvc.h"
13 #include "RelationalAccess/ConnectionService.h"
14 
15 #include "IOVDbConn.h"
16 
17 IOVDbConn::IOVDbConn(const std::string& dbname, const bool readOnly,
18  MsgStream & msg) :
19  m_log(msg),
20  m_active(false),
21  m_readonly(readOnly),
22  m_abort(false),
23  m_nconn(0),
24  m_nfolder(0),
25  m_connstr(dbname){
26  //nop
27 }
28 
29 IOVDbConn::~IOVDbConn() = default;
30 
31 cool::IDatabasePtr IOVDbConn::getCoolDb() {
32  // only open if not already activated
33  if (not m_coolDb.get()) {
34  // check connection not already aborted
35  if (m_abort) {
36  m_log << MSG::ERROR << "COOL connection for " << m_connstr << " already aborted as invalid" << endmsg;
37  // in this case, return the null connection ptr immediately to avoid
38  // another full attempt to connect
39  return m_coolDb;
40  }
41  // open new connection
42  m_log << MSG::INFO << "Opening COOL connection for " << m_connstr <<
43  endmsg;
44  ++m_nconn;
45  cool::IDatabaseSvc& dbSvc=cool::DatabaseSvcFactory::databaseService();
46  try {
47  m_coolDb=dbSvc.openDatabase(m_connstr,m_readonly);
48  m_active=true;
49  } catch (std::exception& e) {
50  // create a new COOL conditions DB
51  m_log << MSG::INFO << "*** COOL exception caught: " << e.what() << endmsg;
52  m_log << MSG::INFO << "Create a new conditions database: " << m_connstr<< endmsg;
53  try {
54  m_coolDb=dbSvc.createDatabase(m_connstr);
55  m_active=true;
56  } catch (std::exception&e ) {
57  m_log << MSG::ERROR << "*** COOL exception caught: " << e.what() << endmsg;
58  m_log << MSG::ERROR << "Could not create a new conditions database - abort connection"<< endmsg;
59  m_abort=true;
60  m_coolDb.reset();
61  }
62  }
63  }
64  return m_coolDb;
65 }
66 
67 bool
69  m_coolDb = getCoolDb();
70  return valid();
71 }
72 
73 void
75  setInactive();
76 }
77 
78 bool
80  bool success{};
81  try {
82  setInactive();
83  success = open();
84  } catch (std::exception& e) {
85  m_log << MSG::WARNING << "Exception from disconnect/reconnect: " <<e.what() << endmsg;
86  // try once more to connect
87  try {
88  success = open();
89  } catch (std::exception& e) {
90  m_log << MSG::ERROR << "Cannot reconnect to database:" << e.what()<< endmsg;
91  }
92  }
93  return success;
94 }
95 
97  // only open if not already activated
98  if (not m_coracoolDb.get()) {
99  // open new connection
100  m_log << MSG::INFO << "Opening CoraCool connection for " << m_connstr << endmsg;
101  coral::ConnectionService connSvc;
103  m_coracoolDb->connect();
104  }
105  return m_coracoolDb;
106 }
107 
109  if (m_coolDb.get()) {
110  m_log << MSG::INFO << "Disconnecting from " << m_connstr << endmsg;
111  try {
112  m_coolDb->closeDatabase();
113  } catch (std::exception& e) {
114  m_log << MSG::INFO << "Exception caught when disconnecting: " <<e.what() << endmsg;
115  }
116  m_coolDb.reset();
117  }
118  if (m_coracoolDb.get()) {
119  m_log << MSG::INFO << "Disconnecting CoraCool from " << m_connstr << endmsg;
120  try {
121  m_coracoolDb->disconnect();
122  } catch (std::exception& e) {
123  m_log << MSG::INFO << "Exception caught when disconnecting CoraCool: "<< e.what() << endmsg;
124  }
125  m_coracoolDb.reset();
126  }
127  m_active=false;
128 }
129 
130 void IOVDbConn::setReadOnly(const bool readOnly) {
131  // reset readonly state, assuming different from current one
132  // close current connection if required
133  setInactive();
134  m_readonly=readOnly;
135 }
136 
137 void IOVDbConn::summary(const float fread) {
138  // print summary of usage
139  m_log << MSG::INFO << "Connection " << m_connstr << " : nConnect: " <<
140  m_nconn << " nFolders: " << m_nfolder << " ReadTime: (( " << std::fixed <<
141  std::setw(8) << std::setprecision(2) << fread << " ))s" << endmsg;
142 }
IOVDbConn::m_log
MsgStream & m_log
Definition: IOVDbConn.h:49
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
CaloNoise_fillDB.dbname
dbname
Definition: CaloNoise_fillDB.py:43
CoraCoolDatabase
Definition: CoraCoolDatabase.h:24
IOVDbConn::m_coracoolDb
CoraCoolDatabasePtr m_coracoolDb
Definition: IOVDbConn.h:57
IOVDbConn::open
bool open()
Definition: IOVDbConn.cxx:68
IOVDbConn.h
IOVDbConn::m_nconn
unsigned int m_nconn
Definition: IOVDbConn.h:53
IOVDbConn::close
void close()
Definition: IOVDbConn.cxx:74
IOVDbConn::valid
bool valid() const
Definition: IOVDbConn.h:60
IOVDbConn::m_readonly
bool m_readonly
Definition: IOVDbConn.h:51
IOVDbConn::m_nfolder
unsigned int m_nfolder
Definition: IOVDbConn.h:54
IOVDbConn::m_coolDb
cool::IDatabasePtr m_coolDb
Definition: IOVDbConn.h:56
IOVDbConn::getCoolDb
cool::IDatabasePtr getCoolDb()
Definition: IOVDbConn.cxx:31
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
calibdata.exception
exception
Definition: calibdata.py:496
IOVDbConn::~IOVDbConn
~IOVDbConn()
IOVDbConn::m_abort
bool m_abort
Definition: IOVDbConn.h:52
IOVDbConn::IOVDbConn
IOVDbConn(const std::string &dbname, const bool readOnly, MsgStream &msg)
Definition: IOVDbConn.cxx:17
CaloNoise_fillDB.dbSvc
dbSvc
Definition: CaloNoise_fillDB.py:108
IOVDbConn::summary
void summary(const float fread)
Definition: IOVDbConn.cxx:137
IOVDbConn::setInactive
void setInactive()
Definition: IOVDbConn.cxx:108
CoraCoolDatabasePtr
boost::shared_ptr< CoraCoolDatabase > CoraCoolDatabasePtr
Definition: CoraCoolTypes.h:12
IOVDbConn::m_active
bool m_active
Definition: IOVDbConn.h:50
IOVDbConn::setReadOnly
void setReadOnly(const bool readOnly)
Definition: IOVDbConn.cxx:130
IOVDbConn::m_connstr
const std::string m_connstr
Definition: IOVDbConn.h:55
CoraCoolDatabase.h
IOVDbConn::dropAndReconnect
bool dropAndReconnect()
Definition: IOVDbConn.cxx:79
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
IOVDbConn::getCoraCoolDb
CoraCoolDatabasePtr getCoraCoolDb()
Definition: IOVDbConn.cxx:96