Skip to content

Commit 7c56b11

Browse files
committed
* Turn -[SPMySQLConnection checkConnectionIfNecessary] into a public method, since it is actually the preferable way to -[SPMySQLConnection checkConnection] (which forces network IO in a new thread each time)
* Change `-[SPDatabaseStructure queryDbStructureWithUserInfo:]` doing a forced connection check inside a tight loop (#2306)
1 parent 6c3ab14 commit 7c56b11

File tree

6 files changed

+37
-35
lines changed

6 files changed

+37
-35
lines changed

Frameworks/SPMySQLFramework/Source/SPMySQL Private APIs.h

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
- (void)_disconnect;
4848
- (void)_updateConnectionVariables;
4949
- (void)_restoreConnectionVariables;
50-
- (BOOL)_checkConnectionIfNecessary;
5150
- (void)_validateThreadSetup;
5251
+ (void)_removeThreadVariables:(NSNotification *)aNotification;
5352

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ - (NSString *)escapeString:(NSString *)theString includingQuotes:(BOOL)includeQu
7979
// Ensure per-thread variables are set up
8080
[self _validateThreadSetup];
8181

82-
if (![self _checkConnectionIfNecessary]) return nil;
82+
if (![self checkConnectionIfNecessary]) return nil;
8383

8484
// Perform a lossy conversion to bytes, using NSData to do the hard work. Preserves
8585
// nul characters correctly.
@@ -259,7 +259,7 @@ - (id)queryString:(NSString *)theQueryString usingEncoding:(NSStringEncoding)the
259259
[self _validateThreadSetup];
260260

261261
// Check the connection if necessary, returning nil if the state couldn't be validated
262-
if (![self _checkConnectionIfNecessary]) return nil;
262+
if (![self checkConnectionIfNecessary]) return nil;
263263

264264
// Determine whether a maximum query size needs to be restored from a previous query
265265
if (queryActionShouldRestoreMaxQuerySize != NSNotFound) {

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ - (SPMySQLResult *)listProcesses
107107
if (state != SPMySQLConnected) return nil;
108108

109109
// Check the connection if appropriate
110-
if (![self _checkConnectionIfNecessary]) return nil;
110+
if (![self checkConnectionIfNecessary]) return nil;
111111

112112
// Lock the connection before using it
113113
[self _lockConnection];
@@ -153,7 +153,7 @@ - (BOOL)killQueryOnThreadID:(unsigned long)theThreadID
153153

154154
- (BOOL)serverShutdown
155155
{
156-
if([self _checkConnectionIfNecessary]) {
156+
if([self checkConnectionIfNecessary]) {
157157
[self _lockConnection];
158158
// Ensure per-thread variables are set up
159159
[self _validateThreadSetup];

Frameworks/SPMySQLFramework/Source/SPMySQLConnection.h

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
- (BOOL)isConnected;
189189
- (BOOL)isConnectedViaSSL;
190190
- (BOOL)checkConnection;
191+
- (BOOL)checkConnectionIfNecessary;
191192
- (double)timeConnected;
192193
- (BOOL)userTriggeredDisconnect;
193194

Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m

+31-29
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ - (BOOL)isConnectedViaSSL
336336
*
337337
* WARNING: This method may return NO if the current thread is cancelled!
338338
* You MUST check the isCancelled flag before using the result!
339+
*
340+
* NOTE: In general -checkConnectionIfNecessary should be used instead!
339341
*/
340342
- (BOOL)checkConnection
341343
{
@@ -374,6 +376,35 @@ - (BOOL)checkConnection
374376
return connectionVerified;
375377
}
376378

379+
/**
380+
* If thirty seconds have passed since the last time the connection was
381+
* used, check the connection.
382+
* This minimises the impact of continuous additional connection checks -
383+
* each of which requires a round trip to the server - but handles most
384+
* network issues.
385+
* Returns whether the connection is considered still valid.
386+
*
387+
* WARNING: This method may return NO if the current thread is cancelled!
388+
* You MUST check the isCancelled flag before using the result!
389+
*/
390+
- (BOOL)checkConnectionIfNecessary
391+
{
392+
393+
// If the connection has been dropped in the background, trigger a
394+
// reconnect and return the success state here
395+
if (state == SPMySQLConnectionLostInBackground) {
396+
return [self _reconnectAllowingRetries:YES];
397+
}
398+
399+
// If the connection was recently used, return success
400+
if (_elapsedSecondsSinceAbsoluteTime(lastConnectionUsedTime) < 30) {
401+
return YES;
402+
}
403+
404+
// Otherwise check the connection
405+
return [self checkConnection];
406+
}
407+
377408
/**
378409
* Retrieve the time elapsed since the connection was established, in seconds.
379410
* This time is retrieved in a monotonically increasing fashion and is high
@@ -1042,35 +1073,6 @@ - (void)_restoreConnectionVariables
10421073
[self setEncodingUsesLatin1Transport:encodingUsesLatin1Transport];
10431074
}
10441075

1045-
/**
1046-
* If thirty seconds have passed since the last time the connection was
1047-
* used, check the connection.
1048-
* This minimises the impact of continuous additional connection checks -
1049-
* each of which requires a round trip to the server - but handles most
1050-
* network issues.
1051-
* Returns whether the connection is considered still valid.
1052-
*
1053-
* WARNING: This method may return NO if the current thread is cancelled!
1054-
* You MUST check the isCancelled flag before using the result!
1055-
*/
1056-
- (BOOL)_checkConnectionIfNecessary
1057-
{
1058-
1059-
// If the connection has been dropped in the background, trigger a
1060-
// reconnect and return the success state here
1061-
if (state == SPMySQLConnectionLostInBackground) {
1062-
return [self _reconnectAllowingRetries:YES];
1063-
}
1064-
1065-
// If the connection was recently used, return success
1066-
if (_elapsedSecondsSinceAbsoluteTime(lastConnectionUsedTime) < 30) {
1067-
return YES;
1068-
}
1069-
1070-
// Otherwise check the connection
1071-
return [self checkConnection];
1072-
}
1073-
10741076
/**
10751077
* Ensure that the thread this method is called on has been registered for
10761078
* use with MySQL. MySQL requires thread-specific variables for safe

Source/SPDatabaseStructure.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ - (BOOL)_ensureConnectionUnsafe
551551
if (!mySQLConnection || !delegate) return NO;
552552

553553
// Check the connection state
554-
if ([mySQLConnection isConnected] && [mySQLConnection checkConnection]) return YES;
554+
if ([mySQLConnection isConnected] && [mySQLConnection checkConnectionIfNecessary]) return YES;
555555

556556
// the result of checkConnection may be meaningless if the thread was cancelled during execution. (issue #2353)
557557
if([[NSThread currentThread] isCancelled]) return NO;

0 commit comments

Comments
 (0)