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/XComponent.hpp>
23 #include "ZPooledConnection.hxx"
24 #include "ZPoolCollection.hxx"
25 #include <connectivity/ConnectionWrapper.hxx>
26 #include <com/sun/star/beans/XPropertySet.hpp>
30 using namespace ::com::sun::star::uno
;
31 using namespace ::com::sun::star::lang
;
32 using namespace ::com::sun::star::sdbc
;
33 using namespace ::com::sun::star::beans
;
34 using namespace ::com::sun::star::container
;
35 using namespace connectivity
;
38 void SAL_CALL
OPoolTimer::onShot()
40 m_pPool
->invalidatePooledConnections();
43 constexpr OUString TIMEOUT_NODENAME
= u
"Timeout"_ustr
;
45 OConnectionPool::OConnectionPool(const Reference
< XDriver
>& _xDriver
,
46 const Reference
< XInterface
>& _xDriverNode
,
47 const Reference
< css::reflection::XProxyFactory
>& _rxProxyFactory
)
49 ,m_xDriverNode(_xDriverNode
)
50 ,m_xProxyFactory(_rxProxyFactory
)
54 OSL_ENSURE(m_xDriverNode
.is(),"NO valid Driver node set!");
55 Reference
< XComponent
> xComponent(m_xDriverNode
, UNO_QUERY
);
57 xComponent
->addEventListener(this);
59 Reference
<XPropertySet
> xProp(m_xDriverNode
,UNO_QUERY
);
61 xProp
->addPropertyChangeListener(TIMEOUT_NODENAME
,this);
63 OPoolCollection::getNodeValue(TIMEOUT_NODENAME
, m_xDriverNode
) >>= m_nALiveCount
;
66 m_xInvalidator
= new OPoolTimer(this,::salhelper::TTimeValue(m_nTimeOut
,0));
67 m_xInvalidator
->start();
70 OConnectionPool::~OConnectionPool()
77 struct TRemoveEventListenerFunctor
79 OConnectionPool
* m_pConnectionPool
;
82 TRemoveEventListenerFunctor(OConnectionPool
* _pConnectionPool
, bool _bDispose
)
83 : m_pConnectionPool(_pConnectionPool
)
84 ,m_bDispose(_bDispose
)
86 OSL_ENSURE(m_pConnectionPool
,"No connection pool!");
89 void dispose(const Reference
<XInterface
>& _xComponent
)
91 Reference
< XComponent
> xComponent(_xComponent
, UNO_QUERY
);
93 if ( xComponent
.is() )
95 xComponent
->removeEventListener(m_pConnectionPool
);
97 xComponent
->dispose();
101 void operator()(const TPooledConnections::value_type
& _aValue
)
106 void operator()(const TActiveConnectionMap::value_type
& _aValue
)
108 dispose(_aValue
.first
);
112 struct TConnectionPoolFunctor
114 OConnectionPool
* m_pConnectionPool
;
116 explicit TConnectionPoolFunctor(OConnectionPool
* _pConnectionPool
)
117 : m_pConnectionPool(_pConnectionPool
)
119 OSL_ENSURE(m_pConnectionPool
,"No connection pool!");
121 void operator()(const TConnectionMap::value_type
& _aValue
)
123 std::for_each(_aValue
.second
.aConnections
.begin(),_aValue
.second
.aConnections
.end(),TRemoveEventListenerFunctor(m_pConnectionPool
,true));
129 void OConnectionPool::clear(bool _bDispose
)
131 std::unique_lock
aGuard(m_aMutex
);
133 if(m_xInvalidator
->isTicking())
134 m_xInvalidator
->stop();
136 std::for_each(m_aPool
.begin(),m_aPool
.end(),TConnectionPoolFunctor(this));
139 std::for_each(m_aActiveConnections
.begin(),m_aActiveConnections
.end(),TRemoveEventListenerFunctor(this,_bDispose
));
140 m_aActiveConnections
.clear();
142 Reference
< XComponent
> xComponent(m_xDriverNode
, UNO_QUERY
);
144 xComponent
->removeEventListener(this);
145 Reference
< XPropertySet
> xProp(m_xDriverNode
, UNO_QUERY
);
147 xProp
->removePropertyChangeListener(TIMEOUT_NODENAME
, this);
149 m_xDriverNode
.clear();
153 Reference
< XConnection
> OConnectionPool::getConnectionWithInfo( const OUString
& _rURL
, const Sequence
< PropertyValue
>& _rInfo
)
155 std::unique_lock
aGuard(m_aMutex
);
157 Reference
<XConnection
> xConnection
;
159 // create a unique id and look for it in our map
160 Sequence
< PropertyValue
> aInfo(_rInfo
);
161 TConnectionMap::key_type nId
;
162 OConnectionWrapper::createUniqueId(_rURL
,aInfo
,nId
.m_pBuffer
);
163 TConnectionMap::iterator aIter
= m_aPool
.find(nId
);
165 if ( m_aPool
.end() != aIter
)
166 xConnection
= getPooledConnection(aIter
);
168 if ( !xConnection
.is() )
169 xConnection
= createNewConnection(_rURL
,_rInfo
);
174 void SAL_CALL
OConnectionPool::disposing( const css::lang::EventObject
& Source
)
176 Reference
<XConnection
> xConnection(Source
.Source
,UNO_QUERY
);
179 std::unique_lock
aGuard(m_aMutex
);
180 TActiveConnectionMap::iterator aIter
= m_aActiveConnections
.find(xConnection
);
181 OSL_ENSURE(aIter
!= m_aActiveConnections
.end(),"OConnectionPool::disposing: Connection wasn't in pool");
182 if(aIter
!= m_aActiveConnections
.end())
183 { // move the pooled connection back to the pool
184 aIter
->second
.aPos
->second
.nALiveCount
= m_nALiveCount
;
185 aIter
->second
.aPos
->second
.aConnections
.push_back(aIter
->second
.xPooledConnection
);
186 m_aActiveConnections
.erase(aIter
);
191 m_xDriverNode
.clear();
195 Reference
< XConnection
> OConnectionPool::createNewConnection(const OUString
& _rURL
,const Sequence
< PropertyValue
>& _rInfo
)
197 // create new pooled connection
198 Reference
< XPooledConnection
> xPooledConnection
= new ::connectivity::OPooledConnection(m_xDriver
->connect(_rURL
,_rInfo
),m_xProxyFactory
);
199 // get the new connection from the pooled connection
200 Reference
<XConnection
> xConnection
= xPooledConnection
->getConnection();
203 // add our own as dispose listener to know when we should put the connection back to the pool
204 Reference
< XComponent
> xComponent(xConnection
, UNO_QUERY
);
206 xComponent
->addEventListener(this);
208 // save some information to find the right pool later on
209 Sequence
< PropertyValue
> aInfo(_rInfo
);
210 TConnectionMap::key_type nId
;
211 OConnectionWrapper::createUniqueId(_rURL
,aInfo
,nId
.m_pBuffer
);
212 TConnectionPool aPack
;
214 // insert the new connection and struct into the active connection map
215 aPack
.nALiveCount
= m_nALiveCount
;
216 TActiveConnectionInfo aActiveInfo
;
217 aActiveInfo
.aPos
= m_aPool
.emplace(nId
,aPack
).first
;
218 aActiveInfo
.xPooledConnection
= std::move(xPooledConnection
);
219 m_aActiveConnections
.emplace(xConnection
,aActiveInfo
);
221 if(m_xInvalidator
->isExpired())
222 m_xInvalidator
->start();
228 void OConnectionPool::invalidatePooledConnections()
230 std::unique_lock
aGuard(m_aMutex
);
231 TConnectionMap::iterator aIter
= m_aPool
.begin();
232 for (; aIter
!= m_aPool
.end(); )
234 if(!(--(aIter
->second
.nALiveCount
))) // connections are invalid
236 std::for_each(aIter
->second
.aConnections
.begin(),aIter
->second
.aConnections
.end(),TRemoveEventListenerFunctor(this,true));
238 aIter
->second
.aConnections
.clear();
240 // look if the iterator aIter is still present in the active connection map
241 bool isPresent
= std::any_of(m_aActiveConnections
.begin(), m_aActiveConnections
.end(),
242 [&aIter
](const TActiveConnectionMap::value_type
& rEntry
) { return rEntry
.second
.aPos
== aIter
; });
244 {// he isn't so we can delete him
245 aIter
= m_aPool
.erase(aIter
);
254 m_xInvalidator
->start();
257 Reference
< XConnection
> OConnectionPool::getPooledConnection(TConnectionMap::iterator
const & _rIter
)
259 Reference
<XConnection
> xConnection
;
261 if(!_rIter
->second
.aConnections
.empty())
263 Reference
< XPooledConnection
> xPooledConnection
= _rIter
->second
.aConnections
.back();
264 _rIter
->second
.aConnections
.pop_back();
266 OSL_ENSURE(xPooledConnection
.is(),"Can not be null here!");
267 xConnection
= xPooledConnection
->getConnection();
268 Reference
< XComponent
> xComponent(xConnection
, UNO_QUERY
);
270 xComponent
->addEventListener(this);
272 TActiveConnectionInfo aActiveInfo
;
273 aActiveInfo
.aPos
= _rIter
;
274 aActiveInfo
.xPooledConnection
= std::move(xPooledConnection
);
275 m_aActiveConnections
[xConnection
] = std::move(aActiveInfo
);
280 void SAL_CALL
OConnectionPool::propertyChange( const PropertyChangeEvent
& evt
)
282 if(TIMEOUT_NODENAME
== evt
.PropertyName
)
284 OPoolCollection::getNodeValue(TIMEOUT_NODENAME
, m_xDriverNode
) >>= m_nALiveCount
;
289 void OConnectionPool::calculateTimeOuts()
291 sal_Int32 nTimeOutCorrection
= 10;
292 if(m_nALiveCount
< 100)
293 nTimeOutCorrection
= 20;
295 m_nTimeOut
= m_nALiveCount
/ nTimeOutCorrection
;
296 m_nALiveCount
= m_nALiveCount
/ m_nTimeOut
;
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */