             return nil
         }
 
-        let status = immediately ? sqlite3_close(db) : sqlite3_close_v2(db)
-
-        // Note that if we use sqlite3_close_v2, this will still return SQLITE_OK even if
-        // there are outstanding prepared statements
+        var status = sqlite3_close(db)
+
         if status != SQLITE_OK {
             log.error("Got status \(status) while attempting to close.")
-            return createErr("During: closing database with flags", status: Int(status))
+            SentryIntegration.shared.sendWithStacktrace(message: "Got status \(status) while attempting to close.", tag: "SwiftData", severity: .error)
+            
+            if immediately {
+                return createErr("During: closing database with flags", status: Int(status))
+            }
+            
+            // Note that if we use sqlite3_close_v2, this will still return SQLITE_OK even if
+            // there are outstanding prepared statements
+            status = sqlite3_close_v2(db)
+            
+            if status != SQLITE_OK {
+                
+                // Based on the above comment regarding sqlite3_close_v2, this shouldn't happen.
+                log.error("Got status \(status) while attempting to close_v2.")
+                SentryIntegration.shared.sendWithStacktrace(message: "Got status \(status) while attempting to close_v2.", tag: "SwiftData", severity: .error)
+                return createErr("During: closing database with flags", status: Int(status))
+            }
         }
 
         log.debug("Closed \(self.filename).")

        } catch let error1 as NSError {
            error = error1
            statement = nil
            log.error("SQL error: \(error1.localizedDescription) for SQL \(sqlStr).")
        }

        // Close, not reset -- this isn't going to be reused.

            // Special case: Write additional info to the database log in the case of a database corruption.
            if error.code == Int(SQLITE_CORRUPT) {
                writeCorruptionInfoForDBNamed(filename, toLogger: Logger.corruptLogger)
                
                SentryIntegration.shared.sendWithStacktrace(message: "SQLITE_CORRUPT for DB \(filename), \(error)", tag: "SwiftData", severity: .error)
            }

            log.error("SQL error. Error code: \(error.code), \(error) for SQL \(sqlStr).")
            SentryIntegration.shared.sendWithStacktrace(message: "SQL error. Error code: \(error.code), \(error)", tag: "SwiftData", severity: .error)

            return Cursor<T>(err: error)
        }

