CMS 3D CMS Logo

SessionImpl.cc
Go to the documentation of this file.
2 #include "SessionImpl.h"
3 
4 #include <memory>
5 
6 #include "DbConnectionString.h"
7 //
8 //
9 #include "RelationalAccess/ISessionProxy.h"
10 #include "RelationalAccess/ITransaction.h"
11 
12 namespace cond {
13 
14  namespace persistency {
15 
17  public:
18  CondDBTransaction(const std::shared_ptr<coral::ISessionProxy>& coralSession) : m_session(coralSession) {}
19  ~CondDBTransaction() override {}
20 
21  void commit() override { m_session->transaction().commit(); }
22 
23  void rollback() override { m_session->transaction().rollback(); }
24 
25  bool isActive() override { return m_session->transaction().isActive(); }
26 
27  private:
28  std::shared_ptr<coral::ISessionProxy> m_session;
29  };
30 
31  SessionImpl::SessionImpl() : coralSession() {}
32 
33  SessionImpl::SessionImpl(std::shared_ptr<coral::ISessionProxy>& session, const std::string& connectionStr)
34  : coralSession(session), connectionString(connectionStr) {}
35 
37 
39  if (coralSession.get()) {
40  if (coralSession->transaction().isActive()) {
41  coralSession->transaction().rollback();
42  }
43  coralSession.reset();
44  }
45  transaction.reset();
46  }
47 
48  bool SessionImpl::isActive() const { return coralSession.get(); }
49 
50  void SessionImpl::startTransaction(bool readOnly) {
51  std::unique_lock<std::recursive_mutex> lock(transactionMutex);
52  if (!transaction.get()) {
53  coralSession->transaction().start(readOnly);
54  iovSchemaHandle = std::make_unique<IOVSchema>(coralSession->nominalSchema());
55  gtSchemaHandle = std::make_unique<GTSchema>(coralSession->nominalSchema());
56  runInfoSchemaHandle = std::make_unique<RunInfoSchema>(coralSession->nominalSchema());
57  transaction = std::make_unique<CondDBTransaction>(coralSession);
58  } else {
59  if (!readOnly)
60  throwException("An update transaction is already active.", "SessionImpl::startTransaction");
61  }
62  transaction->clients++;
63  transactionLock.swap(lock);
64  }
65 
67  std::unique_lock<std::recursive_mutex> lock;
68  lock.swap(transactionLock);
69  if (transaction) {
70  transaction->clients--;
71  if (!transaction->clients) {
72  transaction->commit();
73  transaction.reset();
74  iovSchemaHandle.reset();
75  gtSchemaHandle.reset();
76  runInfoSchemaHandle.reset();
77  }
78  }
79  }
80 
82  std::unique_lock<std::recursive_mutex> lock;
83  lock.swap(transactionLock);
84  if (transaction) {
85  transaction->rollback();
86  transaction.reset();
87  iovSchemaHandle.reset();
88  gtSchemaHandle.reset();
89  runInfoSchemaHandle.reset();
90  }
91  }
92 
93  bool SessionImpl::isTransactionActive(bool deep) const {
94  if (!transaction)
95  return false;
96  if (!deep)
97  return true;
98  return transaction->isActive();
99  }
100 
102  if (!transaction.get())
103  throwException("The transaction is not active.", "SessionImpl::openIovDb");
104  if (!transaction->iovDbOpen) {
105  transaction->iovDbExists = iovSchemaHandle->exists();
106  transaction->iovDbOpen = true;
107  }
108  if (!transaction->iovDbExists) {
109  if (policy == CREATE) {
110  iovSchemaHandle->create();
111  transaction->iovDbExists = true;
112  } else {
113  if (policy == THROW)
114  throwException("IOV Database does not exist.", "SessionImpl::openIovDb");
115  }
116  }
117  }
118 
120  if (!transaction.get())
121  throwException("The transaction is not active.", "SessionImpl::openGTDb");
122  if (!transaction->gtDbOpen) {
123  transaction->gtDbExists = gtSchemaHandle->exists();
124  transaction->gtDbOpen = true;
125  }
126  if (!transaction->gtDbExists) {
127  if (policy == CREATE) {
128  gtSchemaHandle->create();
129  transaction->gtDbExists = true;
130  } else {
131  if (policy == THROW)
132  throwException("GT Database does not exist.", "SessionImpl::openGTDb");
133  }
134  }
135  }
136 
138  if (!transaction.get())
139  throwException("The transaction is not active.", "SessionImpl::openRunInfoDb");
140  if (!transaction->runInfoDbOpen) {
141  transaction->runInfoDbExists = runInfoSchemaHandle->exists();
142  transaction->runInfoDbOpen = true;
143  }
144  if (!transaction->runInfoDbExists) {
145  throwException("RunInfo Database does not exist.", "SessionImpl::openRunInfoDb");
146  }
147  }
148 
150  if (!transaction.get())
151  throwException("The transaction is not active.", "SessionImpl::openIovDb");
152  if (!transaction->iovDbOpen) {
153  transaction->iovDbExists = iovSchemaHandle->exists();
154  transaction->iovDbOpen = true;
155  }
156  if (!transaction->gtDbOpen) {
157  transaction->gtDbExists = gtSchemaHandle->exists();
158  transaction->gtDbOpen = true;
159  }
160  if (!transaction->iovDbExists) {
161  iovSchemaHandle->create();
162  transaction->iovDbExists = true;
163  if (!transaction->gtDbExists) {
164  gtSchemaHandle->create();
165  transaction->gtDbExists = true;
166  }
167  }
168  }
169 
171 
173 
175  } // namespace persistency
176 } // namespace cond
cond::persistency::SessionImpl::openGTDb
void openGTDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:119
cond::persistency::SessionImpl::isActive
bool isActive() const
Definition: SessionImpl.cc:48
Exception.h
cond::persistency::SessionImpl::transactionLock
std::unique_lock< std::recursive_mutex > transactionLock
Definition: SessionImpl.h:77
cond::persistency::IGTSchema
Definition: IDbSchema.h:207
cond::persistency::SessionImpl::close
void close()
Definition: SessionImpl.cc:38
cond::persistency::IIOVSchema
Definition: IDbSchema.h:155
cond::persistency::CondDBTransaction::commit
void commit() override
Definition: SessionImpl.cc:21
cond::persistency::SessionImpl::rollbackTransaction
void rollbackTransaction()
Definition: SessionImpl.cc:81
cond::persistency::CondDBTransaction
Definition: SessionImpl.cc:16
cond::persistency::CondDBTransaction::isActive
bool isActive() override
Definition: SessionImpl.cc:25
cond::persistency::SessionImpl::~SessionImpl
~SessionImpl()
Definition: SessionImpl.cc:36
cond::persistency::CondDBTransaction::~CondDBTransaction
~CondDBTransaction() override
Definition: SessionImpl.cc:19
cond::persistency::SessionImpl::openRunInfoDb
void openRunInfoDb()
Definition: SessionImpl.cc:137
DbConnectionString.h
SessionImpl.h
cond::persistency::CondDBTransaction::CondDBTransaction
CondDBTransaction(const std::shared_ptr< coral::ISessionProxy > &coralSession)
Definition: SessionImpl.cc:18
cond::persistency::IRunInfoSchema
Definition: IDbSchema.h:239
cond::persistency::SessionImpl::openDb
void openDb()
Definition: SessionImpl.cc:149
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cond::persistency::CondDBTransaction::rollback
void rollback() override
Definition: SessionImpl.cc:23
cond
Definition: plugin.cc:23
cond::persistency::SessionImpl::iovSchema
IIOVSchema & iovSchema()
Definition: SessionImpl.cc:170
cond::persistency::ITransaction
Definition: SessionImpl.h:25
CommonMethods.lock
def lock()
Definition: CommonMethods.py:82
cond::persistency::CondDBTransaction::m_session
std::shared_ptr< coral::ISessionProxy > m_session
Definition: SessionImpl.cc:28
cond::persistency::SessionImpl::startTransaction
void startTransaction(bool readOnly=true)
Definition: SessionImpl.cc:50
cond::persistency::SessionImpl::iovSchemaHandle
std::unique_ptr< IIOVSchema > iovSchemaHandle
Definition: SessionImpl.h:71
cond::persistency::SessionImpl::SessionImpl
SessionImpl()
Definition: SessionImpl.cc:31
cond::persistency::SessionImpl::runInfoSchemaHandle
std::unique_ptr< IRunInfoSchema > runInfoSchemaHandle
Definition: SessionImpl.h:73
cond::persistency::SessionImpl::isTransactionActive
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:93
cond::persistency::SessionImpl::gtSchema
IGTSchema & gtSchema()
Definition: SessionImpl.cc:172
cond::persistency::SessionImpl::runInfoSchema
IRunInfoSchema & runInfoSchema()
Definition: SessionImpl.cc:174
cond::persistency::SessionImpl::transactionMutex
std::recursive_mutex transactionMutex
Definition: SessionImpl.h:76
cond::persistency::SessionImpl::coralSession
std::shared_ptr< coral::ISessionProxy > coralSession
Definition: SessionImpl.h:67
cond::persistency::SessionImpl::gtSchemaHandle
std::unique_ptr< IGTSchema > gtSchemaHandle
Definition: SessionImpl.h:72
cond::persistency::throwException
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
cond::persistency::SessionImpl::CREATE
Definition: SessionImpl.h:42
cond::persistency::SessionImpl::THROW
Definition: SessionImpl.h:42
l1RCTOmdsFedVectorProducer_cfi.connectionString
connectionString
Definition: l1RCTOmdsFedVectorProducer_cfi.py:4
cond::persistency::SessionImpl::openIovDb
void openIovDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:101
cond::persistency::SessionImpl::commitTransaction
void commitTransaction()
Definition: SessionImpl.cc:66
cond::persistency::SessionImpl::FailureOnOpeningPolicy
FailureOnOpeningPolicy
Definition: SessionImpl.h:42
cond::persistency::SessionImpl::transaction
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:70