CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SessionImpl.cc
Go to the documentation of this file.
2 #include "SessionImpl.h"
3 #include "DbConnectionString.h"
4 // for the ORA bridge
5 #include "OraDbSchema.h"
8 //-ap: also to be removed when ORA goes:
10 //
11 //
12 #include "RelationalAccess/ISessionProxy.h"
13 #include "RelationalAccess/ITransaction.h"
14 
15 namespace cond {
16 
17  namespace persistency {
18 
20  public:
21  CondDBTransaction( const boost::shared_ptr<coral::ISessionProxy>& coralSession ):
22  m_session( coralSession ){
23  }
24  virtual ~CondDBTransaction(){}
25 
26  void commit(){
27  m_session->transaction().commit();
28  }
29 
30  void rollback(){
31  m_session->transaction().rollback();
32  }
33 
34  bool isActive(){
35  return m_session->transaction().isActive();
36  }
37  private:
38  boost::shared_ptr<coral::ISessionProxy> m_session;
39  };
40 
41  class OraTransaction : public ITransaction {
42  public:
44  m_session( session ){
45  isOra = true;
46  }
47  virtual ~OraTransaction(){}
48 
49  void commit(){
51  }
52 
53  void rollback(){
55  }
56  bool isActive() {
57  return m_session.transaction().isActive();
58  }
59  private:
61  };
62 
63  BackendType checkBackendType( boost::shared_ptr<coral::ISessionProxy>& coralSession,
64  const std::string& connectionString ){
66  cond::DbSession oraSession;
67  oraSession.open( coralSession, connectionString );
68  oraSession.transaction().start( true );
69  std::unique_ptr<IIOVSchema> iovSchemaHandle( new OraIOVSchema( oraSession ) );
70  std::unique_ptr<IGTSchema> gtSchemaHandle( new OraGTSchema( oraSession ) );
71  if( !iovSchemaHandle->exists() && !gtSchemaHandle->exists() ){
72  iovSchemaHandle.reset( new IOVSchema( coralSession->nominalSchema() ) );
73  ret = COND_DB;
74  } else {
75  ret = ORA_DB;
76  }
77  oraSession.transaction().commit();
78  return ret;
79  }
80 
82  coralSession(),
83  theBackendType( UNKNOWN_DB ){
84  }
85 
86  SessionImpl::SessionImpl( boost::shared_ptr<coral::ISessionProxy>& session,
87  const std::string& connectionStr,
88  BackendType backType ):
89  coralSession( session ),
90  connectionString( connectionStr ),
91  theBackendType( backType ){
92  }
93 
95  close();
96  }
97 
99  if( coralSession.get() ){
100  if( coralSession->transaction().isActive() ){
101  coralSession->transaction().rollback();
102  }
103  coralSession.reset();
104  }
105  transaction.reset();
106  }
107 
108  bool SessionImpl::isActive() const {
109  return coralSession.get();
110  }
111 
112  void SessionImpl::startTransaction( bool readOnly ){
113  if( !transaction.get() ){
114  if ( theBackendType == ORA_DB ) {
115  cond::DbSession oraSession;
116  oraSession.open( coralSession, connectionString );
117  oraSession.transaction().start( readOnly );
118  iovSchemaHandle.reset( new OraIOVSchema( oraSession ) );
119  gtSchemaHandle.reset( new OraGTSchema( oraSession ) );
120  transaction.reset( new OraTransaction( oraSession ) );
121  } else if ( theBackendType == COND_DB ){
122  coralSession->transaction().start( readOnly );
123  iovSchemaHandle.reset( new IOVSchema( coralSession->nominalSchema() ) );
124  gtSchemaHandle.reset( new GTSchema( coralSession->nominalSchema() ) );
126  } else {
127  throwException( "No valid database found.", "SessionImpl::startTransaction" );
128  }
129  } else {
130  if(!readOnly ) throwException( "An update transaction is already active.",
131  "SessionImpl::startTransaction" );
132  }
133  transaction->clients++;
134  }
135 
137  if( transaction ) {
138  transaction->clients--;
139  if( !transaction->clients ){
140  transaction->commit();
141  transaction.reset();
142  iovSchemaHandle.reset();
143  gtSchemaHandle.reset();
144  }
145  }
146  }
147 
149  if( transaction ) {
150  transaction->rollback();
151  transaction.reset();
152  iovSchemaHandle.reset();
153  gtSchemaHandle.reset();
154  }
155  }
156 
157  bool SessionImpl::isTransactionActive( bool deep ) const {
158  if( !transaction ) return false;
159  if( !deep ) return true;
160  return transaction->isActive();
161  }
162 
164  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::openIovDb" );
165  if( !transaction->iovDbOpen ){
166  transaction->iovDbExists = iovSchemaHandle->exists();
167  transaction->iovDbOpen = true;
168  }
169  if( !transaction->iovDbExists ){
170  if( policy==CREATE ){
171  iovSchemaHandle->create();
172  transaction->iovDbExists = true;
173  } else {
174  if( policy==THROW) throwException( "IOV Database does not exist.","SessionImpl::openIovDb");
175  }
176  }
177  }
178 
180  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::open" );
181  if( !transaction->gtDbOpen ){
182  transaction->gtDbExists = gtSchemaHandle->exists();
183  transaction->gtDbOpen = true;
184  }
185  if( !transaction->gtDbExists ){
186  throwException( "GT Database does not exist.","SessionImpl::openGTDb");
187  }
188  }
189 
191  return *iovSchemaHandle;
192  }
193 
195  return *gtSchemaHandle;
196  }
197 
199  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::open" );
200  return transaction->isOra;
201  }
202 
203  }
204 }
BackendType checkBackendType(boost::shared_ptr< coral::ISessionProxy > &coralSession, const std::string &connectionString)
Definition: SessionImpl.cc:63
bool rollback()
rollback transaction
OraTransaction(const cond::DbSession &session)
Definition: SessionImpl.cc:43
DbTransaction & transaction()
Definition: DbSession.cc:208
int isActive() const
current transaction is active
void open(const std::string &connectionString, bool readOnly=false)
Definition: DbSession.cc:159
int commit()
commit transaction.
void openIovDb(FailureOnOpeningPolicy policy=THROW)
Definition: SessionImpl.cc:163
std::unique_ptr< IIOVSchema > iovSchemaHandle
Definition: SessionImpl.h:75
int start(bool readOnly=false)
start transaction
CondDBTransaction(const boost::shared_ptr< coral::ISessionProxy > &coralSession)
Definition: SessionImpl.cc:21
boost::shared_ptr< coral::ISessionProxy > m_session
Definition: SessionImpl.cc:38
boost::shared_ptr< coral::ISessionProxy > coralSession
Definition: SessionImpl.h:70
std::unique_ptr< IGTSchema > gtSchemaHandle
Definition: SessionImpl.h:76
BackendType
Definition: Types.h:23
void startTransaction(bool readOnly=true)
Definition: SessionImpl.cc:112
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:157
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:74
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11