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 (coralSession.get()) {
47  if (coralSession->transaction().isActive()) {
48  coralSession->transaction().rollback();
49  }
50  if (!lockedTags.empty()) {
51  coralSession->transaction().start(true);
53  coralSession->transaction().commit();
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
cond::persistency::SessionImpl::openGTDb
void openGTDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:149
cond::persistency::SessionImpl::isActive
bool isActive() const
Definition: SessionImpl.cc:60
Exception.h
cond::auth::COND_DBTAG_LOCK_ACCESS_CODE
static constexpr int COND_DBTAG_LOCK_ACCESS_CODE
Definition: Auth.h:38
cond::persistency::SessionImpl::releaseTagLocks
void releaseTagLocks()
Definition: SessionImpl.cc:113
cond::persistency::SessionImpl::lockedTags
std::set< std::string > lockedTags
Definition: SessionImpl.h:73
cond::persistency::SessionImpl::transactionLock
std::unique_lock< std::recursive_mutex > transactionLock
Definition: SessionImpl.h:82
cond::auth::KeyGenerator::make
std::string make(size_t keySize)
Definition: DecodingKey.cc:75
cond::persistency::IGTSchema
Definition: IDbSchema.h:194
cond::persistency::IIOVSchema::tagAccessPermissionTable
virtual ITagAccessPermissionTable & tagAccessPermissionTable()=0
cond::persistency::SessionImpl::close
void close()
Definition: SessionImpl.cc:45
cond::persistency::IIOVSchema
Definition: IDbSchema.h:141
cond::persistency::CondDBTransaction::commit
void commit() override
Definition: SessionImpl.cc:23
cond::persistency::SessionImpl::rollbackTransaction
void rollbackTransaction()
Definition: SessionImpl.cc:93
cond::persistency::SessionImpl::sessionHash
std::string sessionHash
Definition: SessionImpl.h:70
cond::persistency::CondDBTransaction
Definition: SessionImpl.cc:18
cond::persistency::CondDBTransaction::isActive
bool isActive() override
Definition: SessionImpl.cc:27
cond::persistency::SessionImpl::~SessionImpl
~SessionImpl()
Definition: SessionImpl.cc:43
cond::persistency::CondDBTransaction::~CondDBTransaction
~CondDBTransaction() override
Definition: SessionImpl.cc:21
cond::persistency::SessionImpl::openRunInfoDb
void openRunInfoDb()
Definition: SessionImpl.cc:167
cond::persistency::ITagTable::unsetProtectionCode
virtual void unsetProtectionCode(const std::string &name, int code)=0
DbConnectionString.h
cond::auth::COND_SESSION_HASH_CODE
static constexpr int COND_SESSION_HASH_CODE
Definition: Auth.h:35
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
SessionImpl.h
cond::persistency::CondDBTransaction::CondDBTransaction
CondDBTransaction(const std::shared_ptr< coral::ISessionProxy > &coralSession)
Definition: SessionImpl.cc:20
cond::persistency::IRunInfoSchema
Definition: IDbSchema.h:226
cond::persistency::SessionImpl::openDb
void openDb()
Definition: SessionImpl.cc:179
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cond::persistency::CondDBTransaction::rollback
void rollback() override
Definition: SessionImpl.cc:25
cond
Definition: plugin.cc:23
cond::persistency::IIOVSchema::tagTable
virtual ITagTable & tagTable()=0
cond::persistency::SessionImpl::iovSchema
IIOVSchema & iovSchema()
Definition: SessionImpl.cc:200
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:30
cond::persistency::SessionImpl::startTransaction
void startTransaction(bool readOnly=true)
Definition: SessionImpl.cc:62
DecodingKey.h
writedatasetfile.action
action
Definition: writedatasetfile.py:8
cond::persistency::SessionImpl::iovSchemaHandle
std::unique_ptr< IIOVSchema > iovSchemaHandle
Definition: SessionImpl.h:75
cond::persistency::ITagLogTable::insert
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
cond::persistency::IIOVSchema::tagLogTable
virtual ITagLogTable & tagLogTable()=0
cond::auth::KeyGenerator
Definition: DecodingKey.h:20
cond::persistency::SessionImpl::SessionImpl
SessionImpl()
Definition: SessionImpl.cc:33
cond::persistency::SessionImpl::runInfoSchemaHandle
std::unique_ptr< IRunInfoSchema > runInfoSchemaHandle
Definition: SessionImpl.h:77
cond::persistency::SessionImpl::isTransactionActive
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:105
cond::auth::COND_SESSION_HASH_SIZE
static constexpr size_t COND_SESSION_HASH_SIZE
Definition: Auth.h:33
cond::persistency::ITagAccessPermissionTable::removeEntriesForCredential
virtual void removeEntriesForCredential(const std::string &credential, int credentialType)=0
cond::persistency::SessionImpl::gtSchema
IGTSchema & gtSchema()
Definition: SessionImpl.cc:202
cond::persistency::SessionImpl::runInfoSchema
IRunInfoSchema & runInfoSchema()
Definition: SessionImpl.cc:204
cond::persistency::SessionImpl::transactionMutex
std::recursive_mutex transactionMutex
Definition: SessionImpl.h:81
cond::persistency::SessionImpl::coralSession
std::shared_ptr< coral::ISessionProxy > coralSession
Definition: SessionImpl.h:69
cond::persistency::SessionImpl::gtSchemaHandle
std::unique_ptr< IGTSchema > gtSchemaHandle
Definition: SessionImpl.h:76
cond::persistency::throwException
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
Auth.h
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:131
cond::persistency::SessionImpl::commitTransaction
void commitTransaction()
Definition: SessionImpl.cc:78
cond::persistency::SessionImpl::FailureOnOpeningPolicy
FailureOnOpeningPolicy
Definition: SessionImpl.h:42
cond::persistency::SessionImpl::transaction
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:74