ATLAS Offline Software
SessionMgr.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include "RelationalAccess/AccessMode.h"
8 #include "RelationalAccess/IDatabaseServiceDescription.h"
9 #include "RelationalAccess/IDatabaseServiceSet.h"
10 #include "RelationalAccess/ILookupService.h"
11 #include "RelationalAccess/IAuthenticationService.h"
12 #include "RelationalAccess/IAuthenticationCredentials.h"
13 #include "RelationalAccess/ITypeConverter.h"
14 #include "RelationalAccess/IRelationalDomain.h"
15 #include "RelationalAccess/ConnectionService.h"
16 #include "RelationalAccess/IConnectionService.h"
17 #include "RelationalAccess/IConnectionServiceConfiguration.h"
18 
19 #include "RelationalAccess/ISessionProxy.h"
20 
21 
22 #include "./ReplicaSorter.h"
23 
24 #include "CoralKernel/Context.h"
25 #include "CoralBase/Exception.h"
26 
27 #include <boost/algorithm/string/case_conv.hpp>
28 
29 #include <iostream>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 
34 using namespace std;
35 using namespace TrigConf;
36 
37 SessionMgr::SessionMgr() :
38  TrigConfMessaging("SessionMgr")
39 {}
40 
42  // Dtors are noexcept by default, so if closeSession() throws,
43  // std::terminate will be called. However, cppcheck warns here
44  // about the uncaught exception, so do the std::terminate call
45  // explicitly.
46  try {
47  closeSession();
48  } catch (...) {
49  std::terminate();
50  }
51  // never delete the m_replicaSorter, we will have to live with that
52  // one-time memory leak. The problem is the CORAL interface that
53  // does keeps a reference to the replicaSorter and keeps using it
54  // but doesn't clean it up. And if we delete it too early, then
55  // later access leads to a crash
56 }
57 
58 void
60 
61  if(m_sessionproxy) {
62  try{
63  delete m_sessionproxy;
64  m_sessionproxy = nullptr;
65  TRG_MSG_INFO("Closing session " << m_connectionString);
66  }
67  catch ( coral::Exception& e ) {
68  TRG_MSG_WARNING("CORAL exception " << e.what());
69  throw;
70  }
71  }
72 
73 }
74 
75 void
77 
78  if(m_connectionString!="")
79  return;
80 
81  if(m_dbserver=="") {
82  m_connectionString = m_dbname;
83  } else if(m_dbtype == "dblookup") {
84  m_connectionString = m_dbserver;
85  } else {
86  m_connectionString = m_dbtype + "://" + m_dbserver + "/" + m_dbname;
87  }
88 }
89 
90 
91 coral::ISessionProxy&
93 
94  if( m_sessionproxy ) return *m_sessionproxy;
95 
96  coral::ConnectionService connSvc;
97  coral::IConnectionServiceConfiguration& csc = connSvc.configuration();
98  csc.setConnectionRetrialPeriod( m_retrialPeriod );
99  csc.setConnectionRetrialTimeOut( m_retrialTimeout );
100  csc.setConnectionTimeOut( m_connectionTimeout );
101 
102 
103  if(csc.replicaSortingAlgorithm() == nullptr) { // likely to be standalone, create our own
104  TRG_MSG_INFO("Create own ReplicaSortingAlgorithm");
105  m_replicaSorter = new TrigConf::ReplicaSorter();
106  csc.setReplicaSortingAlgorithm(*m_replicaSorter);
107  }
108 
109  buildConnectionString();
110  TRG_MSG_INFO("Connecting to " << m_connectionString);
111  m_sessionproxy = connSvc.connect(m_connectionString, coral::AccessMode::ReadOnly);
112  TRG_MSG_INFO("Opening session " << m_connectionString << " with " << m_retrialPeriod << "/" << m_retrialTimeout << "/" << m_connectionTimeout);
113 
114  return *m_sessionproxy;
115 }
116 
117 
118 void
119 TrigConf::SessionMgr::setDbType(const std::string & s) {
120  m_dbtype = boost::to_lower_copy(s);
121  m_connectionString = "";
122 }
123 
124 void
125 TrigConf::SessionMgr::setDbServer(const std::string & s) {
126  m_dbserver = s;
127  m_connectionString = "";
128 }
129 
130 void
131 TrigConf::SessionMgr::setDbName(const std::string & s) {
132  m_dbname = s;
133  m_connectionString = "";
134 }
135 
136 void
137 TrigConf::SessionMgr::setDbUser(const std::string & s) {
138  m_user = s;
139  m_connectionString = "";
140 }
141 
142 void
144  m_password = s;
145  m_connectionString = "";
146 }
147 
TrigConf::SessionMgr::m_connectionString
std::string m_connectionString
connection string
Definition: SessionMgr.h:83
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
TrigConf::SessionMgr::setDbServer
void setDbServer(const std::string &s)
Definition: SessionMgr.cxx:125
TrigConf::ReplicaSorter
Definition: Trigger/TrigConfiguration/TrigConfStorage/src/ReplicaSorter.h:14
TrigConf::SessionMgr::m_sessionproxy
coral::ISessionProxy * m_sessionproxy
the coral database session
Definition: SessionMgr.h:81
SessionMgr.h
TrigConf::SessionMgr::setDbName
void setDbName(const std::string &s)
Definition: SessionMgr.cxx:131
TrigConf
Forward iterator to traverse the main components of the trigger configuration.
Definition: Config.h:22
ReplicaSorter.h
TRG_MSG_INFO
#define TRG_MSG_INFO(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:27
TRG_MSG_WARNING
#define TRG_MSG_WARNING(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:28
TrigConf::SessionMgr::~SessionMgr
virtual ~SessionMgr() override
destructor
Definition: SessionMgr.cxx:41
TrigConf::TrigConfMessaging
Class to provide easy access to TrigConf::MsgStream for TrigConf classes.
Definition: TrigConfMessaging.h:28
TrigConf::SessionMgr::createSession
coral::ISessionProxy & createSession()
instantiates the session
Definition: SessionMgr.cxx:92
TrigConf::SessionMgr::setDbPassword
void setDbPassword(const std::string &s)
Definition: SessionMgr.cxx:143
TrigConf::SessionMgr::closeSession
void closeSession()
close open sessions
Definition: SessionMgr.cxx:59
TrigConf::SessionMgr::setDbUser
void setDbUser(const std::string &s)
Definition: SessionMgr.cxx:137
TrigConf::SessionMgr::buildConnectionString
void buildConnectionString()
Definition: SessionMgr.cxx:76
TrigConf::SessionMgr::setDbType
void setDbType(const std::string &s)
Definition: SessionMgr.cxx:119