Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "destination database is in use " error during database backup #425

Merged
merged 5 commits into from
Oct 4, 2018

Conversation

groue
Copy link
Owner

@groue groue commented Oct 3, 2018

EDIT 05/10/2018: The analysis of this PR is wrong. See #427 for the "real" fix.

This PR addresses #424.

The DatabaseWriter.erase() method internally uses the SQLite Online Backup API:

extension DatabaseWriter {
    func erase() throws {
        // In order to erase the content, we backup
        // an empty in-memory database into self:
        DatabaseQueue().backup(to: self)
    }
}

This technique used to fail with an error of code 1 (SQLITE_ERROR), message "destination database is in use" when live SQLite prepared statement would prevent the backup from starting (see the regression tests for the precise failure setup).

It happens that Record methods keep prepared statements in a cache, in order to optimize insertions, updates, deletions, and tests for record existence.

This PR thus clears the statement cache before starting a backup, when we used to do it after a successful backup.

The error "destination database is in use" can still happen during a backup, if the client application keeps its own prepared statements alive. To fix this error, the client application must release all prepared statements before starting the backup.

@groue groue force-pushed the feature/robust-backup branch from eb03706 to 7a628bf Compare October 4, 2018 06:35
@groue
Copy link
Owner Author

groue commented Oct 4, 2018

The fix is ready, @bellebethcooper 👍

I want to finish #423 first, though: you will have to keep the workaround we discussed in #424 for a little while.

@bellebethcooper
Copy link
Collaborator

No stress, the workaround is just fine. Thanks! 👍

@groue groue merged commit dd4b651 into development Oct 4, 2018
@groue groue deleted the feature/robust-backup branch October 4, 2018 13:59
@groue groue added this to the GRDB 3.4.0 milestone Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants