You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Microsoft.Data.Sqlite: Persist functions, PRAGMAs, etc. between close and re-open #13826
There are quite a few things in SQLite that need to be configured every time a connection is opened. (E.g. certain PRAGMAs, user-defined functions, collations, etc.)
The idea would be that you pass some configuration into your SqliteConnection instances, and every time the connection is opened, it would be configured appropriately.
For the PRAGMAs we could take the same route as System.Data.SQLite does it: use certain keywords in the ConnectionString. Looking at the ones from System.Data.SQLite one could go with:
Foreign Keys: True, False
Journal Mode: Delete, Truncate, Persist, Memory, Wal, Off
Recursive Triggers: True, False
Synchronous: Off, Normal, Full, Extra
Additional PRAGMA settings of interest could be:
Auto Vacuum: None, Full, Incremental
Automatic Index: True, False
For collations or user defined functions we would need an additional configuration object, but the information storage in this object will be tricky. One could add the same functions as defined in #14 and #19 inside this SqliteConnectionConfiguration class and use them to store the given information in lists which would be parsed later in the Open call of the collation.
I'm not a big fan of the things System.Data.SQlite added to the connection string. In my mind, things in the connection string should be things you can't change after the connection is opened.
You ideas about SqliteConnectionConfiguration match mine.
If SQLite doesn't have a default (or it can be changed with a compile flag), we should probably make the properties nullable. null would mean "don't send a pragma".
We should also look at alternatives to this feature. Using the SqliteConnection.StateChanged event isn't too bad.
Maybe adding properties directly to SqliteConnection would be better along with making calls to AddFunction, etc. re-apply themselves whenever the connection is re-opened.
In my opinion it would be good to have a seperate object which stores the configuration, so that one can initialize different connections with the same configuration.
Regarding mutation after initialization, the question is if this is really necessary. At the moment I see no real use case for this. In order to prevent the mutation we could also package the properties and methods proposed above in some kind of SqliteConnectionConfigurationCreator class and define the SqliteConnectionConfiguration as completely immutable. In order to create the configuration one can add a SqliteConnectionConfigurationCreator.CreateConfiguration() method and/or an constructor SqliteConnectionConfiguration(SqliteConnectionConfigurationCreator creator).
Why not support all CS keyword from System.Data.SQLite?
Support Foreign Keys=True (especially if it's not default) is a must.
Adding corresponding properties (get/set) to connection object is needed as well. Boolean SqliteConnection.ForeignKeys {get;set;}
Could you tell me how can I enable Foreign Keys currently on 2.0?
My latest thinking on this is that we should probably add a connection pool in version 3.0, and when we do that, we can make the calls to CreateFunction(), etc. persistent.
I am missing the ability to add the items in the connection string that allow you to fully initialize access to a particular database. I do not know of any other driver where you cannot access the database after configuring a proper connection string.
In Microsoft.Data.Sqlite one has to execute a number of PRAGMAs before the connection is actually valid. This breaks many tools such as RoundhousE (http://projectroundhouse.org) which takes a connection string and a set of sql scripts and migrates the database. There is no provision in these tools to allow commands to be run every time a connection is made. For example, in System.Data.Sqlite one can set the Password, Pooling, and many other things that fully set up the connection to the database so that it is ready for action.
It seems that the idea of a connection string is to fully initialize a connection so that work can be performed on the database. Having to execute PRAGMA statements complicates initialization and prevents existing tools from working.
Also I believe from a pooling implementation perspective in other databases, that connections are pooled based on matching connection strings. If the connect string doesn't fully describe the settings of the connection will it not be difficult to determine if a connection can be reused in a particular context?
Since setting the Journal Mode to Write-Ahead Logging (WAL) is persisted, I decided it would be better for EF just to set it for databases it creates. (See #14059) No need for a connection string keyword yet since this probably covers most scenarios where you want to change it from the default.
We are running on an ARM/iMX6 SBC with Linux/debian where inserts is very slow (2-3 sec). We were hoping to get better performance by doing some optimization on SQLite.
Have you seen this post? It's a good place to start. Issue #14044 will make perf even better with higher-level libraries like EF. Also if you're using encryption, make sure you're not closing a re-openeing the connection a lot (issue #13837 will help this)--one open connection per thread is a good strategy.
Activity
AlexanderTaeschner commentedon Apr 12, 2017
For the PRAGMAs we could take the same route as System.Data.SQLite does it: use certain keywords in the ConnectionString. Looking at the ones from System.Data.SQLite one could go with:
Additional PRAGMA settings of interest could be:
For collations or user defined functions we would need an additional configuration object, but the information storage in this object will be tricky. One could add the same functions as defined in #14 and #19 inside this
SqliteConnectionConfigurationclass and use them to store the given information in lists which would be parsed later in the Open call of the collation.bricelam commentedon Apr 12, 2017
I'm not a big fan of the things System.Data.SQlite added to the connection string. In my mind, things in the connection string should be things you can't change after the connection is opened.
You ideas about
SqliteConnectionConfigurationmatch mine.AlexanderTaeschner commentedon Apr 12, 2017
Okay, then the keywords mentioned above would be properties of the
SqliteConnectionConfiguration.Proposal would than be:
bricelam commentedon Apr 12, 2017
If SQLite doesn't have a default (or it can be changed with a compile flag), we should probably make the properties nullable.
nullwould mean "don't send a pragma".bricelam commentedon Apr 12, 2017
We also need to define what happens when you mutate the configuration after a connection is open. Options:
bricelam commentedon Apr 12, 2017
We should also look at alternatives to this feature. Using the
SqliteConnection.StateChangedevent isn't too bad.Maybe adding properties directly to
SqliteConnectionwould be better along with making calls toAddFunction, etc. re-apply themselves whenever the connection is re-opened.AlexanderTaeschner commentedon Apr 25, 2017
In my opinion it would be good to have a seperate object which stores the configuration, so that one can initialize different connections with the same configuration.
Regarding mutation after initialization, the question is if this is really necessary. At the moment I see no real use case for this. In order to prevent the mutation we could also package the properties and methods proposed above in some kind of
SqliteConnectionConfigurationCreatorclass and define theSqliteConnectionConfigurationas completely immutable. In order to create the configuration one can add aSqliteConnectionConfigurationCreator.CreateConfiguration()method and/or an constructorSqliteConnectionConfiguration(SqliteConnectionConfigurationCreator creator).evil-shrike commentedon Aug 23, 2017
Why not support all CS keyword from System.Data.SQLite?
Support
Foreign Keys=True(especially if it's not default) is a must.Adding corresponding properties (get/set) to connection object is needed as well.
Boolean SqliteConnection.ForeignKeys {get;set;}Could you tell me how can I enable Foreign Keys currently on 2.0?
AlexanderTaeschner commentedon Aug 23, 2017
@evil-shrike: You can enable foreign keys using the sqlite PRAGMA commands (https://www.sqlite.org/pragma.html):
bricelam commentedon Apr 26, 2018
My latest thinking on this is that we should probably add a connection pool in version 3.0, and when we do that, we can make the calls to
CreateFunction(), etc. persistent.jonreis commentedon Jul 19, 2018
I am missing the ability to add the items in the connection string that allow you to fully initialize access to a particular database. I do not know of any other driver where you cannot access the database after configuring a proper connection string.
In Microsoft.Data.Sqlite one has to execute a number of PRAGMAs before the connection is actually valid. This breaks many tools such as RoundhousE (http://projectroundhouse.org) which takes a connection string and a set of sql scripts and migrates the database. There is no provision in these tools to allow commands to be run every time a connection is made. For example, in System.Data.Sqlite one can set the Password, Pooling, and many other things that fully set up the connection to the database so that it is ready for action.
It seems that the idea of a connection string is to fully initialize a connection so that work can be performed on the database. Having to execute PRAGMA statements complicates initialization and prevents existing tools from working.
Also I believe from a pooling implementation perspective in other databases, that connections are pooled based on matching connection strings. If the connect string doesn't fully describe the settings of the connection will it not be difficult to determine if a connection can be reused in a particular context?
bricelam commentedon Jul 20, 2018
@jonreis Can you provide specific examples of what breaks in the current implementation?
bricelam commentedon Jul 20, 2018
See #401 for why we didn't add
Password.[-]Connection configuration[/-][+]Connection configuration (persist between close and re-open)[/+]16 remaining items
Microsoft.Data.Sqlite: Add LoadExtension() to SqliteConnection
bricelam commentedon Feb 28, 2019
I forgot to mention this here:
Since setting the Journal Mode to Write-Ahead Logging (WAL) is persisted, I decided it would be better for EF just to set it for databases it creates. (See #14059) No need for a connection string keyword yet since this probably covers most scenarios where you want to change it from the default.
vhking commentedon Apr 24, 2019
@bricelam Is it possible to set Synchronous in the connection string in the new .net 3.0.0 preview 4? If so, how would you do that?
bricelam commentedon Apr 24, 2019
No, we haven't added that one. What is your scenario? Maybe what you really want is for e_sqlite3 to define SQLITE_DEFAULT_WAL_SYNCHRONOUS=1?
vhking commentedon Apr 25, 2019
We are running on an ARM/iMX6 SBC with Linux/debian where inserts is very slow (2-3 sec). We were hoping to get better performance by doing some optimization on SQLite.
bricelam commentedon Apr 25, 2019
Have you seen this post? It's a good place to start. Issue #14044 will make perf even better with higher-level libraries like EF. Also if you're using encryption, make sure you're not closing a re-openeing the connection a lot (issue #13837 will help this)--one open connection per thread is a good strategy.
darkguy2008 commentedon Aug 18, 2020
So is this done? I'm getting
Keyword not supported: "synchronous"when trying to set this PRAGMA. Any ideas?bricelam commentedon Aug 18, 2020
@darkguy2008 Can you submit a new issue and clarify what you are doing when you get this error?