ATLAS Offline Software
Loading...
Searching...
No Matches
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
17IOVDbConn::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
29IOVDbConn::~IOVDbConn() = default;
30
31cool::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
73bool
76 return valid();
77}
78
79void
83
84bool
86 bool success{};
87 try {
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
136void 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
143void 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}
#define endmsg
boost::shared_ptr< CoraCoolDatabase > CoraCoolDatabasePtr
void summary(const float fread)
CoraCoolDatabasePtr m_coracoolDb
Definition IOVDbConn.h:57
CoraCoolDatabasePtr getCoraCoolDb()
void setReadOnly(const bool readOnly)
void close()
Definition IOVDbConn.cxx:80
const std::string m_connstr
Definition IOVDbConn.h:55
unsigned int m_nconn
Definition IOVDbConn.h:53
void setInactive()
MsgStream & m_log
Definition IOVDbConn.h:49
cool::IDatabasePtr getCoolDb()
Definition IOVDbConn.cxx:31
cool::IDatabasePtr m_coolDb
Definition IOVDbConn.h:56
bool m_abort
Definition IOVDbConn.h:52
bool m_readonly
Definition IOVDbConn.h:51
bool dropAndReconnect()
Definition IOVDbConn.cxx:85
bool valid() const
Definition IOVDbConn.h:60
unsigned int m_nfolder
Definition IOVDbConn.h:54
IOVDbConn(const std::string &dbname, const bool readOnly, MsgStream &msg)
Definition IOVDbConn.cxx:17
bool open()
Definition IOVDbConn.cxx:74
bool m_active
Definition IOVDbConn.h:50
MsgStream & msg
Definition testRead.cxx:32