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 ):
16  m_session( coralSession ){
17  }
18  ~CondDBTransaction() override{}
19 
20  void commit() override{
21  m_session->transaction().commit();
22  }
23 
24  void rollback() override{
25  m_session->transaction().rollback();
26  }
27 
28  bool isActive() override{
29  return m_session->transaction().isActive();
30  }
31  private:
32  std::shared_ptr<coral::ISessionProxy> m_session;
33  };
34 
36  coralSession(){
37  }
38 
39  SessionImpl::SessionImpl( std::shared_ptr<coral::ISessionProxy>& session,
40  const std::string& connectionStr ):
41  coralSession( session ),
42  connectionString( connectionStr ){
43  }
44 
46  close();
47  }
48 
50  if( coralSession.get() ){
51  if( coralSession->transaction().isActive() ){
52  coralSession->transaction().rollback();
53  }
54  coralSession.reset();
55  }
56  transaction.reset();
57  }
58 
59  bool SessionImpl::isActive() const {
60  return coralSession.get();
61  }
62 
63  void SessionImpl::startTransaction( bool readOnly ){
64  if( !transaction.get() ){
65  coralSession->transaction().start( readOnly );
66  iovSchemaHandle.reset( new IOVSchema( coralSession->nominalSchema() ) );
67  gtSchemaHandle.reset( new GTSchema( coralSession->nominalSchema() ) );
68  runInfoSchemaHandle.reset( new RunInfoSchema( coralSession->nominalSchema() ) );
70  } else {
71  if(!readOnly ) throwException( "An update transaction is already active.",
72  "SessionImpl::startTransaction" );
73  }
74  transaction->clients++;
75  }
76 
78  if( transaction ) {
79  transaction->clients--;
80  if( !transaction->clients ){
81  transaction->commit();
82  transaction.reset();
83  iovSchemaHandle.reset();
84  gtSchemaHandle.reset();
85  runInfoSchemaHandle.reset();
86  }
87  }
88  }
89 
91  if( transaction ) {
92  transaction->rollback();
93  transaction.reset();
94  iovSchemaHandle.reset();
95  gtSchemaHandle.reset();
96  runInfoSchemaHandle.reset();
97  }
98  }
99 
100  bool SessionImpl::isTransactionActive( bool deep ) const {
101  if( !transaction ) return false;
102  if( !deep ) return true;
103  return transaction->isActive();
104  }
105 
107  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::openIovDb" );
108  if( !transaction->iovDbOpen ){
109  transaction->iovDbExists = iovSchemaHandle->exists();
110  transaction->iovDbOpen = true;
111  }
112  if( !transaction->iovDbExists ){
113  if( policy==CREATE ){
114  iovSchemaHandle->create();
115  transaction->iovDbExists = true;
116  } else {
117  if( policy==THROW) throwException( "IOV Database does not exist.","SessionImpl::openIovDb");
118  }
119  }
120  }
121 
123  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::openGTDb" );
124  if( !transaction->gtDbOpen ){
125  transaction->gtDbExists = gtSchemaHandle->exists();
126  transaction->gtDbOpen = true;
127  }
128  if( !transaction->gtDbExists ){
129  if( policy==CREATE ){
130  gtSchemaHandle->create();
131  transaction->gtDbExists = true;
132  } else {
133  if( policy==THROW) throwException( "GT Database does not exist.","SessionImpl::openGTDb");
134  }
135  }
136  }
137 
139  if(!transaction.get()) 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()) throwException( "The transaction is not active.","SessionImpl::openIovDb" );
151  if( !transaction->iovDbOpen ){
152  transaction->iovDbExists = iovSchemaHandle->exists();
153  transaction->iovDbOpen = true;
154  }
155  if( !transaction->gtDbOpen ){
156  transaction->gtDbExists = gtSchemaHandle->exists();
157  transaction->gtDbOpen = true;
158  }
159  if( !transaction->iovDbExists ){
160  iovSchemaHandle->create();
161  transaction->iovDbExists = true;
162  if( !transaction->gtDbExists ){
163  gtSchemaHandle->create();
164  transaction->gtDbExists = true;
165  }
166  }
167  }
168 
170  return *iovSchemaHandle;
171  }
172 
174  return *gtSchemaHandle;
175  }
176 
178  return *runInfoSchemaHandle;
179  }
180  }
181 }
std::shared_ptr< coral::ISessionProxy > coralSession
Definition: SessionImpl.h:66
void openGTDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:122
CondDBTransaction(const std::shared_ptr< coral::ISessionProxy > &coralSession)
Definition: SessionImpl.cc:15
void openIovDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:106
std::unique_ptr< IIOVSchema > iovSchemaHandle
Definition: SessionImpl.h:70
std::unique_ptr< IRunInfoSchema > runInfoSchemaHandle
Definition: SessionImpl.h:72
std::unique_ptr< IGTSchema > gtSchemaHandle
Definition: SessionImpl.h:71
void startTransaction(bool readOnly=true)
Definition: SessionImpl.cc:63
Definition: plugin.cc:24
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:100
std::shared_ptr< coral::ISessionProxy > m_session
Definition: SessionImpl.cc:32
IRunInfoSchema & runInfoSchema()
Definition: SessionImpl.cc:177
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:69
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:14