J'ai une classe qui exécute du code transactionnel.
laisse supposer:Partage de la transaction de partage entre threads
class Worker
{
public void doWork()
{
//I do not want to create a new transaction. Instead, i want to use the environmenttransaction used by the caller of this method
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) {
workItem1();
workItem2();
workItem3();
scope.Complete();
}
}
Maintenant, j'ai quelques fils qui exécutent ce code:
Worker worker = new Worker();
using (TransactionScope transaction = new TransactionScope())
{
Thread Thread1 = new Thread(new ThreadStart(worker.doWork));
Thread1.Start();
Thread Thread2 = new Thread(new ThreadStart(worker.doWork));
Thread2.Start();
Thread Thread3 = new Thread(new ThreadStart(worker.doWork));
Thread3.Start();
Thread.Sleep(10000); //this should be enough to all the workers finish their job
transaction.Complete();
}
Chaque thread crée une propre transaction. Comment puis-je partager la même transaction entre tous les threads?
Je suis assez sûr que c'est une mauvaise idée à faire. En fonction de vos tâches, cela pourrait aussi être impossible. –