[英]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.