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
std::shared_ptr< coral::ISessionProxy > coralSession
Definition: SessionImpl.h:67
void openGTDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:116
CondDBTransaction(const std::shared_ptr< coral::ISessionProxy > &coralSession)
Definition: SessionImpl.cc:15
void openIovDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:98
std::unique_ptr< IIOVSchema > iovSchemaHandle
Definition: SessionImpl.h:71
std::unique_ptr< IRunInfoSchema > runInfoSchemaHandle
Definition: SessionImpl.h:73
std::recursive_mutex transactionMutex
Definition: SessionImpl.h:76
std::unique_lock< std::recursive_mutex > transactionLock
Definition: SessionImpl.h:77
std::unique_ptr< IGTSchema > gtSchemaHandle
Definition: SessionImpl.h:72
void startTransaction(bool readOnly=true)
Definition: SessionImpl.cc:47
Definition: plugin.cc:23
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:90
std::shared_ptr< coral::ISessionProxy > m_session
Definition: SessionImpl.cc:25
IRunInfoSchema & runInfoSchema()
Definition: SessionImpl.cc:171
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:70
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12