-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Make each SQLiteConnection
have its own SQLite handle
#50
Conversation
This new version is causing errors in migrations. The error is
I traced it to this error
I suspect this has to do with this part of the code Changing the last line to the following makes the error disappear. But I doubt if it is the right solution.
|
Interestingly, this error doesn't occur in the first version where a |
This bug is now fixed with the latest commit. It turns out that if the event loop in which |
In-memory databases can now be shared by several connections. This is achieved by keeping a handle of the in-memory database in the |
@MrLotU Please review the latest additions. Thanks! |
@MrLotU Could you please review the PR? Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks great, thanks! I wish I would have known about the file:
prefix before. Just a couple of small comments here. I've also fixed CI to accept forks, so on the next push Circle CI should test this patch.
var handle: OpaquePointer? | ||
guard sqlite3_open_v2(self.storage.path, &handle, options, nil) == SQLITE_OK, let c = handle else { | ||
throw SQLiteError(problem: .error, reason: "Could not open database.", source: .capture()) | ||
let options = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question, do we need SQLITE_OPEN_FULLMUTEX
anymore? SQLiteConnection
should only be used on the thread it was created on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is probably not necessary, but I would recommend playing safe and keeping it there...
private let database: SQLiteDatabase | ||
|
||
/// Internal SQLite database handle. | ||
internal var handle: OpaquePointer! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tanner0101 What do you think about using an IUO for storing the handle. This way isClosed
simply becomes a computed property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use a regular optional instead? I'd prefer to force unwrap the optional as needed over having that behavior be implicit. I think the C calls might accept normal optionals anyway, though I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried. The C calls do accept normal optionals, so the compiler won't tell you to force unwrap it.
Looks like this patch isn't building on Linux.
Let me know if you need any help debugging this. |
Thanks a ton for this patch @dpgao! I'm going to do a minor bump on this one since it technically breaks public API by making |
Hey @dpgao, you just merged a pull request, have a coin! You now have 1 coins. |
Currently all
SQLiteConnection
s share a single underlying SQLite connection stored in anSQLiteDatabase
object. This can cause database queries from different threads to be serialised and mixed together, resulting in potential data corruption.This PR makes each
SQLiteConnection
have its own SQLite connection handle so that queries from different threads are run on different SQLite connections.