1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "ZConnectionPool.hxx"
#include <com/sun/star/lang/XComponent.hpp>
#include "ZPooledConnection.hxx"
#include "ZPoolCollection.hxx"
#include <connectivity/ConnectionWrapper.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <algorithm>
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace connectivity;
void SAL_CALL OPoolTimer::onShot()
{
m_pPool->invalidatePooledConnections();
}
constexpr OUString TIMEOUT_NODENAME = u"Timeout"_ustr;
OConnectionPool::OConnectionPool(const Reference< XDriver >& _xDriver,
const Reference< XInterface >& _xDriverNode,
const Reference< css::reflection::XProxyFactory >& _rxProxyFactory)
:m_xDriver(_xDriver)
,m_xDriverNode(_xDriverNode)
,m_xProxyFactory(_rxProxyFactory)
,m_nTimeOut(10)
,m_nALiveCount(10)
{
OSL_ENSURE(m_xDriverNode.is(),"NO valid Driver node set!");
Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY);
if (xComponent.is())
xComponent->addEventListener(this);
Reference<XPropertySet> xProp(m_xDriverNode,UNO_QUERY);
if(xProp.is())
xProp->addPropertyChangeListener(TIMEOUT_NODENAME,this);
OPoolCollection::getNodeValue(TIMEOUT_NODENAME, m_xDriverNode) >>= m_nALiveCount;
calculateTimeOuts();
m_xInvalidator = new OPoolTimer(this,::salhelper::TTimeValue(m_nTimeOut,0));
m_xInvalidator->start();
}
OConnectionPool::~OConnectionPool()
{
clear(false);
}
namespace {
struct TRemoveEventListenerFunctor
{
OConnectionPool* m_pConnectionPool;
bool m_bDispose;
TRemoveEventListenerFunctor(OConnectionPool* _pConnectionPool, bool _bDispose)
: m_pConnectionPool(_pConnectionPool)
,m_bDispose(_bDispose)
{
OSL_ENSURE(m_pConnectionPool,"No connection pool!");
}
void dispose(const Reference<XInterface>& _xComponent)
{
Reference< XComponent > xComponent(_xComponent, UNO_QUERY);
if ( xComponent.is() )
{
xComponent->removeEventListener(m_pConnectionPool);
if ( m_bDispose )
xComponent->dispose();
}
}
void operator()(const TPooledConnections::value_type& _aValue)
{
dispose(_aValue);
}
void operator()(const TActiveConnectionMap::value_type& _aValue)
{
dispose(_aValue.first);
}
};
struct TConnectionPoolFunctor
{
OConnectionPool* m_pConnectionPool;
explicit TConnectionPoolFunctor(OConnectionPool* _pConnectionPool)
: m_pConnectionPool(_pConnectionPool)
{
OSL_ENSURE(m_pConnectionPool,"No connection pool!");
}
void operator()(const TConnectionMap::value_type& _aValue)
{
std::for_each(_aValue.second.aConnections.begin(),_aValue.second.aConnections.end(),TRemoveEventListenerFunctor(m_pConnectionPool,true));
}
};
}
void OConnectionPool::clear(bool _bDispose)
{
std::unique_lock aGuard(m_aMutex);
if(m_xInvalidator->isTicking())
m_xInvalidator->stop();
std::for_each(m_aPool.begin(),m_aPool.end(),TConnectionPoolFunctor(this));
m_aPool.clear();
std::for_each(m_aActiveConnections.begin(),m_aActiveConnections.end(),TRemoveEventListenerFunctor(this,_bDispose));
m_aActiveConnections.clear();
Reference< XComponent > xComponent(m_xDriverNode, UNO_QUERY);
if (xComponent.is())
xComponent->removeEventListener(this);
Reference< XPropertySet > xProp(m_xDriverNode, UNO_QUERY);
if (xProp.is())
xProp->removePropertyChangeListener(TIMEOUT_NODENAME, this);
m_xDriverNode.clear();
m_xDriver.clear();
}
Reference< XConnection > OConnectionPool::getConnectionWithInfo( const OUString& _rURL, const Sequence< PropertyValue >& _rInfo )
{
std::unique_lock aGuard(m_aMutex);
Reference<XConnection> xConnection;
// create a unique id and look for it in our map
Sequence< PropertyValue > aInfo(_rInfo);
TConnectionMap::key_type nId;
OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer);
TConnectionMap::iterator aIter = m_aPool.find(nId);
if ( m_aPool.end() != aIter )
xConnection = getPooledConnection(aIter);
if ( !xConnection.is() )
xConnection = createNewConnection(_rURL,_rInfo);
return xConnection;
}
void SAL_CALL OConnectionPool::disposing( const css::lang::EventObject& Source )
{
Reference<XConnection> xConnection(Source.Source,UNO_QUERY);
if(xConnection.is())
{
std::unique_lock aGuard(m_aMutex);
TActiveConnectionMap::iterator aIter = m_aActiveConnections.find(xConnection);
OSL_ENSURE(aIter != m_aActiveConnections.end(),"OConnectionPool::disposing: Connection wasn't in pool");
if(aIter != m_aActiveConnections.end())
{ // move the pooled connection back to the pool
aIter->second.aPos->second.nALiveCount = m_nALiveCount;
aIter->second.aPos->second.aConnections.push_back(aIter->second.xPooledConnection);
m_aActiveConnections.erase(aIter);
}
}
else
{
m_xDriverNode.clear();
}
}
Reference< XConnection> OConnectionPool::createNewConnection(const OUString& _rURL,const Sequence< PropertyValue >& _rInfo)
{
// create new pooled connection
Reference< XPooledConnection > xPooledConnection = new ::connectivity::OPooledConnection(m_xDriver->connect(_rURL,_rInfo),m_xProxyFactory);
// get the new connection from the pooled connection
Reference<XConnection> xConnection = xPooledConnection->getConnection();
if(xConnection.is())
{
// add our own as dispose listener to know when we should put the connection back to the pool
Reference< XComponent > xComponent(xConnection, UNO_QUERY);
if (xComponent.is())
xComponent->addEventListener(this);
// save some information to find the right pool later on
Sequence< PropertyValue > aInfo(_rInfo);
TConnectionMap::key_type nId;
OConnectionWrapper::createUniqueId(_rURL,aInfo,nId.m_pBuffer);
TConnectionPool aPack;
// insert the new connection and struct into the active connection map
aPack.nALiveCount = m_nALiveCount;
TActiveConnectionInfo aActiveInfo;
aActiveInfo.aPos = m_aPool.emplace(nId,aPack).first;
aActiveInfo.xPooledConnection = std::move(xPooledConnection);
m_aActiveConnections.emplace(xConnection,aActiveInfo);
if(m_xInvalidator->isExpired())
m_xInvalidator->start();
}
return xConnection;
}
void OConnectionPool::invalidatePooledConnections()
{
std::unique_lock aGuard(m_aMutex);
TConnectionMap::iterator aIter = m_aPool.begin();
for (; aIter != m_aPool.end(); )
{
if(!(--(aIter->second.nALiveCount))) // connections are invalid
{
std::for_each(aIter->second.aConnections.begin(),aIter->second.aConnections.end(),TRemoveEventListenerFunctor(this,true));
aIter->second.aConnections.clear();
// look if the iterator aIter is still present in the active connection map
bool isPresent = std::any_of(m_aActiveConnections.begin(), m_aActiveConnections.end(),
[&aIter](const TActiveConnectionMap::value_type& rEntry) { return rEntry.second.aPos == aIter; });
if(!isPresent)
{// he isn't so we can delete him
aIter = m_aPool.erase(aIter);
}
else
++aIter;
}
else
++aIter;
}
if(!m_aPool.empty())
m_xInvalidator->start();
}
Reference< XConnection> OConnectionPool::getPooledConnection(TConnectionMap::iterator const & _rIter)
{
Reference<XConnection> xConnection;
if(!_rIter->second.aConnections.empty())
{
Reference< XPooledConnection > xPooledConnection = _rIter->second.aConnections.back();
_rIter->second.aConnections.pop_back();
OSL_ENSURE(xPooledConnection.is(),"Can not be null here!");
xConnection = xPooledConnection->getConnection();
Reference< XComponent > xComponent(xConnection, UNO_QUERY);
if (xComponent.is())
xComponent->addEventListener(this);
TActiveConnectionInfo aActiveInfo;
aActiveInfo.aPos = _rIter;
aActiveInfo.xPooledConnection = std::move(xPooledConnection);
m_aActiveConnections[xConnection] = std::move(aActiveInfo);
}
return xConnection;
}
void SAL_CALL OConnectionPool::propertyChange( const PropertyChangeEvent& evt )
{
if(TIMEOUT_NODENAME == evt.PropertyName)
{
OPoolCollection::getNodeValue(TIMEOUT_NODENAME, m_xDriverNode) >>= m_nALiveCount;
calculateTimeOuts();
}
}
void OConnectionPool::calculateTimeOuts()
{
sal_Int32 nTimeOutCorrection = 10;
if(m_nALiveCount < 100)
nTimeOutCorrection = 20;
m_nTimeOut = m_nALiveCount / nTimeOutCorrection;
m_nALiveCount = m_nALiveCount / m_nTimeOut;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|