[英]multithreading with database
我正在尋找一種策略來利用多線程(可能是異步委托)來進行同步操作。 我是多線程的新手,所以我將首先概述我的場景。 現在,基於所提供的參數,對一組數據(組合)完成該同步操作。 (psudeo-code)實現如下:
public DataSet DoTests(int fundId, DateTime portfolioDate)
{
// Get test results for the portfolio
// Call the database adapter method, which in turn is a stored procedure,
// which in turns runs a series of "rule" stored procs and fills a local temp table and returns it back.
DataSet resultsDataSet = GetTestResults(fundId, portfolioDate);
try
{
// Do some local processing on the results
DoSomeProcessing(resultsDataSet);
// Save the results in Test, TestResults and TestAllocations tables in a transaction.
// Sets a global transaction which is provided to all the adapter methods called below
// It is defined in the Base class
StartTransaction("TestTransaction");
// Save Test and get a testId
int testId = UpdateTest(resultsDataSet); // Adapter method, uses the same transaction
// Update testId in the other tables in the dataset
UpdateTestId(resultsDataSet, testId);
// Update TestResults
UpdateTestResults(resultsDataSet); // Adapter method, uses the same transaction
// Update TestAllocations
UpdateTestAllocations(resultsDataSet); // Adapter method, uses the same transaction
// It is defined in the base class
CommitTransaction("TestTransaction");
}
catch
{
RollbackTransaction("TestTransaction");
}
return resultsDataSet;
}
現在要求是為多組數據做這件事。 一種方法是在循環中調用上面的DoTests()
方法並獲取數據。 我寧願並行做。 但有一些捕獲:
StartTransaction()
方法每次調用時都會創建一個連接(和事務)。 DoTests().
,所有底層數據庫表,過程都是相同的DoTests().
(明顯)。 因此我的問題是:
DoTests()
方法之外,還有其他更有效的方法來執行上述操作嗎? 希望我完全明白你的意思。 1.如果您在不同的線程上多次調用DoTests
您的代碼性能會更好。 假設您沒有被鎖定在GetTestResults
和UpdateXXX
方法中。 2.這基本上取決於你調用的方法的實現( GetTestResults
和UpdateXXX
方法)3。你打算同時調用幾個DoTests
,對嗎? 那你為什么要問普通循環呢?
實際上我並沒有完全了解交易。 據我所知,每個實例都會使用自己的事務,對嗎? 否則我相信你會遇到序列化交易更新的麻煩。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.