Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2197 from manisandro/dropconnections
Add QgsConnectionPool::dropConnections, delete connection groups when pool is destroyed
(cherry picked from commit 2a7ccca)
  • Loading branch information
jef-n committed Jul 14, 2015
1 parent b73e1a2 commit 60b14a2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/core/qgsconnectionpool.h
Expand Up @@ -228,6 +228,17 @@ class QgsConnectionPool

typedef QMap<QString, T_Group*> T_Groups;

virtual ~QgsConnectionPool()
{
mMutex.lock();
foreach ( T_Group* group, mGroups )
{
delete group;
}
mGroups.clear();
mMutex.unlock();
}

//! Try to acquire a connection: if no connections are available, the thread will get blocked.
//! @return initialized connection or null on error
T acquireConnection( const QString& connInfo )
Expand Down
34 changes: 33 additions & 1 deletion src/providers/ogr/qgsogrconnpool.h
Expand Up @@ -61,7 +61,13 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
Q_OBJECT

public:
QgsOgrConnPoolGroup( QString name ) : QgsConnectionPoolGroup<QgsOgrConn*>( name ) { initTimer( this ); }
QgsOgrConnPoolGroup( QString name ) : QgsConnectionPoolGroup<QgsOgrConn*>( name ), mRefCount( 0 ) { initTimer( this ); }
void ref() { ++mRefCount; }
bool unref()
{
Q_ASSERT( mRefCount > 0 );
return --mRefCount == 0;
}

protected slots:
void handleConnectionExpired() { onConnectionExpired(); }
Expand All @@ -71,6 +77,9 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
protected:
Q_DISABLE_COPY( QgsOgrConnPoolGroup )

private:
int mRefCount;

};

/** Ogr connection pool - singleton */
Expand All @@ -79,6 +88,29 @@ class QgsOgrConnPool : public QgsConnectionPool<QgsOgrConn*, QgsOgrConnPoolGroup
public:
static QgsOgrConnPool* instance();

void ref( const QString& connInfo )
{
mMutex.lock();
T_Groups::iterator it = mGroups.find( connInfo );
if ( it == mGroups.end() )
it = mGroups.insert( connInfo, new QgsOgrConnPoolGroup( connInfo ) );
it.value()->ref();
mMutex.unlock();
}

void unref( const QString& connInfo )
{
mMutex.lock();
T_Groups::iterator it = mGroups.find( connInfo );
Q_ASSERT( it != mGroups.end() );
if ( it.value()->unref() )
{
delete it.value();
mGroups.erase( it );
}
mMutex.unlock();
}

protected:
Q_DISABLE_COPY( QgsOgrConnPool )

Expand Down
6 changes: 6 additions & 0 deletions src/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -342,6 +342,12 @@ QgsOgrFeatureSource::QgsOgrFeatureSource( const QgsOgrProvider* p )
mFields = p->mAttributeFields;
mDriverName = p->ogrDriverName;
mOgrGeometryTypeFilter = wkbFlatten( p->mOgrGeometryTypeFilter );
QgsOgrConnPool::instance()->ref( mFilePath );
}

QgsOgrFeatureSource::~QgsOgrFeatureSource()
{
QgsOgrConnPool::instance()->unref( mFilePath );
}

QgsFeatureIterator QgsOgrFeatureSource::getFeatures( const QgsFeatureRequest& request )
Expand Down
1 change: 1 addition & 0 deletions src/providers/ogr/qgsogrfeatureiterator.h
Expand Up @@ -28,6 +28,7 @@ class QgsOgrFeatureSource : public QgsAbstractFeatureSource
{
public:
QgsOgrFeatureSource( const QgsOgrProvider* p );
~QgsOgrFeatureSource();

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request ) override;

Expand Down
4 changes: 4 additions & 0 deletions src/providers/ogr/qgsogrprovider.cpp
Expand Up @@ -450,6 +450,8 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
mNativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Date & Time" ), "datetime", QVariant::DateTime );
}

QgsOgrConnPool::instance()->ref( mFilePath );
}

QgsOgrProvider::~QgsOgrProvider()
Expand All @@ -470,6 +472,8 @@ QgsOgrProvider::~QgsOgrProvider()
free( extent_ );
extent_ = 0;
}

QgsOgrConnPool::instance()->unref( mFilePath );
}

QgsAbstractFeatureSource* QgsOgrProvider::featureSource() const
Expand Down

0 comments on commit 60b14a2

Please sign in to comment.