[QUESTION] How to correctly set a shared connection?
Hi all, I am on v5.0.1 and I don't understand how to set the connection as shared. I have tried with both Mode=Shared and Connection=Shared but if I open the database in LiteDB Studio and then in my app or the opposite, I receive an exception stating that the file is already opened in another process.
Can you tell me what is the correct way of setting the connection as shared? Thanks in advance
@darcome Here's a working example of how to run LiteDB in Shared connection mode
class Program
{
static async Task Main(string[] args)
{
await Task.WhenAll(
Action(),
Action());
}
private static async Task Action()
{
using var db = new LiteDatabase(@"Filename=database.db;Password='1234';connection=shared");
await Task.Delay(1000);
}
}
If you prefer using a connectionString object instead of a literal string, you can also replace the string with something like this
new ConnectionString(@"database.db")
{
Password = "1234",
Connection = ConnectionType.Shared
};
@JensSchadron THank you very much! It worked like a charm!
I see that I cannot use shared mode when I also specify Upgrade=true?
I cannot get it to work using two separate instances of LiteDB.Studio 'Check Shared' nothing works. (v. 5.0.11)
@TerenceWallace Are you getting any specific error? It should work if both instances are opened in shared mode.
@TerenceWallace Are you getting any specific error? It should work if both instances are opened in shared mode.
Yes, I am getting "The process cannot access the file "....{filename.db}" because it is being used by another process. In both instances (i.e. [Instance 1] running VS in debug mode; [Instance 2] running by double clicking the LiteDB.Studio.exe) the "Shared" box is checked.
I can send the full solution file to view it for yourself.
@TerenceWallace Do you get this error for both instances? Or does the first instance open the file successfully and the second one fails?
Only the second instance fails. The first instance works fine without any issues.
@TerenceWallace This is weird. You can send the solution and the datafile to me at [email protected]
I have emailed you the solution. Thanks for looking into it any feedback is appreciated.
@TerenceWallace The project you sent me uses a very old version of LiteDB.Studio. It should work fine in the latest version.
@lbnascimento is v5.0.11 the very old version? The files come directly from that version. Only, the files are compiled using the .NET 4.6 Framework.
Unfortunately, we are not a Web 2.0 company and do not rush to the latest 'bells & whistles' of MS Framework(s).
IF your saying that the latest (.NET, CORE 3.**, etc.) is a requirement for LiteDB then I can understand that this will not work for us.
@TerenceWallace v5.0.11 is the latest version of LiteDB. You're running a very old version of LiteDB.Studio, which apparently has a bug when opening datafiles in shared mode.
Confirmed. It works perfectly now. Thank you.