Index: pgadmin/db/pgConn.cpp =================================================================== --- pgadmin/db/pgConn.cpp (revision 8074) +++ pgadmin/db/pgConn.cpp (working copy) @@ -75,7 +75,6 @@ conn=0; noticeArg=0; connStatus = PGCONN_BAD; - dbname = database; #ifdef __WXMSW__ struct in_addr ipaddr; @@ -117,7 +116,6 @@ wxLogInfo(wxT("Server name: %s (resolved to: %s)"), server.c_str(), hostip.c_str()); // Create the connection string - wxString connstr; if (!hostname.IsEmpty()) { connstr.Append(wxT(" host=")); connstr.Append(qtConnString(hostname)); @@ -165,12 +163,28 @@ } } connstr.Trim(false); - + + dbHost = server; + dbHostName = hostname; + dbHostAddress = hostip; + // Open the connection wxString cleanConnStr = connstr; cleanConnStr.Replace(qtConnString(password), wxT("'XXXXXX'")); wxLogInfo(wxT("Opening connection with connection string: %s"), cleanConnStr.c_str()); + + DoConnect(); +} + +pgConn::~pgConn() +{ + Close(); +} + + +bool pgConn::DoConnect() +{ wxCharBuffer cstrUTF=connstr.mb_str(wxConvUTF8); conn = PQconnectdb(cstrUTF); if (PQstatus(conn) == CONNECTION_OK) @@ -185,10 +199,6 @@ } } - dbHost = server; - dbHostName = hostname; - dbHostAddress = hostip; - // Set client encoding to Unicode/Ascii if (PQstatus(conn) == CONNECTION_OK) { @@ -199,15 +209,15 @@ wxString sql=wxT("SET DateStyle=ISO;SELECT oid, pg_encoding_to_char(encoding) AS encoding, datlastsysoid\n") wxT(" FROM pg_database WHERE "); - if (oid) - sql += wxT("oid = ") + NumToStr(oid); + if (save_oid) + sql += wxT("oid = ") + NumToStr(save_oid); else { // Note, can't use qtDbString here as we don't know the server version yet. - wxString db = database; + wxString db = save_database; db.Replace(wxT("\\"), wxT("\\\\")); db.Replace(wxT("'"), wxT("''")); - sql += wxT("datname=") + qtString(database); + sql += wxT("datname=") + qtString(save_database); } @@ -238,14 +248,13 @@ delete set; } } + else + return false; + + return true; } -pgConn::~pgConn() -{ - Close(); -} - void pgConn::Close() { if (conn) @@ -254,11 +263,33 @@ connStatus=PGCONN_BAD; } + +// Reconnect to the server +bool pgConn::Reconnect() +{ + // Close the existing (possibly broken) connection + Close(); + + // Reset any vars that need to be in a defined state before connecting + needColQuoting = false; + + // Attempt the reconnect + if (!DoConnect()) + { + wxLogError(_("Failed to re-establish the connection to the server %s"), GetName()); + return false; + } + + return true; +} + + pgConn *pgConn::Duplicate() { return new pgConn(wxString(save_server), wxString(save_database), wxString(save_username), wxString(save_password), save_port, save_sslmode, save_oid); } + // Return the SSL mode name wxString pgConn::GetSslModeName() { @@ -502,9 +533,9 @@ { wxString str; if (dbHost.IsEmpty()) - str.Printf(_("%s on local socket"), dbname.c_str()); + str.Printf(_("%s on local socket"), save_database.c_str()); else - str.Printf(_("%s on %s@%s:%d"), dbname.c_str(), GetUser().c_str(), dbHost.c_str(), GetPort()); + str.Printf(_("%s on %s@%s:%d"), save_database.c_str(), GetUser().c_str(), dbHost.c_str(), GetPort()); return str; } @@ -672,7 +703,7 @@ PQclear(qryRes); } } - return 0; + return new pgSet(); } ////////////////////////////////////////////////////////////////////////// @@ -723,10 +754,10 @@ { if (GetStatus() != PGCONN_OK) { - lastResultError.severity = wxString(wxT("FATAL")); - lastResultError.msg_primary = wxString(PQerrorMessage(conn), *conv); - lastResultError.formatted_msg = lastResultError.severity + wxT(": ") + lastResultError.msg_primary; - wxLogError(wxT("%s"), lastResultError.msg_primary.c_str()); +// lastResultError.severity = wxString(wxT("FATAL")); +// lastResultError.msg_primary = wxString(PQerrorMessage(conn), *conv); +// lastResultError.formatted_msg = lastResultError.severity + wxT(": ") + lastResultError.msg_primary; +// wxLogError(wxT("%s"), lastResultError.msg_primary.c_str()); return false; } Index: pgadmin/db/pgSet.cpp =================================================================== --- pgadmin/db/pgSet.cpp (revision 8074) +++ pgadmin/db/pgSet.cpp (working copy) @@ -24,6 +24,16 @@ #include "utils/sysLogger.h" #include "utils/pgDefs.h" +pgSet::pgSet() +: conv(wxConvLibc) +{ + conn = 0; + res = 0; + nCols = 0; + nRows = 0; + pos = 0; +} + pgSet::pgSet(PGresult *newRes, pgConn *newConn, wxMBConv &cnv, bool needColQt) : conv(cnv) { Index: pgadmin/frm/frmMain.cpp =================================================================== --- pgadmin/frm/frmMain.cpp (revision 8074) +++ pgadmin/frm/frmMain.cpp (working copy) @@ -762,11 +762,11 @@ conn->Close(); if (!userInformed) { - wxMessageDialog dlg(this, _("Close database browser? If you abort, the object browser will not show accurate data."), + wxMessageDialog dlg(this, _("Do you want to attempt to reconnect to the database?"), wxString::Format(_("Connection to database %s lost."), db->GetName().c_str()), wxICON_EXCLAMATION|wxYES_NO|wxYES_DEFAULT); - closeIt = (dlg.ShowModal() == wxID_YES); + closeIt = (dlg.ShowModal() == wxID_NO); userInformed = true; } if (closeIt) @@ -776,6 +776,29 @@ browser->DeleteChildren(db->GetId()); db->UpdateIcon(browser); } + else + { + // Create a server object and connect it. + wxBusyInfo waiting(wxString::Format(_("Reconnecting to database %s"), + db->GetName().c_str()), this); + + // Give the UI a chance to redraw + wxSafeYield(); + wxMilliSleep(100); + wxSafeYield(); + + if (!conn->Reconnect()) + { + db->Disconnect(); + + browser->DeleteChildren(db->GetId()); + db->UpdateIcon(browser); + } + else + // Indicate things are back to normal + userInformed = false; + } + } } } @@ -792,11 +815,11 @@ server->connection()->Close(); if (!userInformed) { - wxMessageDialog dlg(this, _("Close server browser? If you abort, the object browser will not show accurate data."), + wxMessageDialog dlg(this, _("Do you want to attempt to reconnect to the server?"), wxString::Format(_("Connection to server %s lost."), server->GetName().c_str()), wxICON_EXCLAMATION|wxYES_NO|wxYES_DEFAULT); - closeIt = (dlg.ShowModal() == wxID_YES); + closeIt = (dlg.ShowModal() == wxID_NO); userInformed = true; } if (closeIt) @@ -806,6 +829,28 @@ execSelChange(serverItem, true); browser->DeleteChildren(serverItem); } + else + { + // Create a server object and connect it. + wxBusyInfo waiting(wxString::Format(_("Reconnecting to server %s (%s:%d)"), + server->GetDescription().c_str(), server->GetName().c_str(), server->GetPort()), this); + + // Give the UI a chance to redraw + wxSafeYield(); + wxMilliSleep(100); + wxSafeYield(); + + if (!server->connection()->Reconnect()) + { + server->Disconnect(this); + browser->SelectItem(serverItem); + execSelChange(serverItem, true); + browser->DeleteChildren(serverItem); + } + else + // Indicate things are back to normal + userInformed = false; + } } } } Index: pgadmin/include/db/pgConn.h =================================================================== --- pgadmin/include/db/pgConn.h (revision 8074) +++ pgadmin/include/db/pgConn.h (working copy) @@ -102,6 +102,7 @@ static double GetLibpqVersion() { return libpqVersion; } void Close(); + bool Reconnect(); bool ExecuteVoid(const wxString& sql, bool reportError = true); wxString ExecuteScalar(const wxString& sql); pgSet *ExecuteSet(const wxString& sql); @@ -110,7 +111,7 @@ wxString GetHost() const { return dbHost; } wxString GetHostName() const { return dbHostName; } wxString GetHostAddress() const { return dbHostAddress; } - wxString GetDbname() const { return dbname; } + wxString GetDbname() const { return save_database; } wxString GetName() const; bool GetNeedUtfConnectString() { return utfConnectString; } int GetPort() const { return atoi(PQport(conn)); }; @@ -151,7 +152,7 @@ wxMBConv *conv; bool needColQuoting, utfConnectString; - wxString dbHost, dbHostName, dbHostAddress, dbname; + wxString dbHost, dbHostName, dbHostAddress; OID lastSystemOID; OID dbOid; @@ -162,6 +163,8 @@ friend class pgQueryThread; private: + bool DoConnect(); + wxString qtString(const wxString& value); bool features[32]; @@ -170,7 +173,8 @@ bool isGreenplum; wxString reservedNamespaces; - + wxString connstr; + wxString save_server, save_database, save_username, save_password; int save_port, save_sslmode; OID save_oid; Index: pgadmin/include/db/pgSet.h =================================================================== --- pgadmin/include/db/pgSet.h (revision 8074) +++ pgadmin/include/db/pgSet.h (working copy) @@ -36,6 +36,7 @@ class pgSet { public: + pgSet(); pgSet(PGresult *newRes, pgConn *newConn, wxMBConv &cnv, bool needColQt); ~pgSet(); long NumRows() const { return nRows; }