bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / cpool / ZConnectionPool.cxx
blob2c200325963c5628de629917dd47cce2dc5c1e1e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "ZConnectionPool.hxx"
22 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
23 #include <com/sun/star/container/ElementExistException.hpp>
24 #include <comphelper/extract.hxx>
25 #include <comphelper/types.hxx>
26 #include <com/sun/star/lang/XComponent.hpp>
27 #include "ZPooledConnection.hxx"
28 #include "ZPoolCollection.hxx"
29 #include <connectivity/ConnectionWrapper.hxx>
30 #include <com/sun/star/beans/XPropertySet.hpp>
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::sdbc;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::container;
38 using namespace ::osl;
39 using namespace connectivity;
41 #include <algorithm>
43 void SAL_CALL OPoolTimer::onShot()
45 m_pPool->invalidatePooledConnections();
48 static const char TIMEOUT_NODENAME[] = "Timeout";
50 OConnectionPool::OConnectionPool(const Reference< XDriver >& _xDriver,
51 const Reference< XInterface >& _xDriverNode,
52 const Reference< ::com::sun::star::reflection::XProxyFactory >& _rxProxyFactory)
53 :m_xDriver(_xDriver)
54 ,m_xDriverNode(_xDriverNode)
55 ,m_xProxyFactory(_rxProxyFactory)
56 ,m_nTimeOut(10)
57 ,m_nALiveCount(10)
59 OSL_ENSURE(m_xDriverNode.is(),"NO valid Driver node set!");
60 Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY);
61 if (xComponent.is())
62 xComponent->addEventListener(this);
64 Reference<XPropertySet> xProp(m_xDriverNode,UNO_QUERY);
65 if(xProp.is())
66 xProp->addPropertyChangeListener(TIMEOUT_NODENAME,this);
68 OPoolCollection::getNodeValue(TIMEOUT_NODENAME, m_xDriverNode) >>= m_nALiveCount;
69 calculateTimeOuts();
71 m_xInvalidator = new OPoolTimer(this,::salhelper::TTimeValue(m_nTimeOut,0));
72 m_xInvalidator->start();
75 OConnectionPool::~OConnectionPool()
77 clear(false);
80 struct TRemoveEventListenerFunctor : ::std::unary_function<TPooledConnections::value_type,void>
81 ,::std::unary_function<TActiveConnectionMap::value_type,void>
83 OConnectionPool* m_pConnectionPool;
84 bool m_bDispose;
86 TRemoveEventListenerFunctor(OConnectionPool* _pConnectionPool,bool _bDispose = false)
87 : m_pConnectionPool(_pConnectionPool)
88 ,m_bDispose(_bDispose)
90 OSL_ENSURE(m_pConnectionPool,"No connection pool!");
93 void dispose(const Reference<XInterface>& _xComponent)
95 Reference< XComponent > xComponent(_xComponent, UNO_QUERY);
97 if ( xComponent.is() )
99 xComponent->removeEventListener(m_pConnectionPool);
100 if ( m_bDispose )
101 xComponent->dispose();
105 void operator()(const TPooledConnections::value_type& _aValue)
107 dispose(_aValue);
110 void operator()(const TActiveConnectionMap::value_type& _aValue)
112 dispose(_aValue.first);
116 struct TConnectionPoolFunctor : ::std::unary_function<TConnectionMap::value_type,void>
118 OConnectionPool* m_pConnectionPool;
120 TConnectionPoolFunctor(OConnectionPool* _pConnectionPool)
121 : m_pConnectionPool(_pConnectionPool)
123 OSL_ENSURE(m_pConnectionPool,"No connection pool!");
125 void operator()(const TConnectionMap::value_type& _aValue)
127 ::std::for_each(_aValue.second.aConnections.begin(),_aValue.second.aConnections.end(),TRemoveEventListenerFunctor(m_pConnectionPool,true));
131 void OConnectionPool::clear(bool _bDispose)
133 MutexGuard aGuard(m_aMutex);
135 if(m_xInvalidator->isTicking())
136 m_xInvalidator->stop();
138 ::std::for_each(m_aPool.begin(),m_aPool.end(),TConnectionPoolFunctor(this));
139 m_aPool.clear();
141 ::std::for_each(m_aActiveConnections.begin(),m_aActiveConnections.end(),TRemoveEventListenerFunctor(this,_bDispose));
142 m_aActiveConnections.clear();
144 Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY);
145 if (xComponent.is())
146 xComponent->removeEventListener(this);
147 Reference< XPropertySet > xProp(m_xDriverNode, UNO_QUERY);
148 if (xProp.is())
149 xProp->removePropertyChangeListener(TIMEOUT_NODENAME, this);
151 m_xDriverNode.clear();
152 m_xDriver.clear();
155 Reference< XConnection > SAL_CALL OConnectionPool::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo ) throw(SQLException, RuntimeException)
157 MutexGuard aGuard(m_aMutex);
159 Reference<XConnection> xConnection;
161 // create a unique id and look for it in our map
162 Sequence< PropertyValue > aInfo(_rInfo);
163 TConnectionMap::key_type nId;
164 OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer);
165 TConnectionMap::iterator aIter = m_aPool.find(nId);
167 if ( m_aPool.end() != aIter )
168 xConnection = getPooledConnection(aIter);
170 if ( !xConnection.is() )
171 xConnection = createNewConnection(_rURL,_rInfo);
173 return xConnection;
176 void SAL_CALL OConnectionPool::disposing( const ::com::sun::star::lang::EventObject& Source ) throw (RuntimeException, std::exception)
178 Reference<XConnection> xConnection(Source.Source,UNO_QUERY);
179 if(xConnection.is())
181 MutexGuard aGuard(m_aMutex);
182 TActiveConnectionMap::iterator aIter = m_aActiveConnections.find(xConnection);
183 OSL_ENSURE(aIter != m_aActiveConnections.end(),"OConnectionPool::disposing: Conenction wasn't in pool");
184 if(aIter != m_aActiveConnections.end())
185 { // move the pooled connection back to the pool
186 aIter->second.aPos->second.nALiveCount = m_nALiveCount;
187 aIter->second.aPos->second.aConnections.push_back(aIter->second.xPooledConnection);
188 m_aActiveConnections.erase(aIter);
191 else
193 m_xDriverNode.clear();
197 Reference< XConnection> OConnectionPool::createNewConnection(const OUString& _rURL,const Sequence< PropertyValue >& _rInfo)
199 // create new pooled conenction
200 Reference< XPooledConnection > xPooledConnection = new ::connectivity::OPooledConnection(m_xDriver->connect(_rURL,_rInfo),m_xProxyFactory);
201 // get the new connection from the pooled connection
202 Reference<XConnection> xConnection = xPooledConnection->getConnection();
203 if(xConnection.is())
205 // add our own as dispose listener to know when we should put the connection back to the pool
206 Reference< XComponent > xComponent(xConnection, UNO_QUERY);
207 if (xComponent.is())
208 xComponent->addEventListener(this);
210 // save some information to find the right pool later on
211 Sequence< PropertyValue > aInfo(_rInfo);
212 TConnectionMap::key_type nId;
213 OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer);
214 TConnectionPool aPack;
216 // insert the new connection and struct into the active connection map
217 aPack.nALiveCount = m_nALiveCount;
218 TActiveConnectionInfo aActiveInfo;
219 aActiveInfo.aPos = m_aPool.insert(TConnectionMap::value_type(nId,aPack)).first;
220 aActiveInfo.xPooledConnection = xPooledConnection;
221 m_aActiveConnections.insert(TActiveConnectionMap::value_type(xConnection,aActiveInfo));
223 if(m_xInvalidator->isExpired())
224 m_xInvalidator->start();
227 return xConnection;
230 void OConnectionPool::invalidatePooledConnections()
232 MutexGuard aGuard(m_aMutex);
233 TConnectionMap::iterator aIter = m_aPool.begin();
234 for (; aIter != m_aPool.end(); )
236 if(!(--(aIter->second.nALiveCount))) // connections are invalid
238 ::std::for_each(aIter->second.aConnections.begin(),aIter->second.aConnections.end(),TRemoveEventListenerFunctor(this,true));
240 aIter->second.aConnections.clear();
242 // look if the iterator aIter is still present in the active connection map
243 TActiveConnectionMap::iterator aActIter = m_aActiveConnections.begin();
244 for (; aActIter != m_aActiveConnections.end(); ++aActIter)
246 if(aIter == aActIter->second.aPos)
247 break;
249 if(aActIter == m_aActiveConnections.end())
250 {// he isn't so we can delete him
251 TConnectionMap::iterator aDeleteIter = aIter;
252 ++aIter;
253 m_aPool.erase(aDeleteIter);
255 else
256 ++aIter;
258 else
259 ++aIter;
261 if(!m_aPool.empty())
262 m_xInvalidator->start();
265 Reference< XConnection> OConnectionPool::getPooledConnection(TConnectionMap::iterator& _rIter)
267 Reference<XConnection> xConnection;
269 if(!_rIter->second.aConnections.empty())
271 Reference< XPooledConnection > xPooledConnection = _rIter->second.aConnections.back();
272 _rIter->second.aConnections.pop_back();
274 OSL_ENSURE(xPooledConnection.is(),"Can not be null here!");
275 xConnection = xPooledConnection->getConnection();
276 Reference< XComponent > xComponent(xConnection, UNO_QUERY);
277 if (xComponent.is())
278 xComponent->addEventListener(this);
280 TActiveConnectionInfo aActiveInfo;
281 aActiveInfo.aPos = _rIter;
282 aActiveInfo.xPooledConnection = xPooledConnection;
283 m_aActiveConnections[xConnection] = aActiveInfo;
285 return xConnection;
288 void SAL_CALL OConnectionPool::propertyChange( const PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException, std::exception)
290 if(TIMEOUT_NODENAME == evt.PropertyName)
292 evt.NewValue >>= m_nALiveCount;
293 calculateTimeOuts();
297 void OConnectionPool::calculateTimeOuts()
299 sal_Int32 nTimeOutCorrection = 10;
300 if(m_nALiveCount < 100)
301 nTimeOutCorrection = 20;
303 m_nTimeOut = m_nALiveCount / nTimeOutCorrection;
304 m_nALiveCount = m_nALiveCount / m_nTimeOut;
308 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */