Browse Source

Fixed a problem under GC where leaked statments were keeping a database from closing. Patch from Chris Dolan.
Added + (BOOL)isThreadSafe to FMDatabase. It'll let you know if the version of SQLite you are running is compiled with it's thread safe options. THIS DOES NOT MEAN FMDATABASE IS THREAD SAFE. I haven't done a review of it for this case, so I'm just saying.

August Mueller 14 years ago
parent
commit
a545f71d08
5 changed files with 22 additions and 2 deletions
  1. 3 1
      CHANGES_AND_TODO_LIST.txt
  2. 1 0
      CONTRIBUTORS.txt
  3. 1 1
      src/FMDatabase.h
  4. 15 0
      src/FMDatabase.m
  5. 2 0
      src/fmdb.m

+ 3 - 1
CHANGES_AND_TODO_LIST.txt

@@ -5,7 +5,9 @@ If you would like to contribute some code- awesome!  I just ask that you make it
 
 2011.06.22
     Changed some methods to properties.  Hello 2011.
-    Added a warning when you try and use a database that wasn't opened.  Hacked together based on patches from Drarok Ithaqua
+    Added a warning when you try and use a database that wasn't opened.  Hacked together based on patches from Drarok Ithaqua.
+    Fixed a problem under GC where leaked statments were keeping a database from closing.  Patch from Chris Dolan.
+    Added + (BOOL)isThreadSafe to FMDatabase.  It'll let you know if the version of SQLite you are running is compiled with it's thread safe options.  THIS DOES NOT MEAN FMDATABASE IS THREAD SAFE.  I haven't done a review of it for this case, so I'm just saying.
 
 2011.04.09
 	Added a method to validate a SQL statement.

+ 1 - 0
CONTRIBUTORS.txt

@@ -29,5 +29,6 @@ Jeff Meininger
 Pascal Pfiffner
 Dave DeLong
 Drarok Ithaqua
+Chris Dolan
 
 Aaaaannnd, Gus Mueller (that's me!)

+ 1 - 1
src/FMDatabase.h

@@ -78,7 +78,7 @@
 - (BOOL)shouldCacheStatements;
 - (void)setShouldCacheStatements:(BOOL)value;
 
-
++ (BOOL)isThreadSafe;
 + (NSString*)sqliteLibVersion;
 
 - (int)changes;

+ 15 - 0
src/FMDatabase.m

@@ -94,6 +94,8 @@ - (BOOL)close {
     int  rc;
     BOOL retry;
     int numberOfRetries = 0;
+    BOOL triedFinalizingOpenStatements = NO;
+    
     do {
         retry   = NO;
         rc      = sqlite3_close(db);
@@ -105,6 +107,15 @@ - (BOOL)close {
                 NSLog(@"Database busy, unable to close");
                 return NO;
             }
+            
+            if (!triedFinalizingOpenStatements) {
+                triedFinalizingOpenStatements = YES;
+                sqlite3_stmt *pStmt;
+                while ((pStmt = sqlite3_next_stmt(db, 0x00)) !=0) {
+                    NSLog(@"Closing leaked statement");
+                    sqlite3_finalize(pStmt);
+                }
+            }
         }
         else if (SQLITE_OK != rc) {
             NSLog(@"error closing!: %d", rc);
@@ -859,6 +870,10 @@ - (void)setShouldCacheStatements:(BOOL)value {
     }
 }
 
++ (BOOL)isThreadSafe {
+    // make sure to read the sqlite headers on this guy!
+    return sqlite3_threadsafe();
+}
 
 @end
 

+ 2 - 0
src/fmdb.m

@@ -13,6 +13,8 @@ int main (int argc, const char * argv[]) {
     
     FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
     
+    NSLog(@"Is SQLite compiled with it's thread safe options turned on? %@!", [FMDatabase isThreadSafe] ? @"Yes" : @"No");
+    
     {
 		// -------------------------------------------------------------------------------
 		// Un-opened database check.