Search code examples
c#databaseormpetapoco

PetaPoco - Transaction isolation level on insert to DB


Is there a way to set isolation level on Insert to DB using PetaPoco!

Transaction locks the database while its not completed, I know there are ways to edit the actual query's or SP's but is there a way to control isolation level via petapoco? It seems as there was an option in earlier version, but cant find anything in latest versions.

My current example code:

using (var scope = contextasync.getInstance().GetTransaction())
{
    try {
        object userId = context.Insert(user);
        example.UserId = user.Id;
        object affected2 = context.Insert(example); }catch(Exception exc)
    {
        //log the error

    }
scope.Complete();

}

Checked this, but it did not help: PetaPoco - setting transaction isolation level


Solution

  • With the latest version of PetaPoco, you can now set the isolation level.

    Using fluent configuration

    var db = config.Build()
             .UsingConnectionString("cs")
             .UsingProvider<SqlServerDatabaseProvider>()
             .UsingIsolationLevel(IsolationLevel.Chaos)
             .Create();
    
     db.IsolationLevel.ShouldBe(IsolationLevel.Chaos);
    

    Or traditional constructor

    var db = new Database("MyConnectionStringName") { IsolationLevel = IsolationLevel.Chaos };