Browse Source

Merge pull request #40 from srirampatil/threadtests

Fixed a db closed bug and resolved some pool overflow issues
August "Gus" Mueller 14 years ago
parent
commit
e432dbfa7d

+ 7 - 0
fmdb.xcodeproj/project.xcworkspace/contents.xcworkspacedata

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Workspace
+   version = "1.0">
+   <FileRef
+      location = "self:fmdb.xcodeproj">
+   </FileRef>
+</Workspace>

BIN
fmdb.xcodeproj/project.xcworkspace/xcuserdata/sriram.xcuserdatad/UserInterfaceState.xcuserstate


+ 2 - 0
src/FMDatabase.m

@@ -488,6 +488,8 @@ - (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDiction
 - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args {
     
     if (![self databaseExists]) {
+        //Pushing the FMDatabase instance back to the pool if error occurs
+        [self checkPoolPushBack];
         return 0x00;
     }
     

+ 3 - 0
src/FMDatabaseAdditions.m

@@ -112,6 +112,9 @@ - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName {
         }
     }
     
+    //If this is not done FMDatabase instance stays out of pool
+    [rs close];
+    
     return returnBool;
 }
 

+ 15 - 11
src/FMDatabasePool.m

@@ -93,21 +93,25 @@ - (FMDatabase*)db {
             }
             
             db = [FMDatabase databaseWithPath:_path];
-            
-            if ([db open]) {
-                if ([_delegate respondsToSelector:@selector(databasePool:shouldAddDatabaseToPool:)] && ![_delegate databasePool:self shouldAddDatabaseToPool:db]) {
-                    [db close];
-                    db = 0x00;
-                }
-                else {
-                    [_databaseOutPool addObject:db];
-                }
+        }
+        
+        //This ensures that the db is opened before returning
+        if ([db open]) {
+            if ([_delegate respondsToSelector:@selector(databasePool:shouldAddDatabaseToPool:)] && ![_delegate databasePool:self shouldAddDatabaseToPool:db]) {
+                [db close];
+                db = 0x00;
             }
             else {
-                NSLog(@"Could not open up the database at path %@", _path);
-                db = 0x00;
+                //It should not get added in the pool twice if lastObject was found
+                if (![_databaseOutPool containsObject:db]) {
+                    [_databaseOutPool addObject:db];
+                }
             }
         }
+        else {
+            NSLog(@"Could not open up the database at path %@", _path);
+            db = 0x00;
+        }
         
         [db setPool:self];
     }];