CMS 3D CMS Logo

SessionImpl.cc
Go to the documentation of this file.
4 #include "SessionImpl.h"
5 
6 #include <memory>
7 
8 #include "DbConnectionString.h"
9 //
10 //
11 #include "RelationalAccess/ISessionProxy.h"
12 #include "RelationalAccess/ITransaction.h"
13 
14 namespace cond {
15 
16  namespace persistency {
17 
19  public:
20  CondDBTransaction(const std::shared_ptr<coral::ISessionProxy>& coralSession) : m_session(coralSession) {}
21  ~CondDBTransaction() override {}
22 
23  void commit() override { m_session->transaction().commit(); }
24 
25  void rollback() override { m_session->transaction().rollback(); }
26 
27  bool isActive() override { return m_session->transaction().isActive(); }
28 
29  private:
30  std::shared_ptr<coral::ISessionProxy> m_session;
31  };
32 
33  SessionImpl::SessionImpl() : coralSession() {}
34 
35  SessionImpl::SessionImpl(std::shared_ptr<coral::ISessionProxy>& session,
36  const std::string& connectionStr,
37  const std::string& principalNm)
38  : coralSession(session), sessionHash(""), connectionString(connectionStr), principalName(principalNm) {
41  }
42 
44 
46  if (isActive()) {
47  if (coralSession->transaction().isActive()) {
49  }
50  if (!lockedTags.empty()) {
51  startTransaction(false);
54  }
55  coralSession.reset();
56  }
57  transaction.reset();
58  }
59 
60  bool SessionImpl::isActive() const { return coralSession.get(); }
61 
62  void SessionImpl::startTransaction(bool readOnly) {
63  std::unique_lock<std::recursive_mutex> lock(transactionMutex);
64  if (!transaction.get()) {
65  coralSession->transaction().start(readOnly);
66  iovSchemaHandle = std::make_unique<IOVSchema>(coralSession->nominalSchema());
67  gtSchemaHandle = std::make_unique<GTSchema>(coralSession->nominalSchema());
68  runInfoSchemaHandle = std::make_unique<RunInfoSchema>(coralSession->nominalSchema());
69  transaction = std::make_unique<CondDBTransaction>(coralSession);
70  } else {
71  if (!readOnly)
72  throwException("An update transaction is already active.", "SessionImpl::startTransaction");
73  }
74  transaction->clients++;
75  transactionLock.swap(lock);
76  }
77 
79  std::unique_lock<std::recursive_mutex> lock;
80  lock.swap(transactionLock);
81  if (transaction) {
82  transaction->clients--;
83  if (!transaction->clients) {
84  transaction->commit();
85  transaction.reset();
86  iovSchemaHandle.reset();
87  gtSchemaHandle.reset();
88  runInfoSchemaHandle.reset();
89  }
90  }
91  }
92 
94  std::unique_lock<std::recursive_mutex> lock;
95  lock.swap(transactionLock);
96  if (transaction) {
97  transaction->rollback();
98  transaction.reset();
99  iovSchemaHandle.reset();
100  gtSchemaHandle.reset();
101  runInfoSchemaHandle.reset();
102  }
103  }
104 
105  bool SessionImpl::isTransactionActive(bool deep) const {
106  if (!transaction)
107  return false;
108  if (!deep)
109  return true;
110  return transaction->isActive();
111  }
112 
116  std::string lt("-");
117  std::string action("Lock removed by session ");
118  action += sessionHash;
119  for (const auto& tag : lockedTags) {
122  boost::posix_time::microsec_clock::universal_time(),
123  cond::getUserName(),
124  cond::getHostName(),
125  cond::getCommand(),
126  action,
127  lt);
128  }
129  }
130 
132  if (!transaction.get())
133  throwException("The transaction is not active.", "SessionImpl::openIovDb");
134  if (!transaction->iovDbOpen) {
135  transaction->iovDbExists = iovSchemaHandle->exists();
136  transaction->iovDbOpen = true;
137  }
138  if (!transaction->iovDbExists) {
139  if (policy == CREATE) {
140  iovSchemaHandle->create();
141  transaction->iovDbExists = true;
142  } else {
143  if (policy == THROW)
144  throwException("IOV Database does not exist.", "SessionImpl::openIovDb");
145  }
146  }
147  }
148 
150  if (!transaction.get())
151  throwException("The transaction is not active.", "SessionImpl::openGTDb");
152  if (!transaction->gtDbOpen) {
153  transaction->gtDbExists = gtSchemaHandle->exists();
154  transaction->gtDbOpen = true;
155  }
156  if (!transaction->gtDbExists) {
157  if (policy == CREATE) {
158  gtSchemaHandle->create();
159  transaction->gtDbExists = true;
160  } else {
161  if (policy == THROW)
162  throwException("GT Database does not exist.", "SessionImpl::openGTDb");
163  }
164  }
165  }
166 
168  if (!transaction.get())
169  throwException("The transaction is not active.", "SessionImpl::openRunInfoDb");
170  if (!transaction->runInfoDbOpen) {
171  transaction->runInfoDbExists = runInfoSchemaHandle->exists();
172  transaction->runInfoDbOpen = true;
173  }
174  if (!transaction->runInfoDbExists) {
175  throwException("RunInfo Database does not exist.", "SessionImpl::openRunInfoDb");
176  }
177  }
178 
180  if (!transaction.get())
181  throwException("The transaction is not active.", "SessionImpl::openIovDb");
182  if (!transaction->iovDbOpen) {
183  transaction->iovDbExists = iovSchemaHandle->exists();
184  transaction->iovDbOpen = true;
185  }
186  if (!transaction->gtDbOpen) {
187  transaction->gtDbExists = gtSchemaHandle->exists();
188  transaction->gtDbOpen = true;
189  }
190  if (!transaction->iovDbExists) {
191  iovSchemaHandle->create();
192  transaction->iovDbExists = true;
193  if (!transaction->gtDbExists) {
194  gtSchemaHandle->create();
195  transaction->gtDbExists = true;
196  }
197  }
198  }
199 
201 
203 
205  } // namespace persistency
206 } // namespace cond
virtual void removeEntriesForCredential(const std::string &credential, int credentialType)=0
std::set< std::string > lockedTags
Definition: SessionImpl.h:73
std::shared_ptr< coral::ISessionProxy > coralSession
Definition: SessionImpl.h:69
virtual void unsetProtectionCode(const std::string &name, int code)=0
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:105
void openGTDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:149
CondDBTransaction(const std::shared_ptr< coral::ISessionProxy > &coralSession)
Definition: SessionImpl.cc:20
void openIovDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:131
static constexpr size_t COND_SESSION_HASH_SIZE
Definition: Auth.h:40
std::unique_ptr< IIOVSchema > iovSchemaHandle
Definition: SessionImpl.h:75
std::unique_ptr< IRunInfoSchema > runInfoSchemaHandle
Definition: SessionImpl.h:77
virtual ITagLogTable & tagLogTable()=0
virtual void insert(const std::string &tag, const boost::posix_time::ptime &eventTime, const std::string &userName, const std::string &hostName, const std::string &command, const std::string &action, const std::string &userText)=0
std::recursive_mutex transactionMutex
Definition: SessionImpl.h:81
std::unique_lock< std::recursive_mutex > transactionLock
Definition: SessionImpl.h:82
std::unique_ptr< IGTSchema > gtSchemaHandle
Definition: SessionImpl.h:76
static constexpr int COND_DBTAG_LOCK_ACCESS_CODE
Definition: Auth.h:45
virtual ITagAccessPermissionTable & tagAccessPermissionTable()=0
void startTransaction(bool readOnly=true)
Definition: SessionImpl.cc:62
std::string make(size_t keySize)
Definition: DecodingKey.cc:75
Definition: plugin.cc:23
std::shared_ptr< coral::ISessionProxy > m_session
Definition: SessionImpl.cc:30
IRunInfoSchema & runInfoSchema()
Definition: SessionImpl.cc:204
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:74
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
virtual ITagTable & tagTable()=0
static constexpr int COND_SESSION_HASH_CODE
Definition: Auth.h:42