Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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>
31 :
32 :
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;
40 :
41 : #include <algorithm>
42 :
43 0 : void SAL_CALL OPoolTimer::onShot()
44 : {
45 0 : m_pPool->invalidatePooledConnections();
46 0 : }
47 :
48 : static const char TIMEOUT_NODENAME[] = "Timeout";
49 :
50 0 : 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 0 : ,m_nALiveCount(10)
58 : {
59 : OSL_ENSURE(m_xDriverNode.is(),"NO valid Driver node set!");
60 0 : Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY);
61 0 : if (xComponent.is())
62 0 : xComponent->addEventListener(this);
63 :
64 0 : Reference<XPropertySet> xProp(m_xDriverNode,UNO_QUERY);
65 0 : if(xProp.is())
66 0 : xProp->addPropertyChangeListener(TIMEOUT_NODENAME,this);
67 :
68 0 : OPoolCollection::getNodeValue(TIMEOUT_NODENAME, m_xDriverNode) >>= m_nALiveCount;
69 0 : calculateTimeOuts();
70 :
71 0 : m_xInvalidator = new OPoolTimer(this,::salhelper::TTimeValue(m_nTimeOut,0));
72 0 : m_xInvalidator->start();
73 0 : }
74 :
75 0 : OConnectionPool::~OConnectionPool()
76 : {
77 0 : clear(false);
78 0 : }
79 :
80 : struct TRemoveEventListenerFunctor : ::std::unary_function<TPooledConnections::value_type,void>
81 : ,::std::unary_function<TActiveConnectionMap::value_type,void>
82 : {
83 : OConnectionPool* m_pConnectionPool;
84 : bool m_bDispose;
85 :
86 0 : TRemoveEventListenerFunctor(OConnectionPool* _pConnectionPool,bool _bDispose = false)
87 : : m_pConnectionPool(_pConnectionPool)
88 0 : ,m_bDispose(_bDispose)
89 : {
90 : OSL_ENSURE(m_pConnectionPool,"No connection pool!");
91 0 : }
92 :
93 0 : void dispose(const Reference<XInterface>& _xComponent)
94 : {
95 0 : Reference< XComponent > xComponent(_xComponent, UNO_QUERY);
96 :
97 0 : if ( xComponent.is() )
98 : {
99 0 : xComponent->removeEventListener(m_pConnectionPool);
100 0 : if ( m_bDispose )
101 0 : xComponent->dispose();
102 0 : }
103 0 : }
104 :
105 0 : void operator()(const TPooledConnections::value_type& _aValue)
106 : {
107 0 : dispose(_aValue);
108 0 : }
109 :
110 0 : void operator()(const TActiveConnectionMap::value_type& _aValue)
111 : {
112 0 : dispose(_aValue.first);
113 0 : }
114 : };
115 :
116 : struct TConnectionPoolFunctor : ::std::unary_function<TConnectionMap::value_type,void>
117 : {
118 : OConnectionPool* m_pConnectionPool;
119 :
120 0 : explicit TConnectionPoolFunctor(OConnectionPool* _pConnectionPool)
121 0 : : m_pConnectionPool(_pConnectionPool)
122 : {
123 : OSL_ENSURE(m_pConnectionPool,"No connection pool!");
124 0 : }
125 0 : void operator()(const TConnectionMap::value_type& _aValue)
126 : {
127 0 : ::std::for_each(_aValue.second.aConnections.begin(),_aValue.second.aConnections.end(),TRemoveEventListenerFunctor(m_pConnectionPool,true));
128 0 : }
129 : };
130 :
131 0 : void OConnectionPool::clear(bool _bDispose)
132 : {
133 0 : MutexGuard aGuard(m_aMutex);
134 :
135 0 : if(m_xInvalidator->isTicking())
136 0 : m_xInvalidator->stop();
137 :
138 0 : ::std::for_each(m_aPool.begin(),m_aPool.end(),TConnectionPoolFunctor(this));
139 0 : m_aPool.clear();
140 :
141 0 : ::std::for_each(m_aActiveConnections.begin(),m_aActiveConnections.end(),TRemoveEventListenerFunctor(this,_bDispose));
142 0 : m_aActiveConnections.clear();
143 :
144 0 : Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY);
145 0 : if (xComponent.is())
146 0 : xComponent->removeEventListener(this);
147 0 : Reference< XPropertySet > xProp(m_xDriverNode, UNO_QUERY);
148 0 : if (xProp.is())
149 0 : xProp->removePropertyChangeListener(TIMEOUT_NODENAME, this);
150 :
151 0 : m_xDriverNode.clear();
152 0 : m_xDriver.clear();
153 0 : }
154 :
155 0 : Reference< XConnection > SAL_CALL OConnectionPool::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo ) throw(SQLException, RuntimeException)
156 : {
157 0 : MutexGuard aGuard(m_aMutex);
158 :
159 0 : Reference<XConnection> xConnection;
160 :
161 : // create a unique id and look for it in our map
162 0 : Sequence< PropertyValue > aInfo(_rInfo);
163 0 : TConnectionMap::key_type nId;
164 0 : OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer);
165 0 : TConnectionMap::iterator aIter = m_aPool.find(nId);
166 :
167 0 : if ( m_aPool.end() != aIter )
168 0 : xConnection = getPooledConnection(aIter);
169 :
170 0 : if ( !xConnection.is() )
171 0 : xConnection = createNewConnection(_rURL,_rInfo);
172 :
173 0 : return xConnection;
174 : }
175 :
176 0 : void SAL_CALL OConnectionPool::disposing( const ::com::sun::star::lang::EventObject& Source ) throw (RuntimeException, std::exception)
177 : {
178 0 : Reference<XConnection> xConnection(Source.Source,UNO_QUERY);
179 0 : if(xConnection.is())
180 : {
181 0 : MutexGuard aGuard(m_aMutex);
182 0 : TActiveConnectionMap::iterator aIter = m_aActiveConnections.find(xConnection);
183 : OSL_ENSURE(aIter != m_aActiveConnections.end(),"OConnectionPool::disposing: Conenction wasn't in pool");
184 0 : if(aIter != m_aActiveConnections.end())
185 : { // move the pooled connection back to the pool
186 0 : aIter->second.aPos->second.nALiveCount = m_nALiveCount;
187 0 : aIter->second.aPos->second.aConnections.push_back(aIter->second.xPooledConnection);
188 0 : m_aActiveConnections.erase(aIter);
189 0 : }
190 : }
191 : else
192 : {
193 0 : m_xDriverNode.clear();
194 0 : }
195 0 : }
196 :
197 0 : Reference< XConnection> OConnectionPool::createNewConnection(const OUString& _rURL,const Sequence< PropertyValue >& _rInfo)
198 : {
199 : // create new pooled conenction
200 0 : Reference< XPooledConnection > xPooledConnection = new ::connectivity::OPooledConnection(m_xDriver->connect(_rURL,_rInfo),m_xProxyFactory);
201 : // get the new connection from the pooled connection
202 0 : Reference<XConnection> xConnection = xPooledConnection->getConnection();
203 0 : if(xConnection.is())
204 : {
205 : // add our own as dispose listener to know when we should put the connection back to the pool
206 0 : Reference< XComponent > xComponent(xConnection, UNO_QUERY);
207 0 : if (xComponent.is())
208 0 : xComponent->addEventListener(this);
209 :
210 : // save some information to find the right pool later on
211 0 : Sequence< PropertyValue > aInfo(_rInfo);
212 0 : TConnectionMap::key_type nId;
213 0 : OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer);
214 0 : TConnectionPool aPack;
215 :
216 : // insert the new connection and struct into the active connection map
217 0 : aPack.nALiveCount = m_nALiveCount;
218 0 : TActiveConnectionInfo aActiveInfo;
219 0 : aActiveInfo.aPos = m_aPool.insert(TConnectionMap::value_type(nId,aPack)).first;
220 0 : aActiveInfo.xPooledConnection = xPooledConnection;
221 0 : m_aActiveConnections.insert(TActiveConnectionMap::value_type(xConnection,aActiveInfo));
222 :
223 0 : if(m_xInvalidator->isExpired())
224 0 : m_xInvalidator->start();
225 : }
226 :
227 0 : return xConnection;
228 : }
229 :
230 0 : void OConnectionPool::invalidatePooledConnections()
231 : {
232 0 : MutexGuard aGuard(m_aMutex);
233 0 : TConnectionMap::iterator aIter = m_aPool.begin();
234 0 : for (; aIter != m_aPool.end(); )
235 : {
236 0 : if(!(--(aIter->second.nALiveCount))) // connections are invalid
237 : {
238 0 : ::std::for_each(aIter->second.aConnections.begin(),aIter->second.aConnections.end(),TRemoveEventListenerFunctor(this,true));
239 :
240 0 : aIter->second.aConnections.clear();
241 :
242 : // look if the iterator aIter is still present in the active connection map
243 0 : TActiveConnectionMap::iterator aActIter = m_aActiveConnections.begin();
244 0 : for (; aActIter != m_aActiveConnections.end(); ++aActIter)
245 : {
246 0 : if(aIter == aActIter->second.aPos)
247 0 : break;
248 : }
249 0 : if(aActIter == m_aActiveConnections.end())
250 : {// he isn't so we can delete him
251 0 : TConnectionMap::iterator aDeleteIter = aIter;
252 0 : ++aIter;
253 0 : m_aPool.erase(aDeleteIter);
254 : }
255 : else
256 0 : ++aIter;
257 : }
258 : else
259 0 : ++aIter;
260 : }
261 0 : if(!m_aPool.empty())
262 0 : m_xInvalidator->start();
263 0 : }
264 :
265 0 : Reference< XConnection> OConnectionPool::getPooledConnection(TConnectionMap::iterator& _rIter)
266 : {
267 0 : Reference<XConnection> xConnection;
268 :
269 0 : if(!_rIter->second.aConnections.empty())
270 : {
271 0 : Reference< XPooledConnection > xPooledConnection = _rIter->second.aConnections.back();
272 0 : _rIter->second.aConnections.pop_back();
273 :
274 : OSL_ENSURE(xPooledConnection.is(),"Can not be null here!");
275 0 : xConnection = xPooledConnection->getConnection();
276 0 : Reference< XComponent > xComponent(xConnection, UNO_QUERY);
277 0 : if (xComponent.is())
278 0 : xComponent->addEventListener(this);
279 :
280 0 : TActiveConnectionInfo aActiveInfo;
281 0 : aActiveInfo.aPos = _rIter;
282 0 : aActiveInfo.xPooledConnection = xPooledConnection;
283 0 : m_aActiveConnections[xConnection] = aActiveInfo;
284 : }
285 0 : return xConnection;
286 : }
287 :
288 0 : void SAL_CALL OConnectionPool::propertyChange( const PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException, std::exception)
289 : {
290 0 : if(TIMEOUT_NODENAME == evt.PropertyName)
291 : {
292 0 : evt.NewValue >>= m_nALiveCount;
293 0 : calculateTimeOuts();
294 : }
295 0 : }
296 :
297 0 : void OConnectionPool::calculateTimeOuts()
298 : {
299 0 : sal_Int32 nTimeOutCorrection = 10;
300 0 : if(m_nALiveCount < 100)
301 0 : nTimeOutCorrection = 20;
302 :
303 0 : m_nTimeOut = m_nALiveCount / nTimeOutCorrection;
304 0 : m_nALiveCount = m_nALiveCount / m_nTimeOut;
305 0 : }
306 :
307 :
308 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|