Parcourir la source

Just a little refactoring for the busy timeout stuff.

ccgus il y a 12 ans
Parent
commit
cc93329648
5 fichiers modifiés avec 12 ajouts et 6 suppressions
  1. 2 0
      src/FMDatabase.h
  2. 4 4
      src/FMDatabase.m
  3. 1 1
      src/FMDatabaseAdditions.m
  4. 1 1
      src/FMResultSet.m
  5. 4 0
      src/fmdb.m

+ 2 - 0
src/FMDatabase.h

@@ -49,6 +49,8 @@
     #define instancetype id
 #endif
 
+#define FMDatabaseSQLiteBusyMicrosecondsTimeout 20
+
 /** A SQLite ([http://sqlite.org/](http://sqlite.org/)) Objective-C wrapper.
  
  ### Usage

+ 4 - 4
src/FMDatabase.m

@@ -134,7 +134,7 @@ - (BOOL)close {
         if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
             
             retry = YES;
-            usleep(20);
+            usleep(FMDatabaseSQLiteBusyMicrosecondsTimeout);
             
             if (_busyRetryTimeout && (numberOfRetries++ > _busyRetryTimeout)) {
                 NSLog(@"%s:%d", __FUNCTION__, __LINE__);
@@ -615,7 +615,7 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr
             
             if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
                 retry = YES;
-                usleep(20);
+                usleep(FMDatabaseSQLiteBusyMicrosecondsTimeout);
                 
                 if (_busyRetryTimeout && (numberOfRetries++ > _busyRetryTimeout)) {
                     NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);
@@ -807,7 +807,7 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra
             rc      = sqlite3_prepare_v2(_db, [sql UTF8String], -1, &pStmt, 0);
             if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
                 retry = YES;
-                usleep(20);
+                usleep(FMDatabaseSQLiteBusyMicrosecondsTimeout);
                 
                 if (_busyRetryTimeout && (numberOfRetries++ > _busyRetryTimeout)) {
                     NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);
@@ -930,7 +930,7 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra
                     NSLog(@"Unexpected result from sqlite3_reset (%d) eu", rc);
                 }
             }
-            usleep(20);
+            usleep(FMDatabaseSQLiteBusyMicrosecondsTimeout);
             
             if (_busyRetryTimeout && (numberOfRetries++ > _busyRetryTimeout)) {
                 NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);

+ 1 - 1
src/FMDatabaseAdditions.m

@@ -191,7 +191,7 @@ - (BOOL)validateSQL:(NSString*)sql error:(NSError**)error {
         int rc = sqlite3_prepare_v2(_db, [sql UTF8String], -1, &pStmt, 0);
         if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
             keepTrying = YES;
-            usleep(20);
+            usleep(FMDatabaseSQLiteBusyMicrosecondsTimeout);
             
             if (_busyRetryTimeout && (numberOfRetries++ > _busyRetryTimeout)) {
                 NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);

+ 1 - 1
src/FMResultSet.m

@@ -167,7 +167,7 @@ - (BOOL)next {
                     NSLog(@"Unexpected result from sqlite3_reset (%d) rs", rc);
                 }
             }
-            usleep(20);
+            usleep(FMDatabaseSQLiteBusyMicrosecondsTimeout);
             
             if ([_parentDB busyRetryTimeout] && (numberOfRetries++ > [_parentDB busyRetryTimeout])) {
                 

+ 4 - 0
src/fmdb.m

@@ -353,6 +353,8 @@ int main (int argc, const char * argv[]) {
     
     NSLog(@"Testing the busy timeout");
     
+    NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
+    
     BOOL success = [db executeUpdate:@"insert into t1 values (5)"];
     
     if (success) {
@@ -375,7 +377,9 @@ int main (int argc, const char * argv[]) {
         NSLog(@"Hurray, we can insert again!");
     }
     
+    NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate] - startTime;
     
+    NSLog(@"Took %f seconds for the timeout.", end);
     
     // test some nullness.
     [db executeUpdate:@"create table t2 (a integer, b integer)"];