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 77 for (
const Item &item : qgis::as_const(
conns ) )
79 qgsConnectionPool_ConnectionDestroy( item.c );
95 T
acquire(
int timeout,
bool requestMayBeNested )
97 const int requiredFreeConnectionCount = requestMayBeNested ? 1 : 3;
101 if ( !
sem.tryAcquire( requiredFreeConnectionCount, timeout ) )
110 sem.acquire( requiredFreeConnectionCount );
112 sem.release( requiredFreeConnectionCount - 1 );
118 if ( !
conns.isEmpty() )
121 if ( !qgsConnectionPool_ConnectionIsValid( i.
c ) )
123 qgsConnectionPool_ConnectionDestroy( i.
c );
124 qgsConnectionPool_ConnectionCreate(
connInfo, i.
c );
129 if (
conns.isEmpty() )
132 QMetaObject::invokeMethod(
expirationTimer->parent(),
"stopExpirationTimer" );
142 qgsConnectionPool_ConnectionCreate(
connInfo, c );
160 if ( !qgsConnectionPool_ConnectionIsValid( conn ) )
162 qgsConnectionPool_ConnectionDestroy( conn );
174 QMetaObject::invokeMethod(
expirationTimer->parent(),
"startExpirationTimer" );
186 for (
const Item &i : qgis::as_const(
conns ) )
188 qgsConnectionPool_ConnectionDestroy( i.c );
192 qgsConnectionPool_InvalidateConnection(
c );
202 QObject::connect(
expirationTimer, SIGNAL( timeout() ), parent, SLOT( handleConnectionExpired() ) );
206 parent->moveToThread( qApp->thread() );
213 QTime now = QTime::currentTime();
217 for (
int i = 0; i <
conns.count(); ++i )
220 toDelete.append( i );
224 for (
int j = toDelete.count() - 1; j >= 0; --j )
226 int index = toDelete[j];
227 qgsConnectionPool_ConnectionDestroy(
conns[index].
c );
228 conns.remove( index );
231 if (
conns.isEmpty() )
265 template <
typename T,
typename T_Group>
275 for ( T_Group *group : qgis::as_const( mGroups ) )
295 typename T_Groups::iterator it = mGroups.find( connInfo );
296 if ( it == mGroups.end() )
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 ) )
340 #endif // QGSCONNECTIONPOOL_H
Extends QApplication to provide access to QGIS specific resources such as theme paths, database paths etc.
virtual ~QgsConnectionPool()
#define CONN_POOL_EXPIRATION_TIME
void invalidateConnections(const QString &connInfo)
Invalidates all connections to the specified resource.
QMap< QString, T_Group * > T_Groups
T acquireConnection(const QString &connInfo, int timeout=-1, bool requestMayBeNested=false)
Try to acquire a connection for a maximum of timeout milliseconds.
void initTimer(QObject *parent)
QgsConnectionPoolGroup & operator=(const QgsConnectionPoolGroup &other)=delete
QgsConnectionPoolGroup cannot be copied.
void releaseConnection(T conn)
Release an existing connection so it will get back into the pool and can be reused.
void onConnectionExpired()
~QgsConnectionPoolGroup()
T acquire(int timeout, bool requestMayBeNested)
Try to acquire a connection for a maximum of timeout milliseconds.
#define CONN_POOL_SPARE_CONNECTIONS
Template class responsible for keeping a pool of open connections.
Template that stores data related to a connection to a single server or datasource.
QgsConnectionPoolGroup(const QString &ci)
void invalidateConnections()