From eda4399be690cb325bb03d3195654e38ed061738 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Sat, 18 Aug 2012 14:18:29 +0000 Subject: Improve connection handling and recovery in general, and specifically to address Issue #877: - On servers with very short timeouts set the wait_timeout for the session as well as the interactive_timeout to prevent the connection from dropping frequently - Improve recovery from connection errors, correctly restoring the connection if appropriate and possible - Allow reconnections to occur recursively by altering the internal tracking mechanism - Fix some edge cases where the connection would remain locked incorrectly - Improve error messaging for the "MySQL Server has gone away" network case --- .../Querying & Preparation.m | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories') diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m index 3b2614f1..9280c156 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m @@ -217,6 +217,8 @@ - (id)queryString:(NSString *)theQueryString usingEncoding:(NSStringEncoding)theEncoding withResultType:(SPMySQLResultType)theReturnType { double queryExecutionTime; + NSString *theErrorMessage; + NSUInteger theErrorID; lastQueryWasCancelled = NO; lastQueryWasCancelledUsingReconnect = NO; @@ -282,11 +284,17 @@ // If the query succeeded, no need to re-attempt. if (!queryStatus) { + theErrorMessage = nil; + theErrorID = 0; break; // If the query failed, determine whether to reattempt the query } else { + // Store the error state + theErrorMessage = [self _stringForCString:mysql_error(mySQLConnection)]; + theErrorID = mysql_errno(mySQLConnection); + // Prevent retries if the query was cancelled or not a connection error if (lastQueryWasCancelled || ![SPMySQLConnection isErrorIDConnectionError:mysql_errno(mySQLConnection)]) { break; @@ -294,10 +302,11 @@ } // Query has failed - check the connection + [self _unlockConnection]; if (![self checkConnection]) { - [self _unlockConnection]; - return nil; + break; } + [self _lockConnection]; queryAttemptsAllowed--; } @@ -330,11 +339,11 @@ theResult = [[SPMySQLFastStreamingResult alloc] initWithMySQLResult:mysqlResult stringEncoding:theEncoding connection:self]; break; } - } - // Record the error state now, as it may be affected by subsequent clean-up queries - NSString *theErrorMessage = [self _stringForCString:mysql_error(mySQLConnection)]; - NSUInteger theErrorID = mysql_errno(mySQLConnection); + // Update the error message, if appropriate, to reflect result store errors or overall success + theErrorMessage = [self _stringForCString:mysql_error(mySQLConnection)]; + theErrorID = mysql_errno(mySQLConnection); + } // Update the connection's stored insert ID if available if (mySQLConnection->insert_id) { @@ -350,6 +359,7 @@ // after query kills. This is also handled within the class for internal cancellations, but // as other external classes may also cancel the query. if (![self serverVersionIsGreaterThanOrEqualTo:5 minorVersion:0 releaseVersion:0]) { + [self _unlockConnection]; [self checkConnection]; } } -- cgit v1.2.3