PetaPoco Transaction in Multithread Env

∥☆過路亽.° 提交于 2019-12-01 02:25:50

问题


I just test PetaPoco Transaction in a multithread way...

I have a simple test case :

-- Simple value object call it MediaDevice -- Insert a record an update it for 1000 times

void TransactionThread(Object object)
{


    Database db = (Database) object;

    for(int i= 0; i < 1000;i++)
    {


        Transaction transaction = db.GetTransaction();

        MediaDevice device = new  MediaDevice();
        device.Name = "Name";
        device.Brand = "Brand";

        db.Insert(device);

        device.Name = "Name_Updated";
        device.Brand = "Brand_Updated";


        db.Update(device);

        transaction.Complete();

    }


    long count = db.ExecuteScalar<long>("SELECT Count(*) FROM MediaDevices");

    Console.WriteLine("Number of all records:" + count);

}

And I call this in two threads like this:[ Single Database object for both threads]

void TransactionTest()
{

    Database db =  GetDatabase();

    Thread tThread1 = ... // thread for  TransactionTest()

    Thread tThread2 = ... // thread for  TransactionTest()

     tThread1.Start(db); // pass Database to TransactionTest()
     tThread2.Start(db); // pass same Database to TransactionTest()

}

I get Null error or sometimes Object disposed error for Database..

But when i supply two Database instance,

void TransactionTest()
{

    Database db =  GetDatabase();
    Database db2 =  GetDatabase();

    Thread tThread1 = ... // thread for  TransactionTest()

    Thread tThread2 = ... // thread for  TransactionTest()


    tThread1.Start(db);  // pass Database instance db to TransactionTest()
    tThread2.Start(db2); // pass Database intance db2 to TransactionTest()

}

Everthing is OK...

Well When I check PetaPoco source code at transaction I see that at transaction.Complete

 public virtual void Complete()
        {
            _db.CompleteTransaction();
            _db = null;
        }

My question is that to able to use transaction from multiple threads Do I have to use new copy of Database object? Or what am i doing wrong?

And to make it thread safe do i have to open and close NEW database at every data update-query?


回答1:


Yes, you need a separate PetaPoco Database instance per-thread. See this quote from the PetaPoco documentation:

Note: for transactions to work, all operations need to use the same instance of the PetaPoco database object. So you'll probably want to use a per-http request, or per-thread IOC container to serve up a shared instance of this object. Personally StructureMap is my favourite for this.

I bolded the phrase that gives the clue. It is saying that one instance of the PetaPoco database object should be used per-thread.




回答2:


Hi use with nolock in select query because the table may be locked. long count = db.ExecuteScalar("SELECT Count(*) with nolock FROM MediaDevices");




回答3:


sorry dude.. yes you are right. they change the object to be null. so you cannot use the same object to threading. you have to use they use described like db=GetDataBase() ; db2=GetDataBase();

otherwise you can change the source code for your requirement. i think their license allow it. but i am not sure.



来源:https://stackoverflow.com/questions/11191926/petapoco-transaction-in-multithread-env

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!