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 .
20 #include <sal/config.h>
26 #include <com/sun/star/sdbc/XPooledConnection.hpp>
27 #include <com/sun/star/sdbc/XDriver.hpp>
28 #include <com/sun/star/beans/PropertyValue.hpp>
29 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
30 #include <com/sun/star/reflection/XProxyFactory.hpp>
31 #include <cppuhelper/implbase.hxx>
32 #include <osl/mutex.hxx>
33 #include <salhelper/timer.hxx>
34 #include <rtl/ref.hxx>
35 #include <rtl/digest.h>
37 namespace connectivity
39 class OConnectionPool
;
41 /// OPoolTimer - Invalidates the connection pool
43 class OPoolTimer
: public ::salhelper::Timer
45 OConnectionPool
* m_pPool
;
47 OPoolTimer(OConnectionPool
* _pPool
,const ::salhelper::TTimeValue
& Time
)
48 : ::salhelper::Timer(Time
)
52 virtual void SAL_CALL
onShot() override
;
56 // OConnectionPool - the one-instance service for PooledConnections
57 // manages the active connections and the connections in the pool
59 // typedef for the internal structure
60 typedef std::vector
< css::uno::Reference
< css::sdbc::XPooledConnection
> > TPooledConnections
;
62 // contains the currently pooled connections
63 struct TConnectionPool
65 TPooledConnections aConnections
;
66 sal_Int32 nALiveCount
; // will be decremented every time a time says to, when will reach zero the pool will be deleted
71 sal_uInt8 m_pBuffer
[RTL_DIGEST_LENGTH_SHA1
];
79 // typedef TDigestHolder
83 bool operator() (const TDigestHolder
& x
, const TDigestHolder
& y
) const
86 for(i
=0;i
< RTL_DIGEST_LENGTH_SHA1
&& (x
.m_pBuffer
[i
] >= y
.m_pBuffer
[i
]); ++i
)
88 return i
< RTL_DIGEST_LENGTH_SHA1
;
92 typedef std::map
< TDigestHolder
,TConnectionPool
,TDigestLess
> TConnectionMap
;
94 // contains additional information about an activeconnection which is needed to put it back to the pool
95 struct TActiveConnectionInfo
97 TConnectionMap::iterator aPos
;
98 css::uno::Reference
< css::sdbc::XPooledConnection
> xPooledConnection
;
101 typedef std::map
< css::uno::Reference
< css::sdbc::XConnection
>,
102 TActiveConnectionInfo
> TActiveConnectionMap
;
104 class OConnectionPool
: public ::cppu::WeakImplHelper
< css::beans::XPropertyChangeListener
>
106 TConnectionMap m_aPool
; // the pooled connections
107 TActiveConnectionMap m_aActiveConnections
; // the currently active connections
110 ::rtl::Reference
<OPoolTimer
> m_xInvalidator
; // invalidates the connection pool when shot
112 css::uno::Reference
< css::sdbc::XDriver
> m_xDriver
; // the one and only driver for this connectionpool
113 css::uno::Reference
< css::uno::XInterface
> m_xDriverNode
; // config node entry
114 css::uno::Reference
< css::reflection::XProxyFactory
> m_xProxyFactory
;
115 sal_Int32 m_nTimeOut
;
116 sal_Int32 m_nALiveCount
;
119 css::uno::Reference
< css::sdbc::XConnection
> createNewConnection(const OUString
& _rURL
,
120 const css::uno::Sequence
< css::beans::PropertyValue
>& _rInfo
);
121 css::uno::Reference
< css::sdbc::XConnection
> getPooledConnection(TConnectionMap::iterator
const & _rIter
);
122 // calculate the timeout and the corresponding ALiveCount
123 void calculateTimeOuts();
126 // the dtor will be called from the last instance (last release call)
127 virtual ~OConnectionPool() override
;
129 OConnectionPool(const css::uno::Reference
< css::sdbc::XDriver
>& _xDriver
,
130 const css::uno::Reference
< css::uno::XInterface
>& _xDriverNode
,
131 const css::uno::Reference
< css::reflection::XProxyFactory
>& _rxProxyFactory
);
134 void clear(bool _bDispose
);
135 /// @throws css::sdbc::SQLException
136 /// @throws css::uno::RuntimeException
137 css::uno::Reference
< css::sdbc::XConnection
> getConnectionWithInfo( const OUString
& url
, const css::uno::Sequence
< css::beans::PropertyValue
>& info
);
139 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
140 // XPropertyChangeListener
141 virtual void SAL_CALL
propertyChange( const css::beans::PropertyChangeEvent
& evt
) override
;
143 void invalidatePooledConnections();
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */