Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 << endmsg;
43  ++m_nconn;
44  cool::IDatabaseSvc& dbSvc = cool::DatabaseSvcFactory::databaseService();
45  try {
46  m_coolDb = dbSvc.openDatabase(m_connstr, m_readonly);
47  m_active = true;
48  } catch (std::exception& e) {
49  // create a new COOL conditions DB
50  if (!m_readonly) {
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  } else { //read-only-case:
63  m_log << MSG::ERROR << "*** COOL exception caught: " << e.what() << endmsg;
64  m_log << MSG::ERROR << "*** Cannot open database connection [" << m_connstr << "] in " << (m_readonly ? "readonly" : "writing") << " mode - abort connection" << endmsg;
65  m_abort = true;
66  m_coolDb.reset();
67  }
68  }// end catch
69  }
70  return m_coolDb;
71 }
72 
73 bool
75  m_coolDb = getCoolDb();
76  return valid();
77 }
78 
79 void
81  setInactive();
82 }
83 
84 bool
86  bool success{};
87  try {
88  setInactive();
89  success = open();
90  } catch (std::exception& e) {
91  m_log << MSG::WARNING << "Exception from disconnect/reconnect: " <<e.what() << endmsg;
92  // try once more to connect
93  try {
94  success = open();
95  } catch (std::exception& e) {
96  m_log << MSG::ERROR << "Cannot reconnect to database:" << e.what()<< endmsg;
97  }
98  }
99  return success;
100 }
101 
103  // only open if not already activated
104  if (not m_coracoolDb.get()) {
105  // open new connection
106  m_log << MSG::INFO << "Opening CoraCool connection for " << m_connstr << endmsg;
107  coral::ConnectionService connSvc;
109  m_coracoolDb->connect();
110  }
111  return m_coracoolDb;
112 }
113 
115  if (m_coolDb.get()) {
116  m_log << MSG::INFO << "Disconnecting from " << m_connstr << endmsg;
117  try {
118  m_coolDb->closeDatabase();
119  } catch (std::exception& e) {
120  m_log << MSG::INFO << "Exception caught when disconnecting: " <<e.what() << endmsg;
121  }
122  m_coolDb.reset();
123  }
124  if (m_coracoolDb.get()) {
125  m_log << MSG::INFO << "Disconnecting CoraCool from " << m_connstr << endmsg;
126  try {
127  m_coracoolDb->disconnect();
128  } catch (std::exception& e) {
129  m_log << MSG::INFO << "Exception caught when disconnecting CoraCool: "<< e.what() << endmsg;
130  }
131  m_coracoolDb.reset();
132  }
133  m_active=false;
134 }
135 
136 void IOVDbConn::setReadOnly(const bool readOnly) {
137  // reset readonly state, assuming different from current one
138  // close current connection if required
139  setInactive();
140  m_readonly=readOnly;
141 }
142 
143 void IOVDbConn::summary(const float fread) {
144  // print summary of usage
145  m_log << MSG::INFO << "Connection " << m_connstr << " : nConnect: " <<
146  m_nconn << " nFolders: " << m_nfolder << " ReadTime: (( " << std::fixed <<
147  std::setw(8) << std::setprecision(2) << fread << " ))s" << endmsg;
148 }
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:74
IOVDbConn.h
IOVDbConn::m_nconn
unsigned int m_nconn
Definition: IOVDbConn.h:53
IOVDbConn::close
void close()
Definition: IOVDbConn.cxx:80
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:143
IOVDbConn::setInactive
void setInactive()
Definition: IOVDbConn.cxx:114
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:136
IOVDbConn::m_connstr
const std::string m_connstr
Definition: IOVDbConn.h:55
CoraCoolDatabase.h
IOVDbConn::dropAndReconnect
bool dropAndReconnect()
Definition: IOVDbConn.cxx:85
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
IOVDbConn::getCoraCoolDb
CoraCoolDatabasePtr getCoraCoolDb()
Definition: IOVDbConn.cxx:102