Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upMicrosoft.Data.Sqlite: Add way to provide underlying SQLitePCL connection for SqliteConnection #21090
Comments
|
/cc @bricelam |
|
I don’t think SqliteConnection is thread safe (even if the underlying |
|
But yes, going from |
|
That's why it would be great if you could constuct new SqliteConnection from raw SQLite connection. This way SqliteConnection class wouldn't need to be thread safe. |
|
Although a static method like |
|
I'm interested in contributing. If I understand correctly, I should add a static method FromHandle, that can create a SqliteConnection from raw SQLite connection. Could someone give me more details, thank you! |
|
@KaloyanIT Yep. I imagine it would do something like this: var connection = new SqliteConnection();
connection.ConnectionOptions = new SqliteConnectionStringBuilder
{
DataSource = sqlite3_db_filename(handle),
Mode = sqlite3_db_readonly(db, "main") == 0 ? default : SqliteOpenMode.ReadOnly,
// Cache = ???
};
connection._connectionString = ConnectionOptions.ToString();
connection._db = handle;
connection._state = ConnectionState.Open;
return connection;We might also need to avoid closing the handle when the SqliteConnection is disposed... |
|
Hmm, I don't see a way to tell if the database is using a shared cache. We may need to just trust the user if they try to use |
|
@bricelam Single-thread. In this mode, all mutexes are disabled and SQLite is unsafe to use in more than a single thread at once. Multi-thread. In this mode, SQLite can be safely used by multiple threads provided that no single database connection is used simultaneously in two or more threads. Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction. Would it not be a lot better for performance to be able to use sqlite in multithreaded mode for read and write? |
|
@groege When I've thought about this in the past, I was a little concerned about creating the connection on one thread but executing commands on another. (The connection pool #13837 would do this.) I think this is ok so long as it's not used simultaneous on different threads. Can you submit a new issue? Performance is one of the themes for 6.0, and it would be good to investigate whether we can safely pass |
@bricelam Do you mean a general perf issue or about making sqlite multithreaded? (maybe compile it with all options enabled so the dev can decide which mode to use via modelbuilder, etc...) |
|
Filed #22330. |


I was looking at SQLite docs and it seems single connection supports multithreading in serialized mode. From what I read here default mode for included SQLite is serialized. So you could just use single connection for lifetime of your app if you don't care about sharing PRAGMAs etc.
But current SqliteConnection implementation doesn't support providing your own underlying SQLite connection.
Add constructor that allows providing your own SQLitePCL connection.