1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/lang/XComponent.hpp>
25 #include "ZPooledConnection.hxx"
26 #include "ZPoolCollection.hxx"
27 #include <connectivity/ConnectionWrapper.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::sdbc
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::container
;
36 using namespace ::osl
;
37 using namespace connectivity
;
41 void SAL_CALL
OPoolTimer::onShot()
43 m_pPool
->invalidatePooledConnections();
46 static const char TIMEOUT_NODENAME
[] = "Timeout";
48 OConnectionPool::OConnectionPool(const Reference
< XDriver
>& _xDriver
,
49 const Reference
< XInterface
>& _xDriverNode
,
50 const Reference
< css::reflection::XProxyFactory
>& _rxProxyFactory
)
52 ,m_xDriverNode(_xDriverNode
)
53 ,m_xProxyFactory(_rxProxyFactory
)
57 OSL_ENSURE(m_xDriverNode
.is(),"NO valid Driver node set!");
58 Reference
< XComponent
> xComponent(m_xDriverNode
, UNO_QUERY
);
60 xComponent
->addEventListener(this);
62 Reference
<XPropertySet
> xProp(m_xDriverNode
,UNO_QUERY
);
64 xProp
->addPropertyChangeListener(TIMEOUT_NODENAME
,this);
66 OPoolCollection::getNodeValue(TIMEOUT_NODENAME
, m_xDriverNode
) >>= m_nALiveCount
;
69 m_xInvalidator
= new OPoolTimer(this,::salhelper::TTimeValue(m_nTimeOut
,0));
70 m_xInvalidator
->start();
73 OConnectionPool::~OConnectionPool()
78 struct TRemoveEventListenerFunctor
80 OConnectionPool
* m_pConnectionPool
;
83 TRemoveEventListenerFunctor(OConnectionPool
* _pConnectionPool
, bool _bDispose
)
84 : m_pConnectionPool(_pConnectionPool
)
85 ,m_bDispose(_bDispose
)
87 OSL_ENSURE(m_pConnectionPool
,"No connection pool!");
90 void dispose(const Reference
<XInterface
>& _xComponent
)
92 Reference
< XComponent
> xComponent(_xComponent
, UNO_QUERY
);
94 if ( xComponent
.is() )
96 xComponent
->removeEventListener(m_pConnectionPool
);
98 xComponent
->dispose();
102 void operator()(const TPooledConnections::value_type
& _aValue
)
107 void operator()(const TActiveConnectionMap::value_type
& _aValue
)
109 dispose(_aValue
.first
);
113 struct TConnectionPoolFunctor
115 OConnectionPool
* m_pConnectionPool
;
117 explicit TConnectionPoolFunctor(OConnectionPool
* _pConnectionPool
)
118 : m_pConnectionPool(_pConnectionPool
)
120 OSL_ENSURE(m_pConnectionPool
,"No connection pool!");
122 void operator()(const TConnectionMap::value_type
& _aValue
)
124 std::for_each(_aValue
.second
.aConnections
.begin(),_aValue
.second
.aConnections
.end(),TRemoveEventListenerFunctor(m_pConnectionPool
,true));
128 void OConnectionPool::clear(bool _bDispose
)
130 MutexGuard
aGuard(m_aMutex
);
132 if(m_xInvalidator
->isTicking())
133 m_xInvalidator
->stop();
135 std::for_each(m_aPool
.begin(),m_aPool
.end(),TConnectionPoolFunctor(this));
138 std::for_each(m_aActiveConnections
.begin(),m_aActiveConnections
.end(),TRemoveEventListenerFunctor(this,_bDispose
));
139 m_aActiveConnections
.clear();
141 Reference
< XComponent
> xComponent(m_xDriverNode
, UNO_QUERY
);
143 xComponent
->removeEventListener(this);
144 Reference
< XPropertySet
> xProp(m_xDriverNode
, UNO_QUERY
);
146 xProp
->removePropertyChangeListener(TIMEOUT_NODENAME
, this);
148 m_xDriverNode
.clear();
152 Reference
< XConnection
> OConnectionPool::getConnectionWithInfo( const OUString
& _rURL
, const Sequence
< PropertyValue
>& _rInfo
)
154 MutexGuard
aGuard(m_aMutex
);
156 Reference
<XConnection
> xConnection
;
158 // create a unique id and look for it in our map
159 Sequence
< PropertyValue
> aInfo(_rInfo
);
160 TConnectionMap::key_type nId
;
161 OConnectionWrapper::createUniqueId(_rURL
,aInfo
,nId
.m_pBuffer
);
162 TConnectionMap::iterator aIter
= m_aPool
.find(nId
);
164 if ( m_aPool
.end() != aIter
)
165 xConnection
= getPooledConnection(aIter
);
167 if ( !xConnection
.is() )
168 xConnection
= createNewConnection(_rURL
,_rInfo
);
173 void SAL_CALL
OConnectionPool::disposing( const css::lang::EventObject
& Source
)
175 Reference
<XConnection
> xConnection(Source
.Source
,UNO_QUERY
);
178 MutexGuard
aGuard(m_aMutex
);
179 TActiveConnectionMap::iterator aIter
= m_aActiveConnections
.find(xConnection
);
180 OSL_ENSURE(aIter
!= m_aActiveConnections
.end(),"OConnectionPool::disposing: Connection wasn't in pool");
181 if(aIter
!= m_aActiveConnections
.end())
182 { // move the pooled connection back to the pool
183 aIter
->second
.aPos
->second
.nALiveCount
= m_nALiveCount
;
184 aIter
->second
.aPos
->second
.aConnections
.push_back(aIter
->second
.xPooledConnection
);
185 m_aActiveConnections
.erase(aIter
);
190 m_xDriverNode
.clear();
194 Reference
< XConnection
> OConnectionPool::createNewConnection(const OUString
& _rURL
,const Sequence
< PropertyValue
>& _rInfo
)
196 // create new pooled connection
197 Reference
< XPooledConnection
> xPooledConnection
= new ::connectivity::OPooledConnection(m_xDriver
->connect(_rURL
,_rInfo
),m_xProxyFactory
);
198 // get the new connection from the pooled connection
199 Reference
<XConnection
> xConnection
= xPooledConnection
->getConnection();
202 // add our own as dispose listener to know when we should put the connection back to the pool
203 Reference
< XComponent
> xComponent(xConnection
, UNO_QUERY
);
205 xComponent
->addEventListener(this);
207 // save some information to find the right pool later on
208 Sequence
< PropertyValue
> aInfo(_rInfo
);
209 TConnectionMap::key_type nId
;
210 OConnectionWrapper::createUniqueId(_rURL
,aInfo
,nId
.m_pBuffer
);
211 TConnectionPool aPack
;
213 // insert the new connection and struct into the active connection map
214 aPack
.nALiveCount
= m_nALiveCount
;
215 TActiveConnectionInfo aActiveInfo
;
216 aActiveInfo
.aPos
= m_aPool
.emplace(nId
,aPack
).first
;
217 aActiveInfo
.xPooledConnection
= xPooledConnection
;
218 m_aActiveConnections
.emplace(xConnection
,aActiveInfo
);
220 if(m_xInvalidator
->isExpired())
221 m_xInvalidator
->start();
227 void OConnectionPool::invalidatePooledConnections()
229 MutexGuard
aGuard(m_aMutex
);
230 TConnectionMap::iterator aIter
= m_aPool
.begin();
231 for (; aIter
!= m_aPool
.end(); )
233 if(!(--(aIter
->second
.nALiveCount
))) // connections are invalid
235 std::for_each(aIter
->second
.aConnections
.begin(),aIter
->second
.aConnections
.end(),TRemoveEventListenerFunctor(this,true));
237 aIter
->second
.aConnections
.clear();
239 // look if the iterator aIter is still present in the active connection map
240 bool isPresent
= std::any_of(m_aActiveConnections
.begin(), m_aActiveConnections
.end(),
241 [&aIter
](const TActiveConnectionMap::value_type
& rEntry
) { return rEntry
.second
.aPos
== aIter
; });
243 {// he isn't so we can delete him
244 aIter
= m_aPool
.erase(aIter
);
253 m_xInvalidator
->start();
256 Reference
< XConnection
> OConnectionPool::getPooledConnection(TConnectionMap::iterator
const & _rIter
)
258 Reference
<XConnection
> xConnection
;
260 if(!_rIter
->second
.aConnections
.empty())
262 Reference
< XPooledConnection
> xPooledConnection
= _rIter
->second
.aConnections
.back();
263 _rIter
->second
.aConnections
.pop_back();
265 OSL_ENSURE(xPooledConnection
.is(),"Can not be null here!");
266 xConnection
= xPooledConnection
->getConnection();
267 Reference
< XComponent
> xComponent(xConnection
, UNO_QUERY
);
269 xComponent
->addEventListener(this);
271 TActiveConnectionInfo aActiveInfo
;
272 aActiveInfo
.aPos
= _rIter
;
273 aActiveInfo
.xPooledConnection
= xPooledConnection
;
274 m_aActiveConnections
[xConnection
] = aActiveInfo
;
279 void SAL_CALL
OConnectionPool::propertyChange( const PropertyChangeEvent
& evt
)
281 if(TIMEOUT_NODENAME
== evt
.PropertyName
)
283 evt
.NewValue
>>= m_nALiveCount
;
288 void OConnectionPool::calculateTimeOuts()
290 sal_Int32 nTimeOutCorrection
= 10;
291 if(m_nALiveCount
< 100)
292 nTimeOutCorrection
= 20;
294 m_nTimeOut
= m_nALiveCount
/ nTimeOutCorrection
;
295 m_nALiveCount
= m_nALiveCount
/ m_nTimeOut
;
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */