CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/CondCore/ORA/src/ConnectionPool.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/Exception.h"
00002 #include "CondCore/ORA/interface/ConnectionPool.h"
00003 // externals
00004 #include "RelationalAccess/ISessionProxy.h"
00005 
00006 ora::SharedSession::SharedSession():
00007   m_proxy(){
00008 }
00009 
00010 ora::SharedSession::SharedSession(boost::shared_ptr<coral::ISessionProxy>& coralSession):
00011   m_proxy(coralSession){
00012 }
00013 
00014 ora::SharedSession::SharedSession( const ora::SharedSession& rhs ):
00015   m_proxy( rhs.m_proxy ){
00016 }
00017 
00018 ora::SharedSession::~SharedSession(){
00019 }
00020 
00021 ora::SharedSession& ora::SharedSession::operator=( const ora::SharedSession& rhs ){
00022   m_proxy = rhs.m_proxy;
00023   return *this;
00024 }
00025 
00026 coral::ISessionProxy&
00027 ora::SharedSession::get(){
00028   if(!m_proxy.get()){
00029     throwException("Coral Database Session is not available.",
00030                    "SharedSession::proxy");
00031   }
00032   return *m_proxy;
00033 }
00034 
00035 bool ora::SharedSession::isValid(){
00036   return m_proxy.get();
00037 }
00038 
00039 void ora::SharedSession::close(){
00040   m_proxy.reset();
00041 }
00042 
00043 ora::ConnectionPool::ConnectionPool():m_connectionService(),m_sessions(){
00044 }
00045 
00046 ora::ConnectionPool::~ConnectionPool(){
00047 }
00048 
00049 coral::IConnectionService& ora::ConnectionPool::connectionService(){
00050   return m_connectionService;
00051 }
00052 coral::IConnectionServiceConfiguration& ora::ConnectionPool::configuration(){
00053   return m_connectionService.configuration();
00054 }
00055 
00056 ora::SharedSession ora::ConnectionPool::connect( const std::string& connectionString,
00057                                                  coral::AccessMode accessMode ){
00058   bool valid = false;
00059   boost::shared_ptr<coral::ISessionProxy> session;
00060   std::map<std::string,boost::weak_ptr<coral::ISessionProxy> >::iterator iS = m_sessions.find( lookupString( connectionString, accessMode ) );
00061   if( iS != m_sessions.end() ){
00062     if( !iS->second.expired() ){
00063       session = iS->second.lock();
00064       valid = true;
00065     } 
00066   } else {
00067     iS = m_sessions.insert(std::make_pair( lookupString( connectionString, accessMode ),boost::weak_ptr<coral::ISessionProxy>())).first;
00068   }
00069   if(!valid){
00070     session.reset(m_connectionService.connect( connectionString, accessMode ));
00071     boost::weak_ptr<coral::ISessionProxy> tmp(session);
00072     iS->second.swap( tmp );
00073   }
00074   return SharedSession( session );
00075 }
00076 
00077 ora::SharedSession ora::ConnectionPool::connect( const std::string& connectionString,
00078                                                  const std::string& asRole,
00079                                                  coral::AccessMode accessMode ){
00080   bool valid = false;
00081   boost::shared_ptr<coral::ISessionProxy> session;
00082   std::map<std::string,boost::weak_ptr<coral::ISessionProxy> >::iterator iS = m_sessions.find( lookupString( connectionString, asRole, accessMode ) );
00083   if( iS != m_sessions.end() ){
00084     if( !iS->second.expired() ){
00085       session = iS->second.lock();
00086       valid = true;
00087     } 
00088   } else {
00089     iS = m_sessions.insert(std::make_pair( lookupString( connectionString, asRole, accessMode ),boost::weak_ptr<coral::ISessionProxy>())).first;
00090   }
00091   if(!valid){
00092     session.reset(m_connectionService.connect( connectionString, asRole, accessMode ));
00093     boost::weak_ptr<coral::ISessionProxy> tmp(session);
00094     iS->second.swap( tmp );
00095   }
00096   return SharedSession( session );
00097 }
00098 
00099 
00100 std::string ora::ConnectionPool::lookupString( const std::string& connectionString,
00101                                                coral::AccessMode accessMode ){
00102   std::stringstream lookupString;
00103   lookupString << "["<<connectionString << "]_";
00104   if(accessMode == coral::ReadOnly){
00105     lookupString << "R";
00106   } else {
00107     lookupString << "W";
00108   }
00109   return lookupString.str();
00110 }
00111 
00112 std::string ora::ConnectionPool::lookupString( const std::string& connectionString,
00113                                                const std::string& role,
00114                                                coral::AccessMode accessMode ){
00115   std::string rolePrefix(role);
00116   return rolePrefix+lookupString( connectionString, accessMode );
00117 }