16 #ifndef QGSCONNECTIONPOOL_H
17 #define QGSCONNECTIONPOOL_H
23 #include <QCoreApplication>
33 #define CONN_POOL_EXPIRATION_TIME 60 // in seconds
34 #define CONN_POOL_SPARE_CONNECTIONS 2 // number of spare connections in case all the base connections are used but we have a nested request with the risk of a deadlock
79 for (
const Item &item : qgis::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 : qgis::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() )
267 template <
typename T,
typename T_Group>
277 for ( T_Group *group : qgis::as_const(
mGroups ) )
292 T
acquireConnection(
const QString &connInfo,
int timeout = -1,
bool requestMayBeNested =
false )
295 typename T_Groups::iterator it =
mGroups.find( connInfo );
298 it =
mGroups.insert( connInfo,
new T_Group( connInfo ) );
300 T_Group *group = *it;
303 return group->acquire( timeout, requestMayBeNested );
310 typename T_Groups::iterator it =
mGroups.find( qgsConnectionPool_ConnectionToName( conn ) );
311 Q_ASSERT( it !=
mGroups.end() );
312 T_Group *group = *it;
315 group->release( conn );
328 if (
mGroups.contains( connInfo ) )
329 mGroups[connInfo]->invalidateConnections();
340 #endif // QGSCONNECTIONPOOL_H