From 8939b86ba487c9e361d9a20803ee626b45fc4ae4 Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Tue, 8 Sep 2015 08:12:18 +0000 Subject: [PATCH] Improve handling of server failure git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/sqlclient/trunk@38974 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++++ Postgres.m | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 44246a7..8d924bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-07-22 Richard Frith-Macdonald + + * Postgres.m: disconnect on fatal error, so we don't keep trying to + re-use the same connection when there's a problem with the server. + 2015-07-23 Wolfgang Lux * SQLClient.m (initialize): Restore initialization of NSDateClass diff --git a/Postgres.m b/Postgres.m index 1d036b7..f4129d6 100644 --- a/Postgres.m +++ b/Postgres.m @@ -430,18 +430,36 @@ connectQuote(NSString *str) p = PQparameterStatus(connection, "standard_conforming_strings"); if (p != 0) { + PGresult *result; + /* If the escape_string_warning setting is on, * the server will warn about backslashes even * in properly quoted strings, so turn it off. */ if (strcmp(p, "on") == 0) { - [self execute: @"SET escape_string_warning=off", nil]; + result = PQexec(connection, + "SET escape_string_warning=off"); } else { - [self execute: @"SET standard_conforming_strings=on;" - @"SET escape_string_warning=off", nil]; + result = PQexec(connection, + "SET standard_conforming_strings=on;" + "SET escape_string_warning=off"); + } + if (0 == result + || PQresultStatus(result) != PGRES_COMMAND_OK) + { + [self debug: @"Error setting string handling" + @" with '%@' (%@) - %s", + [self name], m, PQerrorMessage(connection)]; + if (result != 0) + { + PQclear(result); + } + PQfinish(connection); + connection = 0; + connected = NO; } } else @@ -454,7 +472,14 @@ connectQuote(NSString *str) if ([self debugging] > 0) { - [self debug: @"Connected to '%@'", [self name]]; + if (YES == connected) + { + [self debug: @"Connected to '%@'", [self name]]; + } + else + { + [self debug: @"Disconnected '%@'", [self name]]; + } } } } @@ -634,6 +659,7 @@ connectQuote(NSString *str) { str = [NSString stringWithCString: cstr]; } + [self backendDisconnect]; [NSException raise: SQLException format: @"Error executing %@: %@", stmt, str]; } @@ -989,6 +1015,7 @@ static inline unsigned int trim(char *str, unsigned len) { str = [NSString stringWithCString: cstr]; } + [self backendDisconnect]; [NSException raise: SQLException format: @"Error executing %@: %@", stmt, str]; }