Solved

how to use multiple instance of a db connection with multiple thread in C#.Net

Posted on 2012-12-23
9
521 Views
Last Modified: 2013-01-22
I have a console application(C#). I want to use more than one instance of mysqlconnection.
I have  a master table with 50000 rows. I want to split the data in 10 set and want to run some procedures parallel with the data. please help me.
0
Comment
Question by:dynamicweb09
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 1

Expert Comment

by:igordevelop
ID: 38717968
Hi,

Using multiple instances is usually something that developers want to avoid. In fact, you should use the same instance for multiple operations. Make the connection instance to be global so you can access it from any code-block and just play with its state (open it and close it after use each time).

Example, make this global, public:
SqlConnection conn = new SqlConnection("ConnectionString");

Then, every time you need to use this connection don't forget to open and close it, or if you use it for parallel execution do not close it after all queries are executed;

conn.Open();
//run queries
conn.Close();

Let me know if anything.

Regards,
Igor
0
 
LVL 1

Author Comment

by:dynamicweb09
ID: 38717980
Thanks Igor for your reply.

Actually I want to run same procedure with different parameter  parallel y. please help me
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 167 total points
ID: 38718287
You do not need multiple instances of the connection, you need multiple instances of the type of object in which you receive the data (collection, DataTable, DataGridView).

The same Connection object can be used with multiple Command objects, each of whom having different parameters, and they can all use the same Connection. However, by default, you need to run the commands one after the other. You cannot run multiple command at the same time.

SQL Server has a mode that enables multiple commands to run simultaneously on the same connection, but I cannot say if it is available for MySql. It is called Multiple Active Result Sets (MARS). You might want to search your MySql documentation to know if it is available.

I would also point out that I would not use a global variable as suggested by Igor. Global variables are a pain to debug. When you have a problem with one of those, you need to look all over the place in the application.

ADO.NET has been built in order to enable to use local variables for connections. Using those, if you have a problem, your search is limited to the method in which you have created the variable. This makes debugging and maintenance a lot easier.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 1

Author Comment

by:dynamicweb09
ID: 38718335
Can I call Asynchronously? if yes then how?
0
 
LVL 40
ID: 38718963
Once again, I speak of my experience with SQL Server, so I am not sure that MySql has those, but the Command object has a series of BeginExecute methods that runs asynchronously. Since all the ADO.NET libraries follow the same model, you might have those on your side.
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 83 total points
ID: 38720417
@JamesBurger
SQL Server has a mode that enables multiple commands to run simultaneously on the same connection, but I cannot say if it is available for MySql. It is called Multiple Active Result Sets (MARS).
Please cite a reference that indicates MARS queries in parallel.

http://technet.microsoft.com/en-us/library/cc966385.aspx

It is probably as important to delimit what MARS is not:

Parallel execution: Though MARS enables more than one request to be submitted under the same connection, this does not imply that they will be executed in parallel inside the server. MARS will multiplex execution thread between outstanding requests in the connection, interleaving at well defined points.
0
 
LVL 40
ID: 38720424
@kaufmed

As stated in your reference, MARS works through "Interleaved execution". It is my understanding and my experience that from the application point of view, you feel that the result is the same as if the stuff was working in real parallel execution. If you start a long asynchrone query, and follow it with a short asynchrone query, the second one will finish before the first.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38720652
I don't believe that is guaranteed, though. The reference states "at well-defined points." Do you know what those "well-defined points" are? Is it guaranteed that a short query will finish before a long query?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 167 total points
ID: 38720795
Unfortunately, I am not in the secrets of the gods. And I never explecitely tested for that. All I can say by my experience is that you get the feeling that the queries are executing at the same time, and in my book, how the user feels is more important than what really happens.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Backup Strategy 15 46
Optimize the query 5 43
IEnumerable<T> to a List<T> 8 40
Visual Studio npm 1 12
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question