I am just testing a PetaPoco transaction in a multi-threaded way ...
I have a simple test case:
- The simple value object is called MediaDevice - Insert the update record 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 it in two threads as follows: [Single Database object for both threads]
void TransactionTest()
{
Database db = GetDatabase();
Thread tThread1 = ...
Thread tThread2 = ...
tThread1.Start(db);
tThread2.Start(db);
}
I get a Null error or sometimes an object located for the database ..
But when I put two database instances,
void TransactionTest()
{
Database db = GetDatabase();
Database db2 = GetDatabase();
Thread tThread1 = ...
Thread tThread2 = ...
tThread1.Start(db);
tThread2.Start(db2);
}
Everthing OK ...
Good. When I check the source code of PetaPoco in a transaction, I see that in a transaction. Full featured
public virtual void Complete()
{
_db.CompleteTransaction();
_db = null;
}
My question is the ability to use a transaction from multiple threads. Should I use a new copy of the database object? Or what am I doing wrong?
, ?