简体   繁体   中英

Connection Pooling with Task Parallel Library in .NET 4

I Have exe which is running on the server continuously for batch processing of thousands of file, data from the file is inserted into the database for all file same connection string is being used. I Have made use of TPL of .net 4 for improving performance. Basic flow of the code as follows since i dont know much about the connection pooling will following approach will lead me into trouble Please suggest. Thanks.

Flow: 1. create array of task (around 50 Task for 50 files) 2. Add each Task to task of Array 3. wait for All task and repeat for next 50 files

                   Task[] taskArry = new Task[50];
                    for (int i = 0; i < 50; i++)
                    {
                        taskArry[i]=(Task.Factory.StartNew(() =>                          InstanceDataObject.InserData()));

                        //var t2 = Task.Factory.StartNew(() => InsertData());
                    }

                         Task.WaitAll(taskArry);

Key point to note here is that i am initiating my connection object for each task in InsertData() methode as each task must be performed with transaction so 50 connection are created for 50 Task will this impact the performace?

    internal static void InserData(string insPath, int filingId)
        {
             try
            {
               DataAccess dataAcess = new DataAccess(true);
               // insert operation here
               dataAcess.CommitTransaction();
            }
            finally
            {
                 dataAcess.Dispose();
                _dbmanager = null;

            }
}

I am using connection string like this "Server=Test;User id=user;Password=@1234;database=test" as default connection pooling is enabled for conection string i Have not set any parameters related to pooling. Shall i have to set the parameter regarding connection pooling? for my scenario

In theory, yes it should be faster.

In reality, it depends how long it takes for InsertData() to run. If it is slow enough, then each of the 50 Tasks may end up creating their own connections (because the pool is always empty), and so you will get no advantage from the connection pool.

The best thing to do is to try your code both with pooling enabled and disabled and see which is better (try to simulate your production environment as closely as possible - network latency and performance of the client and server will make large differences to your data).

You can also use the performance counters built into ADO.NET ( http://msdn.microsoft.com/en-us/library/ms254503.aspx ) to help you evaluate if the pool is helping you or not. Try running your code with pooling turned on and have a look at the NumberOfPooledConnections counter - if this is less than 50, then you are successfully reusing connections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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