Skip to content

Commit 8cdd723

Browse files
committed
Merge pull request #2757 from manisandro/pool_invalidate_release
Connection pool improvements
2 parents 070b28a + 95030d6 commit 8cdd723

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

src/core/qgsconnectionpool.h

+17-9
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,22 @@ class QgsConnectionPoolGroup
128128
{
129129
connMutex.lock();
130130
acquiredConns.removeAll( conn );
131-
Item i;
132-
i.c = conn;
133-
i.lastUsedTime = QTime::currentTime();
134-
conns.push( i );
135-
136-
if ( !expirationTimer->isActive() )
131+
if ( !qgsConnectionPool_ConnectionIsValid( conn ) )
132+
{
133+
qgsConnectionPool_ConnectionDestroy( conn );
134+
}
135+
else
137136
{
138-
// will call the slot directly or queue the call (if the object lives in a different thread)
139-
QMetaObject::invokeMethod( expirationTimer->parent(), "startExpirationTimer" );
137+
Item i;
138+
i.c = conn;
139+
i.lastUsedTime = QTime::currentTime();
140+
conns.push( i );
141+
142+
if ( !expirationTimer->isActive() )
143+
{
144+
// will call the slot directly or queue the call (if the object lives in a different thread)
145+
QMetaObject::invokeMethod( expirationTimer->parent(), "startExpirationTimer" );
146+
}
140147
}
141148

142149
connMutex.unlock();
@@ -149,8 +156,9 @@ class QgsConnectionPoolGroup
149156
connMutex.lock();
150157
Q_FOREACH ( Item i, conns )
151158
{
152-
qgsConnectionPool_InvalidateConnection( i.c );
159+
qgsConnectionPool_ConnectionDestroy( i.c );
153160
}
161+
conns.clear();
154162
Q_FOREACH ( T c, acquiredConns )
155163
qgsConnectionPool_InvalidateConnection( c );
156164
connMutex.unlock();

src/providers/ogr/qgsogrconnpool.h

+13-10
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
105105
//
106106
static void cleanupInstance();
107107

108+
/**
109+
* @brief Increases the reference count on the connection pool for the specified connection.
110+
* @param connInfo The connection string.
111+
* @note
112+
* Any user of the connection pool needs to increase the reference count
113+
* before it acquires any connections and decrease the reference count after
114+
* releasing all acquired connections to ensure that all open OGR handles
115+
* are freed when and only when no one is using the pool anymore.
116+
*/
108117
void ref( const QString& connInfo )
109118
{
110119
mMutex.lock();
@@ -115,6 +124,10 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
115124
mMutex.unlock();
116125
}
117126

127+
/**
128+
* @brief Decrease the reference count on the connection pool for the specified connection.
129+
* @param connInfo The connection string.
130+
*/
118131
void unref( const QString& connInfo )
119132
{
120133
mMutex.lock();
@@ -133,16 +146,6 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
133146
mMutex.unlock();
134147
}
135148

136-
static void refS( const QString &connInfo )
137-
{
138-
instance()->ref( connInfo );
139-
}
140-
141-
static void unrefS( const QString &connInfo )
142-
{
143-
instance()->unref( connInfo );
144-
}
145-
146149
protected:
147150
Q_DISABLE_COPY( QgsOgrConnPool )
148151

src/providers/ogr/qgsogrfeatureiterator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,12 @@ QgsOgrFeatureSource::QgsOgrFeatureSource( const QgsOgrProvider* p )
404404
mFields = p->mAttributeFields;
405405
mDriverName = p->ogrDriverName;
406406
mOgrGeometryTypeFilter = wkbFlatten( p->mOgrGeometryTypeFilter );
407-
QgsOgrConnPool::refS( mFilePath );
407+
QgsOgrConnPool::instance()->ref( mFilePath );
408408
}
409409

410410
QgsOgrFeatureSource::~QgsOgrFeatureSource()
411411
{
412-
QgsOgrConnPool::unrefS( mFilePath );
412+
QgsOgrConnPool::instance()->unref( mFilePath );
413413
}
414414

415415
QgsFeatureIterator QgsOgrFeatureSource::getFeatures( const QgsFeatureRequest& request )

src/providers/ogr/qgsogrprovider.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
374374
<< QgsVectorDataProvider::NativeType( tr( "Date & Time" ), "datetime", QVariant::DateTime );
375375
}
376376

377-
QgsOgrConnPool::refS( mFilePath );
377+
QgsOgrConnPool::instance()->ref( mFilePath );
378378
}
379379

380380
QgsOgrProvider::~QgsOgrProvider()
@@ -2590,7 +2590,7 @@ QString QgsOgrUtils::quotedValue( const QVariant& value )
25902590
bool QgsOgrProvider::syncToDisc()
25912591
{
25922592
//for shapefiles, remove spatial index files and create a new index
2593-
QgsOgrConnPool::unrefS( mFilePath );
2593+
QgsOgrConnPool::instance()->unref( mFilePath );
25942594
bool shapeIndex = false;
25952595
if ( ogrDriverName == "ESRI Shapefile" )
25962596
{
@@ -2621,7 +2621,7 @@ bool QgsOgrProvider::syncToDisc()
26212621

26222622
mShapefileMayBeCorrupted = false;
26232623

2624-
QgsOgrConnPool::refS( mFilePath );
2624+
QgsOgrConnPool::instance()->ref( mFilePath );
26252625
if ( shapeIndex )
26262626
{
26272627
return createSpatialIndex();
@@ -2834,7 +2834,7 @@ void QgsOgrProvider::close()
28342834

28352835
updateExtents();
28362836

2837-
QgsOgrConnPool::unrefS( mFilePath );
2837+
QgsOgrConnPool::instance()->unref( mFilePath );
28382838
}
28392839

28402840
// ---------------------------------------------------------------------------

tests/src/analysis/testqgsvectoranalyzer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ void TestQgsVectorAnalyzer::initTestCase()
8686
}
8787
void TestQgsVectorAnalyzer::cleanupTestCase()
8888
{
89+
delete mpLineLayer;
90+
delete mpPolyLayer;
91+
delete mpPointLayer;
8992
QgsApplication::exitQgis();
9093
}
9194
void TestQgsVectorAnalyzer::init()

0 commit comments

Comments
 (0)