16 #ifndef QGSCONNECTIONPOOL_H 17 #define QGSCONNECTIONPOOL_H 22 #include <QCoreApplication> 32 #define CONN_POOL_MAX_CONCURRENT_CONNS 4 33 #define CONN_POOL_EXPIRATION_TIME 60 // in seconds 78 for (
const Item &item : qgis::as_const(
conns ) )
80 qgsConnectionPool_ConnectionDestroy( item.c );
101 if ( !
sem.tryAcquire( 1, timeout ) )
117 if ( !
conns.isEmpty() )
120 if ( !qgsConnectionPool_ConnectionIsValid( i.
c ) )
122 qgsConnectionPool_ConnectionDestroy( i.
c );
123 qgsConnectionPool_ConnectionCreate(
connInfo, i.
c );
127 if (
conns.isEmpty() )
130 QMetaObject::invokeMethod(
expirationTimer->parent(),
"stopExpirationTimer" );
140 qgsConnectionPool_ConnectionCreate(
connInfo, c );
158 if ( !qgsConnectionPool_ConnectionIsValid( conn ) )
160 qgsConnectionPool_ConnectionDestroy( conn );
172 QMetaObject::invokeMethod(
expirationTimer->parent(),
"startExpirationTimer" );
184 for (
const Item &i : qgis::as_const(
conns ) )
186 qgsConnectionPool_ConnectionDestroy( i.c );
190 qgsConnectionPool_InvalidateConnection(
c );
200 QObject::connect(
expirationTimer, SIGNAL( timeout() ), parent, SLOT( handleConnectionExpired() ) );
204 parent->moveToThread( qApp->thread() );
211 QTime now = QTime::currentTime();
215 for (
int i = 0; i <
conns.count(); ++i )
218 toDelete.append( i );
222 for (
int j = toDelete.count() - 1; j >= 0; --j )
224 int index = toDelete[j];
225 qgsConnectionPool_ConnectionDestroy(
conns[index].
c );
226 conns.remove( index );
229 if (
conns.isEmpty() )
263 template <
typename T,
typename T_Group>
273 for ( T_Group *group : qgis::as_const( mGroups ) )
291 typename T_Groups::iterator it = mGroups.find( connInfo );
292 if ( it == mGroups.end() )
294 it = mGroups.insert( connInfo,
new T_Group( connInfo ) );
296 T_Group *group = *it;
299 return group->acquire( timeout );
306 typename T_Groups::iterator it = mGroups.find( qgsConnectionPool_ConnectionToName( conn ) );
307 Q_ASSERT( it != mGroups.end() );
308 T_Group *group = *it;
311 group->release( conn );
324 if ( mGroups.contains( connInfo ) )
336 #endif // QGSCONNECTIONPOOL_H T acquireConnection(const QString &connInfo, int timeout=-1)
Try to acquire a connection for a maximum of timeout milliseconds.
#define CONN_POOL_MAX_CONCURRENT_CONNS
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
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.
static const int MAX_CONCURRENT_CONNECTIONS
T acquire(int timeout)
Try to acquire a connection for a maximum of timeout milliseconds.
void onConnectionExpired()
~QgsConnectionPoolGroup()
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()