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 std::string ora::ConnectionPool::lookupString( const std::string& connectionString, 00078 coral::AccessMode accessMode ){ 00079 std::stringstream lookupString; 00080 lookupString << connectionString << "_"; 00081 if(accessMode == coral::ReadOnly){ 00082 lookupString << "R"; 00083 } else { 00084 lookupString << "W"; 00085 } 00086 return lookupString.str(); 00087 } 00088