CMS 3D CMS Logo

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