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
I'm using BLToolkit 4.2 together with System.Data.SQLite 1.0.93. I've noticed that sometimes DB file remains locked after running complex queries. After some debugging I've found that an SQLiteConnection remains open. Further debugging showed that:
When BLToolkit executes a subquery, it clones the root DataContextInfo (see QueryContext.GetDataContext()) which in turn clones DbManager.
DbManager clones connection via DataProvider (see DataProviderBase.CloneConnection())
System.Data.SQLite's SQLiteConnection.Clone() creates a new connection and opens it if the original connection is open:
Nested DbManager's _closeConnection flag is never set because connection is already open and OpenConnection() is not executed.
When the root DbManager is disposed it correctly disposes its own connection and the nested DbManager, but the cloned connection is not closed/disposed as _closeConnection == false (see DbManager.Close()).
Consequently, new connection was created in DbManager, but was not disposed in it.
The following small sample demonstrates this behavior:
Program.cs
usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Reflection;usingBLToolkit.Data;usingBLToolkit.Data.DataProvider;namespaceBLToolkitSQLiteTest{classProgram{staticvoidMain(string[]args){// ConfigurationvardataProvider=newSQLiteDataProvider();DbManager.AddDataProvider(dataProvider);// Clean up and init DBif(File.Exists("test.db"))File.Delete("test.db");using(vardb=newDbManager(dataProvider,GetConnectionString("test.db"))){using(varstream=Assembly.GetExecutingAssembly().GetManifestResourceStream("BLToolkitSQLiteTest.init.sql"))using(varreader=newStreamReader(stream)){varquery=reader.ReadToEnd();db.SetCommand(query).ExecuteNonQuery();}}using(vardb=newDbManager(dataProvider,GetConnectionString("test.db"))){// Execute complex queryvartable1=db.GetTable<Asset_Type>();vartable2=db.GetTable<Asset_Type>();varresult=table1.GroupJoin(table2, t1 =>t1.Type_Id, t2 =>t2.Type_Id,(t1,t2)=>t1.Type_Name+"; "+String.Join(", ",t2.Select(_ =>_.Type_Name))).ToList();}try{// Check if file is lockedFile.Delete("test.db");Console.WriteLine("OK");}catch(Exceptione){Console.WriteLine("ERROR");Console.WriteLine(e);}Console.ReadKey();}protectedstaticstringGetConnectionString(stringpath){return"Data Source="+path+";Version=3;Compress=True;foreign keys=true;";}}publicclassAsset_Type{publicintType_Id{get;set;}publicstringType_Name{get;set;}}}
This helped with my issue but I am not sure if it may affect other scenarios/data provider types, and I'm not able to run unit test properly as I don't have proper environment with different databases configured. Please advise if this is an acceptable solution.
The text was updated successfully, but these errors were encountered:
I'm using BLToolkit 4.2 together with System.Data.SQLite 1.0.93. I've noticed that sometimes DB file remains locked after running complex queries. After some debugging I've found that an SQLiteConnection remains open. Further debugging showed that:
Consequently, new connection was created in DbManager, but was not disposed in it.
The following small sample demonstrates this behavior:
Program.cs
init.sql
My workaround was to check if connection is already open after cloning and set DbManager._closeConnection flag if it is:
This helped with my issue but I am not sure if it may affect other scenarios/data provider types, and I'm not able to run unit test properly as I don't have proper environment with different databases configured. Please advise if this is an acceptable solution.
The text was updated successfully, but these errors were encountered: