Skip to content
Permalink
Browse files

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 13, 2015
1 parent b73e1a2 commit 60b14a2f51c3dad4cc80ea23fa9ff8e051c05270
@@ -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 )
@@ -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(); }
@@ -71,6 +77,9 @@ class QgsOgrConnPoolGroup : public QObject, public QgsConnectionPoolGroup<QgsOgr
protected:
Q_DISABLE_COPY( QgsOgrConnPoolGroup )

private:
int mRefCount;

};

/** Ogr connection pool - singleton */
@@ -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 )

@@ -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 )
@@ -28,6 +28,7 @@ class QgsOgrFeatureSource : public QgsAbstractFeatureSource
{
public:
QgsOgrFeatureSource( const QgsOgrProvider* p );
~QgsOgrFeatureSource();

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request ) override;

@@ -450,6 +450,8 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
mNativeTypes
<< QgsVectorDataProvider::NativeType( tr( "Date & Time" ), "datetime", QVariant::DateTime );
}

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

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

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

QgsAbstractFeatureSource* QgsOgrProvider::featureSource() const

0 comments on commit 60b14a2

Please sign in to comment.
You can’t perform that action at this time.