简体   繁体   English

多线程与数据库

[英]multithreading with database

I am looking out for a strategy to utilize multithreading (probably asynchronous delegates) to do a synchronous operation. 我正在寻找一种策略来利用多线程(可能是异步委托)来进行同步操作。 I am new to multithreading so i will outline my scenario first. 我是多线程的新手,所以我将首先概述我的场景。 This synchronous operation right now is done for one set of data (portfolio) based on the the parameters provided. 现在,基于所提供的参数,对一组数据(组合)完成该同步操作。 The (psudeo-code) implementation is given below: (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;
}

Now the requirement is to do it for multiple set of data. 现在要求是为多组数据做这件事。 One way would be to call the above DoTests() method in a loop and get the data. 一种方法是在循环中调用上面的DoTests()方法并获取数据。 I would prefer doing it in parallel. 我宁愿并行做。 But there are certain catches: 但有一些捕获:

  • StartTransaction() method creates a connection (and transaction) every time it is called. StartTransaction()方法每次调用时都会创建一个连接(和事务)。
  • All the underlying database tables, procedures are the same for each call of DoTests(). 每次调用DoTests(). ,所有底层数据库表,过程都是相同的DoTests(). (obviously). (明显)。

Thus my question are: 因此我的问题是:

  • Will using multithreading anyway improve performance? 无论如何使用多线程会提高性能吗?
  • What are the chances of deadlock especially when new TestId's are being created and the Tests, TestResults and TestAllocations are being saved? 什么是死锁的可能性,特别是在创建新的TestId并且正在保存测试,TestResults和TestAllocations时? How can these deadlocked be handled? 这些僵局如何处理?
  • Is there any other more efficient way of doing the above operation apart from looping over the DoTests() method repeatedly? 除了重复循环DoTests()方法之外,还有其他更有效的方法来执行上述操作吗?

Hopefully I completely understand what you mean. 希望我完全明白你的意思。 1. Yes if you call DoTests several times on different threads your code performance will be better. 1.如果您在不同的线程上多次调用DoTests您的代码性能会更好。 Assuming you don't get locked inside your GetTestResults and UpdateXXX methods. 假设您没有被锁定在GetTestResultsUpdateXXX方法中。 2. This basically depends on your implementation of methods you call ( GetTestResults and UpdateXXX methods) 3. You were going to call several DoTests simultaneously, correct? 2.这基本上取决于你调用的方法的实现( GetTestResultsUpdateXXX方法)3。你打算同时调用几个DoTests ,对吗? So why do you ask about plain looping? 那你为什么要问普通循环呢?

Actually I did not get you completely about transactions. 实际上我并没有完全了解交易。 To my understanding each instance will you use its own transaction, correct? 据我所知,每个实例都会使用自己的事务,对吗? Otherwise I believe you run into troubles serializing updates of transaction. 否则我相信你会遇到序列化交易更新的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM