16 #ifndef QGSCONNECTIONPOOL_H
17 #define QGSCONNECTIONPOOL_H
23 #include <QCoreApplication>
33 #define CONN_POOL_EXPIRATION_TIME 60
34 #define CONN_POOL_SPARE_CONNECTIONS 2
79 for (
const Item &item : std::as_const(
conns ) )
81 qgsConnectionPool_ConnectionDestroy( item.c );
97 T
acquire(
int timeout,
bool requestMayBeNested )
99 const int requiredFreeConnectionCount = requestMayBeNested ? 1 : 3;
103 if ( !
sem.tryAcquire( requiredFreeConnectionCount, timeout ) )
112 sem.acquire( requiredFreeConnectionCount );
114 sem.release( requiredFreeConnectionCount - 1 );
120 if ( !
conns.isEmpty() )
123 if ( !qgsConnectionPool_ConnectionIsValid( i.
c ) )
125 qgsConnectionPool_ConnectionDestroy( i.
c );
126 qgsConnectionPool_ConnectionCreate(
connInfo, i.
c );
131 if (
conns.isEmpty() )
134 QMetaObject::invokeMethod(
expirationTimer->parent(),
"stopExpirationTimer" );
144 qgsConnectionPool_ConnectionCreate(
connInfo,
c );
162 if ( !qgsConnectionPool_ConnectionIsValid( conn ) )
164 qgsConnectionPool_ConnectionDestroy( conn );
176 QMetaObject::invokeMethod(
expirationTimer->parent(),
"startExpirationTimer" );
188 for (
const Item &i : std::as_const(
conns ) )
190 qgsConnectionPool_ConnectionDestroy( i.c );
194 qgsConnectionPool_InvalidateConnection(
c );
204 QObject::connect(
expirationTimer, SIGNAL( timeout() ), parent, SLOT( handleConnectionExpired() ) );
208 parent->moveToThread( qApp->thread() );
215 QTime now = QTime::currentTime();
219 for (
int i = 0; i <
conns.count(); ++i )
222 toDelete.append( i );
226 for (
int j = toDelete.count() - 1; j >= 0; --j )
228 int index = toDelete[j];
229 qgsConnectionPool_ConnectionDestroy(
conns[index].
c );
230 conns.remove( index );
233 if (
conns.isEmpty() )
268 template <
typename T,
typename T_Group>
278 for ( T_Group *group : std::as_const(
mGroups ) )
293 T
acquireConnection(
const QString &connInfo,
int timeout = -1,
bool requestMayBeNested =
false )
296 typename T_Groups::iterator it =
mGroups.find( connInfo );
299 it =
mGroups.insert( connInfo,
new T_Group( connInfo ) );
301 T_Group *group = *it;
304 return group->acquire( timeout, requestMayBeNested );
311 typename T_Groups::iterator it =
mGroups.find( qgsConnectionPool_ConnectionToName( conn ) );
312 Q_ASSERT( it !=
mGroups.end() );
313 T_Group *group = *it;
316 group->release( conn );
329 if (
mGroups.contains( connInfo ) )
330 mGroups[connInfo]->invalidateConnections();
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
Template that stores data related to a connection to a single server or datasource.
void invalidateConnections()
QgsConnectionPoolGroup(const QgsConnectionPoolGroup &other)=delete
QgsConnectionPoolGroup cannot be copied.
T acquire(int timeout, bool requestMayBeNested)
Try to acquire a connection for a maximum of timeout milliseconds.
QgsConnectionPoolGroup(const QString &ci)
QgsConnectionPoolGroup & operator=(const QgsConnectionPoolGroup &other)=delete
QgsConnectionPoolGroup cannot be copied.
void initTimer(QObject *parent)
~QgsConnectionPoolGroup()
void onConnectionExpired()
Template class responsible for keeping a pool of open connections.
T acquireConnection(const QString &connInfo, int timeout=-1, bool requestMayBeNested=false)
Try to acquire a connection for a maximum of timeout milliseconds.
QMap< QString, T_Group * > T_Groups
void invalidateConnections(const QString &connInfo)
Invalidates all connections to the specified resource.
virtual ~QgsConnectionPool()
void releaseConnection(T conn)
Release an existing connection so it will get back into the pool and can be reused.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define CONN_POOL_SPARE_CONNECTIONS
#define CONN_POOL_EXPIRATION_TIME