16 #ifndef QGSCONNECTIONPOOL_H 17 #define QGSCONNECTIONPOOL_H 21 #include <QCoreApplication> 31 #define CONN_POOL_MAX_CONCURRENT_CONNS 4 32 #define CONN_POOL_EXPIRATION_TIME 60 // in seconds 79 qgsConnectionPool_ConnectionDestroy( item.
c );
100 if ( !
sem.tryAcquire( 1, timeout ) )
116 if ( !
conns.isEmpty() )
119 if ( !qgsConnectionPool_ConnectionIsValid( i.
c ) )
121 qgsConnectionPool_ConnectionDestroy( i.
c );
122 qgsConnectionPool_ConnectionCreate(
connInfo, i.
c );
126 if (
conns.isEmpty() )
129 QMetaObject::invokeMethod(
expirationTimer->parent(),
"stopExpirationTimer" );
139 qgsConnectionPool_ConnectionCreate(
connInfo, c );
157 if ( !qgsConnectionPool_ConnectionIsValid( conn ) )
159 qgsConnectionPool_ConnectionDestroy( conn );
171 QMetaObject::invokeMethod(
expirationTimer->parent(),
"startExpirationTimer" );
185 qgsConnectionPool_ConnectionDestroy( i.
c );
189 qgsConnectionPool_InvalidateConnection( c );
199 QObject::connect(
expirationTimer, SIGNAL( timeout() ), parent, SLOT( handleConnectionExpired() ) );
203 parent->moveToThread( qApp->thread() );
210 QTime now = QTime::currentTime();
214 for (
int i = 0; i <
conns.count(); ++i )
217 toDelete.append( i );
221 for (
int j = toDelete.count() - 1; j >= 0; --j )
223 int index = toDelete[j];
224 qgsConnectionPool_ConnectionDestroy(
conns[index].
c );
225 conns.remove( index );
228 if (
conns.isEmpty() )
262 template <
typename T,
typename T_Group>
272 Q_FOREACH ( T_Group *group, mGroups )
290 typename T_Groups::iterator it = mGroups.find( connInfo );
291 if ( it == mGroups.end() )
293 it = mGroups.insert( connInfo,
new T_Group( connInfo ) );
295 T_Group *group = *it;
298 return group->acquire( timeout );
305 typename T_Groups::iterator it = mGroups.find( qgsConnectionPool_ConnectionToName( conn ) );
306 Q_ASSERT( it != mGroups.end() );
307 T_Group *group = *it;
310 group->release( conn );
323 if ( mGroups.contains( connInfo ) )
335 #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 one server.
QgsConnectionPoolGroup(const QString &ci)
void invalidateConnections()