Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #9280: fix "Add postgis layer" creates a connection that is not removed until qgis is terminated #1051

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/providers/postgres/qgscolumntypethread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void QgsGeomColumnTypeThread::stop()
void QgsGeomColumnTypeThread::run()
{
QgsDataSourceURI uri = QgsPostgresConn::connUri( mName );
mConn = QgsPostgresConn::connectDb( uri.connectionInfo(), true );
mConn = new QgsPostgresConn( uri.connectionInfo(), true );
if ( !mConn )
{
QgsDebugMsg( "Connection failed - " + uri.connectionInfo() );
Expand All @@ -63,6 +63,7 @@ void QgsGeomColumnTypeThread::run()
layerProperties.isEmpty() )
{
mConn->disconnect();
delete mConn;
mConn = 0;
return;
}
Expand Down Expand Up @@ -109,5 +110,6 @@ void QgsGeomColumnTypeThread::run()
emit progressMessage( tr( "Table retrieval finished." ) );

mConn->disconnect();
delete mConn;
mConn = 0;
}
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ class QgsPostgresConn : public QObject
static bool allowGeometrylessTables( QString theConnName );
static void deleteConnection( QString theConnName );

private:
public:
QgsPostgresConn( QString conninfo, bool readOnly );
~QgsPostgresConn();

private:
int mRef;
int mOpenCursors;
PGconn *mConn;
Expand Down
14 changes: 12 additions & 2 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3360,6 +3360,7 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
errCause = QObject::tr( "Operation aborted. No changes were made in the database" );
conn->disconnect();
return false;
}

Expand Down Expand Up @@ -3439,7 +3440,9 @@ QGISEXTERN QString loadStyle( const QString& uri, QString& errCause )

QgsPostgresResult result = conn->PQexec( selectQmlQuery, false );

return result.PQntuples() == 1 ? result.PQgetvalue( 0, 0 ) : "";
QString style = result.PQntuples() == 1 ? result.PQgetvalue( 0, 0 ) : "";
conn->disconnect();
return style;
}

QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &names,
Expand Down Expand Up @@ -3470,6 +3473,7 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectRelatedQuery ) );
errCause = QObject::tr( "Error executing the select query for related styles. The query was logged" );
conn->disconnect();
return -1;
}

Expand All @@ -3495,6 +3499,7 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectOthersQuery ) );
errCause = QObject::tr( "Error executing the select query for unrelated styles. The query was logged" );
conn->disconnect();
return -1;
}
for ( int i = 0; i < result.PQntuples(); i++ )
Expand All @@ -3503,6 +3508,7 @@ QGISEXTERN int listStyles( const QString &uri, QStringList &ids, QStringList &na
names.append( result.PQgetvalue( i, 1 ) );
descriptions.append( result.PQgetvalue( i, 2 ) );
}
conn->disconnect();

return numberOfRelatedStyles;
}
Expand All @@ -3524,16 +3530,20 @@ QGISEXTERN QString getStyleById( const QString& uri, QString styleId, QString& e
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) );
errCause = QObject::tr( "Error executing the select query. The query was logged" );
conn->disconnect();
return "";
}

if ( result.PQntuples() == 1 )
{
return result.PQgetvalue( 0, 0 );
QString style = result.PQgetvalue( 0, 0 );
conn->disconnect();
return style;
}
else
{
errCause = QObject::tr( "Consistency error in table '%1'. Style id should be unique" ).arg( "layer_styles" );
conn->disconnect();
return "";
}
}