1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <stdio.h> 32*cdf0e10cSrcweir #include "ZConnectionPool.hxx" 33*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/container/ElementExistException.hpp> 35*cdf0e10cSrcweir #include <comphelper/extract.hxx> 36*cdf0e10cSrcweir #include <comphelper/types.hxx> 37*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp> 38*cdf0e10cSrcweir #include "ZPooledConnection.hxx" 39*cdf0e10cSrcweir #include "ZPoolCollection.hxx" 40*cdf0e10cSrcweir #ifndef _CONNECTIVITY_CONNECTIONWRAPPER_HXX_ 41*cdf0e10cSrcweir #include "connectivity/ConnectionWrapper.hxx" 42*cdf0e10cSrcweir #endif 43*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 44*cdf0e10cSrcweir #ifndef _CONNECTIVITY_CONNECTIONWRAPPER_HXX_ 45*cdf0e10cSrcweir #include "connectivity/ConnectionWrapper.hxx" 46*cdf0e10cSrcweir #endif 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 50*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 51*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 52*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 53*cdf0e10cSrcweir using namespace ::com::sun::star::container; 54*cdf0e10cSrcweir using namespace ::osl; 55*cdf0e10cSrcweir using namespace connectivity; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir #include <algorithm> 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir //========================================================================== 60*cdf0e10cSrcweir //= OPoolTimer 61*cdf0e10cSrcweir //========================================================================== 62*cdf0e10cSrcweir void SAL_CALL OPoolTimer::onShot() 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir m_pPool->invalidatePooledConnections(); 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir namespace 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir //-------------------------------------------------------------------- 69*cdf0e10cSrcweir static const ::rtl::OUString& getTimeoutNodeName() 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir static ::rtl::OUString s_sNodeName = ::rtl::OUString::createFromAscii("Timeout"); 72*cdf0e10cSrcweir return s_sNodeName; 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir //========================================================================== 77*cdf0e10cSrcweir //= OConnectionPool 78*cdf0e10cSrcweir //========================================================================== 79*cdf0e10cSrcweir //-------------------------------------------------------------------------- 80*cdf0e10cSrcweir OConnectionPool::OConnectionPool(const Reference< XDriver >& _xDriver, 81*cdf0e10cSrcweir const Reference< XInterface >& _xDriverNode, 82*cdf0e10cSrcweir const Reference< ::com::sun::star::reflection::XProxyFactory >& _rxProxyFactory) 83*cdf0e10cSrcweir :m_xDriver(_xDriver) 84*cdf0e10cSrcweir ,m_xDriverNode(_xDriverNode) 85*cdf0e10cSrcweir ,m_xProxyFactory(_rxProxyFactory) 86*cdf0e10cSrcweir ,m_nTimeOut(10) 87*cdf0e10cSrcweir ,m_nALiveCount(10) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir OSL_ENSURE(m_xDriverNode.is(),"NO valid Driver node set!"); 90*cdf0e10cSrcweir Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY); 91*cdf0e10cSrcweir if (xComponent.is()) 92*cdf0e10cSrcweir xComponent->addEventListener(this); 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir Reference<XPropertySet> xProp(m_xDriverNode,UNO_QUERY); 95*cdf0e10cSrcweir if(xProp.is()) 96*cdf0e10cSrcweir xProp->addPropertyChangeListener(getTimeoutNodeName(),this); 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir OPoolCollection::getNodeValue(getTimeoutNodeName(),m_xDriverNode) >>= m_nALiveCount; 99*cdf0e10cSrcweir calculateTimeOuts(); 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir m_xInvalidator = new OPoolTimer(this,::vos::TTimeValue(m_nTimeOut,0)); 102*cdf0e10cSrcweir m_xInvalidator->start(); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 105*cdf0e10cSrcweir OConnectionPool::~OConnectionPool() 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir clear(sal_False); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 110*cdf0e10cSrcweir struct TRemoveEventListenerFunctor : ::std::unary_function<TPooledConnections::value_type,void> 111*cdf0e10cSrcweir ,::std::unary_function<TActiveConnectionMap::value_type,void> 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir OConnectionPool* m_pConnectionPool; 114*cdf0e10cSrcweir sal_Bool m_bDispose; 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir TRemoveEventListenerFunctor(OConnectionPool* _pConnectionPool,sal_Bool _bDispose = sal_False) 117*cdf0e10cSrcweir : m_pConnectionPool(_pConnectionPool) 118*cdf0e10cSrcweir ,m_bDispose(_bDispose) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir OSL_ENSURE(m_pConnectionPool,"No connection pool!"); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 123*cdf0e10cSrcweir void dispose(const Reference<XInterface>& _xComponent) 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir Reference< XComponent > xComponent(_xComponent, UNO_QUERY); 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir if ( xComponent.is() ) 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir xComponent->removeEventListener(m_pConnectionPool); 130*cdf0e10cSrcweir if ( m_bDispose ) 131*cdf0e10cSrcweir xComponent->dispose(); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 135*cdf0e10cSrcweir void operator()(const TPooledConnections::value_type& _aValue) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir dispose(_aValue); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 140*cdf0e10cSrcweir void operator()(const TActiveConnectionMap::value_type& _aValue) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir dispose(_aValue.first); 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir }; 145*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 146*cdf0e10cSrcweir struct TConnectionPoolFunctor : ::std::unary_function<TConnectionMap::value_type,void> 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir OConnectionPool* m_pConnectionPool; 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir TConnectionPoolFunctor(OConnectionPool* _pConnectionPool) 151*cdf0e10cSrcweir : m_pConnectionPool(_pConnectionPool) 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir OSL_ENSURE(m_pConnectionPool,"No connection pool!"); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir void operator()(const TConnectionMap::value_type& _aValue) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir ::std::for_each(_aValue.second.aConnections.begin(),_aValue.second.aConnections.end(),TRemoveEventListenerFunctor(m_pConnectionPool,sal_True)); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir }; 160*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 161*cdf0e10cSrcweir void OConnectionPool::clear(sal_Bool _bDispose) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir MutexGuard aGuard(m_aMutex); 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir if(m_xInvalidator->isTicking()) 166*cdf0e10cSrcweir m_xInvalidator->stop(); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir ::std::for_each(m_aPool.begin(),m_aPool.end(),TConnectionPoolFunctor(this)); 169*cdf0e10cSrcweir m_aPool.clear(); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir ::std::for_each(m_aActiveConnections.begin(),m_aActiveConnections.end(),TRemoveEventListenerFunctor(this,_bDispose)); 172*cdf0e10cSrcweir m_aActiveConnections.clear(); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY); 175*cdf0e10cSrcweir if (xComponent.is()) 176*cdf0e10cSrcweir xComponent->removeEventListener(this); 177*cdf0e10cSrcweir Reference< XPropertySet > xProp(m_xDriverNode, UNO_QUERY); 178*cdf0e10cSrcweir if (xProp.is()) 179*cdf0e10cSrcweir xProp->removePropertyChangeListener(getTimeoutNodeName(),this); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir m_xDriverNode.clear(); 182*cdf0e10cSrcweir m_xDriver.clear(); 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir //-------------------------------------------------------------------------- 185*cdf0e10cSrcweir Reference< XConnection > SAL_CALL OConnectionPool::getConnectionWithInfo( const ::rtl::OUString& _rURL, const Sequence< PropertyValue >& _rInfo ) throw(SQLException, RuntimeException) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir MutexGuard aGuard(m_aMutex); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir Reference<XConnection> xConnection; 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir // create a unique id and look for it in our map 192*cdf0e10cSrcweir Sequence< PropertyValue > aInfo(_rInfo); 193*cdf0e10cSrcweir TConnectionMap::key_type nId; 194*cdf0e10cSrcweir OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer); 195*cdf0e10cSrcweir TConnectionMap::iterator aIter = m_aPool.find(nId); 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir if ( m_aPool.end() != aIter ) 198*cdf0e10cSrcweir xConnection = getPooledConnection(aIter); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir if ( !xConnection.is() ) 201*cdf0e10cSrcweir xConnection = createNewConnection(_rURL,_rInfo); 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir return xConnection; 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir //-------------------------------------------------------------------------- 206*cdf0e10cSrcweir void SAL_CALL OConnectionPool::disposing( const ::com::sun::star::lang::EventObject& Source ) throw (RuntimeException) 207*cdf0e10cSrcweir { 208*cdf0e10cSrcweir Reference<XConnection> xConnection(Source.Source,UNO_QUERY); 209*cdf0e10cSrcweir if(xConnection.is()) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir MutexGuard aGuard(m_aMutex); 212*cdf0e10cSrcweir TActiveConnectionMap::iterator aIter = m_aActiveConnections.find(xConnection); 213*cdf0e10cSrcweir OSL_ENSURE(aIter != m_aActiveConnections.end(),"OConnectionPool::disposing: Conenction wasn't in pool"); 214*cdf0e10cSrcweir if(aIter != m_aActiveConnections.end()) 215*cdf0e10cSrcweir { // move the pooled connection back to the pool 216*cdf0e10cSrcweir aIter->second.aPos->second.nALiveCount = m_nALiveCount; 217*cdf0e10cSrcweir aIter->second.aPos->second.aConnections.push_back(aIter->second.xPooledConnection); 218*cdf0e10cSrcweir m_aActiveConnections.erase(aIter); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir else 222*cdf0e10cSrcweir { 223*cdf0e10cSrcweir m_xDriverNode.clear(); 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 227*cdf0e10cSrcweir Reference< XConnection> OConnectionPool::createNewConnection(const ::rtl::OUString& _rURL,const Sequence< PropertyValue >& _rInfo) 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir // create new pooled conenction 230*cdf0e10cSrcweir Reference< XPooledConnection > xPooledConnection = new ::connectivity::OPooledConnection(m_xDriver->connect(_rURL,_rInfo),m_xProxyFactory); 231*cdf0e10cSrcweir // get the new connection from the pooled connection 232*cdf0e10cSrcweir Reference<XConnection> xConnection = xPooledConnection->getConnection(); 233*cdf0e10cSrcweir if(xConnection.is()) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir // add our own as dispose listener to know when we should put the connection back to the pool 236*cdf0e10cSrcweir Reference< XComponent > xComponent(xConnection, UNO_QUERY); 237*cdf0e10cSrcweir if (xComponent.is()) 238*cdf0e10cSrcweir xComponent->addEventListener(this); 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir // save some information to find the right pool later on 241*cdf0e10cSrcweir Sequence< PropertyValue > aInfo(_rInfo); 242*cdf0e10cSrcweir TConnectionMap::key_type nId; 243*cdf0e10cSrcweir OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer); 244*cdf0e10cSrcweir TConnectionPool aPack; 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir // insert the new connection and struct into the active connection map 247*cdf0e10cSrcweir aPack.nALiveCount = m_nALiveCount; 248*cdf0e10cSrcweir TActiveConnectionInfo aActiveInfo; 249*cdf0e10cSrcweir aActiveInfo.aPos = m_aPool.insert(TConnectionMap::value_type(nId,aPack)).first; 250*cdf0e10cSrcweir aActiveInfo.xPooledConnection = xPooledConnection; 251*cdf0e10cSrcweir m_aActiveConnections.insert(TActiveConnectionMap::value_type(xConnection,aActiveInfo)); 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir if(m_xInvalidator->isExpired()) 254*cdf0e10cSrcweir m_xInvalidator->start(); 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir return xConnection; 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 260*cdf0e10cSrcweir void OConnectionPool::invalidatePooledConnections() 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir MutexGuard aGuard(m_aMutex); 263*cdf0e10cSrcweir TConnectionMap::iterator aIter = m_aPool.begin(); 264*cdf0e10cSrcweir for (; aIter != m_aPool.end(); ) 265*cdf0e10cSrcweir { 266*cdf0e10cSrcweir if(!(--(aIter->second.nALiveCount))) // connections are invalid 267*cdf0e10cSrcweir { 268*cdf0e10cSrcweir ::std::for_each(aIter->second.aConnections.begin(),aIter->second.aConnections.end(),TRemoveEventListenerFunctor(this,sal_True)); 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir aIter->second.aConnections.clear(); 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir // look if the iterator aIter is still present in the active connection map 273*cdf0e10cSrcweir TActiveConnectionMap::iterator aActIter = m_aActiveConnections.begin(); 274*cdf0e10cSrcweir for (; aActIter != m_aActiveConnections.end(); ++aActIter) 275*cdf0e10cSrcweir { 276*cdf0e10cSrcweir if(aIter == aActIter->second.aPos) 277*cdf0e10cSrcweir break; 278*cdf0e10cSrcweir } 279*cdf0e10cSrcweir if(aActIter == m_aActiveConnections.end()) 280*cdf0e10cSrcweir {// he isn't so we can delete him 281*cdf0e10cSrcweir TConnectionMap::iterator aDeleteIter = aIter; 282*cdf0e10cSrcweir ++aIter; 283*cdf0e10cSrcweir m_aPool.erase(aDeleteIter); 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir else 286*cdf0e10cSrcweir ++aIter; 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir else 289*cdf0e10cSrcweir ++aIter; 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir if(!m_aPool.empty()) 292*cdf0e10cSrcweir m_xInvalidator->start(); 293*cdf0e10cSrcweir } 294*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 295*cdf0e10cSrcweir Reference< XConnection> OConnectionPool::getPooledConnection(TConnectionMap::iterator& _rIter) 296*cdf0e10cSrcweir { 297*cdf0e10cSrcweir Reference<XConnection> xConnection; 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir if(!_rIter->second.aConnections.empty()) 300*cdf0e10cSrcweir { 301*cdf0e10cSrcweir Reference< XPooledConnection > xPooledConnection = _rIter->second.aConnections.back(); 302*cdf0e10cSrcweir _rIter->second.aConnections.pop_back(); 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir OSL_ENSURE(xPooledConnection.is(),"Can not be null here!"); 305*cdf0e10cSrcweir xConnection = xPooledConnection->getConnection(); 306*cdf0e10cSrcweir Reference< XComponent > xComponent(xConnection, UNO_QUERY); 307*cdf0e10cSrcweir if (xComponent.is()) 308*cdf0e10cSrcweir xComponent->addEventListener(this); 309*cdf0e10cSrcweir 310*cdf0e10cSrcweir TActiveConnectionInfo aActiveInfo; 311*cdf0e10cSrcweir aActiveInfo.aPos = _rIter; 312*cdf0e10cSrcweir aActiveInfo.xPooledConnection = xPooledConnection; 313*cdf0e10cSrcweir m_aActiveConnections[xConnection] = aActiveInfo; 314*cdf0e10cSrcweir } 315*cdf0e10cSrcweir return xConnection; 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 318*cdf0e10cSrcweir void SAL_CALL OConnectionPool::propertyChange( const PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException) 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir if(getTimeoutNodeName() == evt.PropertyName) 321*cdf0e10cSrcweir { 322*cdf0e10cSrcweir evt.NewValue >>= m_nALiveCount; 323*cdf0e10cSrcweir calculateTimeOuts(); 324*cdf0e10cSrcweir } 325*cdf0e10cSrcweir } 326*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 327*cdf0e10cSrcweir void OConnectionPool::calculateTimeOuts() 328*cdf0e10cSrcweir { 329*cdf0e10cSrcweir sal_Int32 nTimeOutCorrection = 10; 330*cdf0e10cSrcweir if(m_nALiveCount < 100) 331*cdf0e10cSrcweir nTimeOutCorrection = 20; 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir m_nTimeOut = m_nALiveCount / nTimeOutCorrection; 334*cdf0e10cSrcweir m_nALiveCount = m_nALiveCount / m_nTimeOut; 335*cdf0e10cSrcweir } 336*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 337