From b1af52f2b2e1d318fc7a5481659e8101bf2e18dd Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Mon, 27 Jul 2020 12:27:44 +0200 Subject: [PATCH] PostgreSQL: Attempt to subscribe even if it is already added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As the connection could be lost and then reconnected for the same driver instance then it should just do the LISTEN query as it will not do anything if it is already subscribed. Fixes: QTBUG-84356 Change-Id: I6179bca3991c3828ccee066fd96a6e5003c522be Pick-to: 5.15 Reviewed-by: Lisandro Damián Nicanor Pérez Meyer Reviewed-by: Volker Hilsheimer --- src/plugins/sqldrivers/psql/qsql_psql.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/plugins/sqldrivers/psql/qsql_psql.cpp b/src/plugins/sqldrivers/psql/qsql_psql.cpp index 60729840e21..cdc4ce7c36b 100644 --- a/src/plugins/sqldrivers/psql/qsql_psql.cpp +++ b/src/plugins/sqldrivers/psql/qsql_psql.cpp @@ -1580,21 +1580,20 @@ bool QPSQLDriver::subscribeToNotification(const QString &name) return false; } - if (d->seid.contains(name)) { - qWarning("QPSQLDriver::subscribeToNotificationImplementation: already subscribing to '%s'.", - qPrintable(name)); - return false; - } - + const bool alreadyContained = d->seid.contains(name); int socket = PQsocket(d->connection); if (socket) { // Add the name to the list of subscriptions here so that QSQLDriverPrivate::exec knows - // to check for notifications immediately after executing the LISTEN - d->seid << name; + // to check for notifications immediately after executing the LISTEN. If it has already + // been subscribed then LISTEN Will do nothing. But we do the call anyway in case the + // connection was lost and this is a re-subscription. + if (!alreadyContained) + d->seid << name; QString query = QStringLiteral("LISTEN ") + escapeIdentifier(name, QSqlDriver::TableName); PGresult *result = d->exec(query); if (PQresultStatus(result) != PGRES_COMMAND_OK) { - d->seid.removeLast(); + if (!alreadyContained) + d->seid.removeLast(); setLastError(qMakeError(tr("Unable to subscribe"), QSqlError::StatementError, d, result)); PQclear(result); return false;