ATLAS Offline Software
SessionMgr.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 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  closeSession();
43  // never delete the m_replicaSorter, we will have to live with that
44  // one-time memory leak. The problem is the CORAL interface that
45  // does keeps a reference to the replicaSorter and keeps using it
46  // but doesn't clean it up. And if we delete it too early, then
47  // later access leads to a crash
48 }
49 
50 void
52 
53  if(m_sessionproxy) {
54  try{
55  delete m_sessionproxy;
56  m_sessionproxy = nullptr;
57  TRG_MSG_INFO("Closing session " << m_connectionString);
58  }
59  catch ( coral::Exception& e ) {
60  TRG_MSG_WARNING("CORAL exception " << e.what());
61  throw;
62  }
63  }
64 
65 }
66 
67 void
69 
70  if(m_connectionString!="")
71  return;
72 
73  if(m_dbserver=="") {
74  m_connectionString = m_dbname;
75  } else if(m_dbtype == "dblookup") {
76  m_connectionString = m_dbserver;
77  } else {
78  m_connectionString = m_dbtype + "://" + m_dbserver + "/" + m_dbname;
79  }
80 }
81 
82 
83 coral::ISessionProxy&
85 
86  if( m_sessionproxy ) return *m_sessionproxy;
87 
88  coral::ConnectionService connSvc;
89  coral::IConnectionServiceConfiguration& csc = connSvc.configuration();
90  csc.setConnectionRetrialPeriod( m_retrialPeriod );
91  csc.setConnectionRetrialTimeOut( m_retrialTimeout );
92  csc.setConnectionTimeOut( m_connectionTimeout );
93 
94 
95  if(csc.replicaSortingAlgorithm() == nullptr) { // likely to be standalone, create our own
96  TRG_MSG_INFO("Create own ReplicaSortingAlgorithm");
97  m_replicaSorter = new TrigConf::ReplicaSorter();
98  csc.setReplicaSortingAlgorithm(*m_replicaSorter);
99  }
100 
101  buildConnectionString();
102  TRG_MSG_INFO("Connecting to " << m_connectionString);
103  m_sessionproxy = connSvc.connect(m_connectionString, coral::AccessMode::ReadOnly);
104  TRG_MSG_INFO("Opening session " << m_connectionString << " with " << m_retrialPeriod << "/" << m_retrialTimeout << "/" << m_connectionTimeout);
105 
106  return *m_sessionproxy;
107 }
108 
109 
110 void
111 TrigConf::SessionMgr::setDbType(const std::string & s) {
112  m_dbtype = boost::to_lower_copy(s);
113  m_connectionString = "";
114 }
115 
116 void
117 TrigConf::SessionMgr::setDbServer(const std::string & s) {
118  m_dbserver = s;
119  m_connectionString = "";
120 }
121 
122 void
123 TrigConf::SessionMgr::setDbName(const std::string & s) {
124  m_dbname = s;
125  m_connectionString = "";
126 }
127 
128 void
129 TrigConf::SessionMgr::setDbUser(const std::string & s) {
130  m_user = s;
131  m_connectionString = "";
132 }
133 
134 void
136  m_password = s;
137  m_connectionString = "";
138 }
139 
TrigConf::SessionMgr::m_connectionString
std::string m_connectionString
connection string
Definition: SessionMgr.h:83
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
TrigConf::SessionMgr::setDbServer
void setDbServer(const std::string &s)
Definition: SessionMgr.cxx:117
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:123
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:84
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
TrigConf::SessionMgr::setDbPassword
void setDbPassword(const std::string &s)
Definition: SessionMgr.cxx:135
TrigConf::SessionMgr::closeSession
void closeSession()
close open sessions
Definition: SessionMgr.cxx:51
TrigConf::SessionMgr::setDbUser
void setDbUser(const std::string &s)
Definition: SessionMgr.cxx:129
TrigConf::SessionMgr::buildConnectionString
void buildConnectionString()
Definition: SessionMgr.cxx:68
TrigConf::SessionMgr::setDbType
void setDbType(const std::string &s)
Definition: SessionMgr.cxx:111