SessionMgr.cxx 3.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/

#include "TrigConfStorage/SessionMgr.h"

#include "RelationalAccess/AccessMode.h"
#include "RelationalAccess/IDatabaseServiceDescription.h"
#include "RelationalAccess/IDatabaseServiceSet.h"
#include "RelationalAccess/ILookupService.h"
#include "RelationalAccess/IAuthenticationService.h"
#include "RelationalAccess/IAuthenticationCredentials.h"
#include "RelationalAccess/ITypeConverter.h"
#include "RelationalAccess/IRelationalDomain.h"
#include "RelationalAccess/ConnectionService.h"
#include "RelationalAccess/IConnectionService.h"
#include "RelationalAccess/IConnectionServiceConfiguration.h"

19
20
21
#include "RelationalAccess/ISessionProxy.h"


22
23
#include "./ReplicaSorter.h"

24
25
26
27
28
29
30
31
32
33
34
35
36
#include "CoralKernel/Context.h"
#include "CoralBase/Exception.h"

#include <boost/algorithm/string/case_conv.hpp>

#include <iostream>
#include <stdlib.h>
#include <string.h>


using namespace std;
using namespace TrigConf;

37
38
SessionMgr::SessionMgr() :
   TrigConfMessaging("SessionMgr")
39
40
41
42
{}

SessionMgr::~SessionMgr() {
   closeSession();
43
44
45
46
47
   // never delete the m_replicaSorter, we will have to live with that
   // one-time memory leak. The problem is the CORAL interface that
   // does keeps a reference to the replicaSorter and keeps using it
   // but doesn't clean it up. And if we delete it too early, then
   // later access leads to a crash
48
49
50
51
52
}

void
SessionMgr::closeSession() {

53
54
55
56
57
   if(m_sessionproxy) {
      try{
         delete m_sessionproxy;
         m_sessionproxy = nullptr;
         TRG_MSG_INFO("Closing session " << m_connectionString);
58
59
      }
      catch ( coral::Exception& e ) {
60
         TRG_MSG_WARNING("CORAL exception " << e.what());
61
62
63
         throw;
      }
   }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

}

void
TrigConf::SessionMgr::buildConnectionString() {

   if(m_connectionString!="")
      return;

   if(m_dbserver=="") { 
      m_connectionString = m_dbname; 
   } else if(m_dbtype == "dblookup") { 
      m_connectionString = m_dbserver; 
   } else { 
      m_connectionString = m_dbtype + "://" + m_dbserver + "/" + m_dbname;     
   }
}


coral::ISessionProxy&
TrigConf::SessionMgr::createSession() {

   if( m_sessionproxy ) return *m_sessionproxy;

   coral::ConnectionService connSvc;
   coral::IConnectionServiceConfiguration& csc = connSvc.configuration();
   csc.setConnectionRetrialPeriod( m_retrialPeriod );
   csc.setConnectionRetrialTimeOut( m_retrialTimeout );
   csc.setConnectionTimeOut( m_connectionTimeout );

94
95
   
   if(csc.replicaSortingAlgorithm() == nullptr) { // likely to be standalone, create our own
96
      TRG_MSG_INFO("Create own ReplicaSortingAlgorithm with useFrontier=" << (m_useFrontier ? "true" : "false") << " and useSQLite=" << ( m_useSQLite ? "true" : "false"));
97
      m_replicaSorter = new TrigConf::ReplicaSorter();
98
99
      m_replicaSorter->setUseFrontier(m_useFrontier);
      m_replicaSorter->setUseSQLite(m_useSQLite);
100
101
102
      csc.setReplicaSortingAlgorithm(*m_replicaSorter);
   }

103
   buildConnectionString();
104
   TRG_MSG_INFO("Connecting to " << m_connectionString);
105
   m_sessionproxy = connSvc.connect(m_connectionString, coral::AccessMode::ReadOnly);
106

107
   TRG_MSG_INFO("Opening session " << m_connectionString << " with " << m_retrialPeriod << "/" << m_retrialTimeout << "/" << m_connectionTimeout);
108

109
   return *m_sessionproxy;
110
111
}

112

113
114
115
116
117
void
TrigConf::SessionMgr::setDbType(const std::string & s) {
   m_dbtype = boost::to_lower_copy(s);
   m_connectionString = "";
}
118

119
120
121
122
123
void
TrigConf::SessionMgr::setDbServer(const std::string & s) {
   m_dbserver = s;
   m_connectionString = "";
}
124

125
126
127
128
129
void
TrigConf::SessionMgr::setDbName(const std::string & s) { 
   m_dbname = s;
   m_connectionString = "";
}
130

131
132
133
134
135
void
TrigConf::SessionMgr::setDbUser(const std::string & s) {
   m_user = s;
   m_connectionString = "";
}
136

137
138
139
140
141
void
TrigConf::SessionMgr::setDbPassword(const std::string & s) {
   m_password = s;
   m_connectionString = "";
}
142