Litedb: [QUESTION] How to correctly set a shared connection?

Created on 8 Feb 2020  路  3Comments  路  Source: mbdavid/LiteDB

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

question

Most helpful comment

@JensSchadron THank you very much! It worked like a charm!

All 3 comments

@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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghiboz picture ghiboz  路  4Comments

sandolkakos picture sandolkakos  路  3Comments

RealBlazeIt picture RealBlazeIt  路  3Comments

MoamenMohamed picture MoamenMohamed  路  4Comments

darcome picture darcome  路  3Comments