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 //
9 #include "RelationalAccess/ISessionProxy.h"
10 #include "RelationalAccess/ITransaction.h"
11 
12 namespace cond {
13 
14  namespace persistency {
15 
17  public:
18  CondDBTransaction( const boost::shared_ptr<coral::ISessionProxy>& coralSession ):
19  m_session( coralSession ){
20  }
21  virtual ~CondDBTransaction(){}
22 
23  void commit(){
24  m_session->transaction().commit();
25  }
26 
27  void rollback(){
28  m_session->transaction().rollback();
29  }
30 
31  bool isActive(){
32  return m_session->transaction().isActive();
33  }
34  private:
35  boost::shared_ptr<coral::ISessionProxy> m_session;
36  };
37 
38  class OraTransaction : public ITransaction {
39  public:
41  m_session( session ){
42  isOra = true;
43  }
44  virtual ~OraTransaction(){}
45 
46  void commit(){
48  }
49 
50  void rollback(){
52  }
53  bool isActive() {
54  return m_session.transaction().isActive();
55  }
56  private:
58  };
59 
60  BackendType checkBackendType( boost::shared_ptr<coral::ISessionProxy>& coralSession,
61  const std::string& connectionString ){
63  cond::DbSession oraSession;
64  oraSession.open( coralSession, connectionString );
65  oraSession.transaction().start( true );
66  std::unique_ptr<IIOVSchema> iovSchemaHandle( new OraIOVSchema( oraSession ) );
67  std::unique_ptr<IGTSchema> gtSchemaHandle( new OraGTSchema( oraSession ) );
68  if( !iovSchemaHandle->exists() && !gtSchemaHandle->exists() ){
69  iovSchemaHandle.reset( new IOVSchema( coralSession->nominalSchema() ) );
70  if( iovSchemaHandle->exists() ){
71  ret = COND_DB;
72  }
73  } else {
74  ret = ORA_DB;
75  }
76  oraSession.transaction().commit();
77  return ret;
78  }
79 
81  coralSession(),
82  theBackendType( UNKNOWN_DB ){
83  }
84 
85  SessionImpl::SessionImpl( boost::shared_ptr<coral::ISessionProxy>& session,
86  const std::string& connectionStr,
87  BackendType backType ):
88  coralSession( session ),
89  connectionString( connectionStr ),
90  theBackendType( backType ){
91  }
92 
94  close();
95  }
96 
98  if( coralSession.get() ){
99  if( coralSession->transaction().isActive() ){
100  coralSession->transaction().rollback();
101  }
102  coralSession.reset();
103  }
104  transaction.reset();
105  }
106 
107  bool SessionImpl::isActive() const {
108  return coralSession.get();
109  }
110 
111  void SessionImpl::startTransaction( bool readOnly ){
112  if( !transaction.get() ){
113  if ( theBackendType == ORA_DB ) {
114  cond::DbSession oraSession;
115  oraSession.open( coralSession, connectionString );
116  oraSession.transaction().start( readOnly );
117  iovSchemaHandle.reset( new OraIOVSchema( oraSession ) );
118  gtSchemaHandle.reset( new OraGTSchema( oraSession ) );
119  transaction.reset( new OraTransaction( oraSession ) );
120  } else if ( theBackendType == COND_DB ){
121  coralSession->transaction().start( readOnly );
122  iovSchemaHandle.reset( new IOVSchema( coralSession->nominalSchema() ) );
123  gtSchemaHandle.reset( new GTSchema( coralSession->nominalSchema() ) );
125  } else {
126  throwException( "No valid database found.", "SessionImpl::startTransaction" );
127  }
128  } else {
129  if(!readOnly ) throwException( "An update transaction is already active.",
130  "SessionImpl::startTransaction" );
131  }
132  transaction->clients++;
133  }
134 
136  if( transaction ) {
137  transaction->clients--;
138  if( !transaction->clients ){
139  transaction->commit();
140  transaction.reset();
141  iovSchemaHandle.reset();
142  gtSchemaHandle.reset();
143  }
144  }
145  }
146 
148  if( transaction ) {
149  transaction->rollback();
150  transaction.reset();
151  iovSchemaHandle.reset();
152  gtSchemaHandle.reset();
153  }
154  }
155 
156  bool SessionImpl::isTransactionActive( bool deep ) const {
157  if( !transaction ) return false;
158  if( !deep ) return true;
159  return transaction->isActive();
160  }
161 
163  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::openIovDb" );
164  if( !transaction->iovDbOpen ){
165  transaction->iovDbExists = iovSchemaHandle->exists();
166  transaction->iovDbOpen = true;
167  }
168  if( !transaction->iovDbExists ){
169  if( policy==CREATE ){
170  iovSchemaHandle->create();
171  transaction->iovDbExists = true;
172  } else {
173  if( policy==THROW) throwException( "IOV Database does not exist.","SessionImpl::openIovDb");
174  }
175  }
176  }
177 
179  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::open" );
180  if( !transaction->gtDbOpen ){
181  transaction->gtDbExists = gtSchemaHandle->exists();
182  transaction->gtDbOpen = true;
183  }
184  if( !transaction->gtDbExists ){
185  throwException( "GT Database does not exist.","SessionImpl::openGTDb");
186  }
187  }
188 
190  return *iovSchemaHandle;
191  }
192 
194  return *gtSchemaHandle;
195  }
196 
198  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::open" );
199  return transaction->isOra;
200  }
201 
202  }
203 }
BackendType checkBackendType(boost::shared_ptr< coral::ISessionProxy > &coralSession, const std::string &connectionString)
Definition: SessionImpl.cc:60
bool rollback()
rollback transaction
OraTransaction(const cond::DbSession &session)
Definition: SessionImpl.cc:40
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:162
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:18
boost::shared_ptr< coral::ISessionProxy > m_session
Definition: SessionImpl.cc:35
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:111
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:156
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:74
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11