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  if( iovSchemaHandle->exists() ){
74  ret = COND_DB;
75  }
76  } else {
77  // edm::LogWarning("CondDB") << "You are using conditions from the old database via: "
78  // << connectionString
79  // << std::endl;
80  ret = ORA_DB;
81  }
82  oraSession.transaction().commit();
83  return ret;
84  }
85 
87  coralSession(),
88  theBackendType( UNKNOWN_DB ){
89  }
90 
91  SessionImpl::SessionImpl( boost::shared_ptr<coral::ISessionProxy>& session,
92  const std::string& connectionStr,
93  BackendType backType ):
94  coralSession( session ),
95  connectionString( connectionStr ),
96  theBackendType( backType ){
97  }
98 
100  close();
101  }
102 
104  if( coralSession.get() ){
105  if( coralSession->transaction().isActive() ){
106  coralSession->transaction().rollback();
107  }
108  coralSession.reset();
109  }
110  transaction.reset();
111  }
112 
113  bool SessionImpl::isActive() const {
114  return coralSession.get();
115  }
116 
117  void SessionImpl::startTransaction( bool readOnly ){
118  if( !transaction.get() ){
119  if ( theBackendType == ORA_DB ) {
120  cond::DbSession oraSession;
121  oraSession.open( coralSession, connectionString );
122  oraSession.transaction().start( readOnly );
123  iovSchemaHandle.reset( new OraIOVSchema( oraSession ) );
124  gtSchemaHandle.reset( new OraGTSchema( oraSession ) );
125  transaction.reset( new OraTransaction( oraSession ) );
126  } else if ( theBackendType == COND_DB ){
127  coralSession->transaction().start( readOnly );
128  iovSchemaHandle.reset( new IOVSchema( coralSession->nominalSchema() ) );
129  gtSchemaHandle.reset( new GTSchema( coralSession->nominalSchema() ) );
131  } else {
132  throwException( "No valid database found.", "SessionImpl::startTransaction" );
133  }
134  } else {
135  if(!readOnly ) throwException( "An update transaction is already active.",
136  "SessionImpl::startTransaction" );
137  }
138  transaction->clients++;
139  }
140 
142  if( transaction ) {
143  transaction->clients--;
144  if( !transaction->clients ){
145  transaction->commit();
146  transaction.reset();
147  iovSchemaHandle.reset();
148  gtSchemaHandle.reset();
149  }
150  }
151  }
152 
154  if( transaction ) {
155  transaction->rollback();
156  transaction.reset();
157  iovSchemaHandle.reset();
158  gtSchemaHandle.reset();
159  }
160  }
161 
162  bool SessionImpl::isTransactionActive( bool deep ) const {
163  if( !transaction ) return false;
164  if( !deep ) return true;
165  return transaction->isActive();
166  }
167 
169  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::openIovDb" );
170  if( !transaction->iovDbOpen ){
171  transaction->iovDbExists = iovSchemaHandle->exists();
172  transaction->iovDbOpen = true;
173  }
174  if( !transaction->iovDbExists ){
175  if( policy==CREATE ){
176  iovSchemaHandle->create();
177  transaction->iovDbExists = true;
178  } else {
179  if( policy==THROW) throwException( "IOV Database does not exist.","SessionImpl::openIovDb");
180  }
181  }
182  }
183 
185  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::open" );
186  if( !transaction->gtDbOpen ){
187  transaction->gtDbExists = gtSchemaHandle->exists();
188  transaction->gtDbOpen = true;
189  }
190  if( !transaction->gtDbExists ){
191  throwException( "GT Database does not exist.","SessionImpl::openGTDb");
192  }
193  }
194 
196  return *iovSchemaHandle;
197  }
198 
200  return *gtSchemaHandle;
201  }
202 
204  if(!transaction.get()) throwException( "The transaction is not active.","SessionImpl::open" );
205  return transaction->isOra;
206  }
207 
208  }
209 }
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:168
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:117
bool isTransactionActive(bool deep=true) const
Definition: SessionImpl.cc:162
std::unique_ptr< ITransaction > transaction
Definition: SessionImpl.h:74
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:11