Connection objects in multithreaded appliations
-
Hi, I am presently working on a multithreaded VC++ application which consists of 3 threads each accessing the same database. Would it be more efficient to use 3 different connection objects or just a single connection object in this case? Looking forward to your reply.. Thanks, Mahadevan
-
Hi, I am presently working on a multithreaded VC++ application which consists of 3 threads each accessing the same database. Would it be more efficient to use 3 different connection objects or just a single connection object in this case? Looking forward to your reply.. Thanks, Mahadevan
It all depends on whether you are bringing up the connection and closing it down on each thread when you need to use the database. Bring up connections it fairly time consuming. I would tend only to have one database connection and serialise the database access between the threads. This database connection would tend to be instanciated when the application initialises and destroyed on application exit. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain) -
It all depends on whether you are bringing up the connection and closing it down on each thread when you need to use the database. Bring up connections it fairly time consuming. I would tend only to have one database connection and serialise the database access between the threads. This database connection would tend to be instanciated when the application initialises and destroyed on application exit. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)Well, In all the threads, the database connection is opened and closed only once and the threads are alive as long as the application is alive. In this case, will my application work faster than an application which has only a single database connection object? I am asking this because in order to serialize a single database connection between the multiple threads would require mutex (correct me if I am wrong) which may degrade the performance? Do you foresee any problem (esp. performance degradation) in case I use multiple connection objects? Regds, Maha
-
Well, In all the threads, the database connection is opened and closed only once and the threads are alive as long as the application is alive. In this case, will my application work faster than an application which has only a single database connection object? I am asking this because in order to serialize a single database connection between the multiple threads would require mutex (correct me if I am wrong) which may degrade the performance? Do you foresee any problem (esp. performance degradation) in case I use multiple connection objects? Regds, Maha
Depending on the type of database connection object used there can be deadlock problems. In the past I have used multiple threads with ADO each having its own database connection. In which I experienced deadlock problems within the ADO code (which is not under my control). I switched the system to a single connection and serialised access to get over this. Saying that, this may not be a problem any longer. Just bear it in mind when you are developing. To answer your question, theoretically it will be more efficient for each thread to have its own connection. Hope that helps with your descision. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain) -
Depending on the type of database connection object used there can be deadlock problems. In the past I have used multiple threads with ADO each having its own database connection. In which I experienced deadlock problems within the ADO code (which is not under my control). I switched the system to a single connection and serialised access to get over this. Saying that, this may not be a problem any longer. Just bear it in mind when you are developing. To answer your question, theoretically it will be more efficient for each thread to have its own connection. Hope that helps with your descision. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)Hi. Prehaps it would make more sense to have a 'manager' from whom you request access, so that if you require a fourth or more thread(s) then the scaling issue is not a problem. James.
-
Hi. Prehaps it would make more sense to have a 'manager' from whom you request access, so that if you require a fourth or more thread(s) then the scaling issue is not a problem. James.
Yes, that is basically what I have with the serialised access. :) Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain) -
Yes, that is basically what I have with the serialised access. :) Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fruity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)Thanks for your help. I still have a small query, What do you generally prefer for connecting to Database, I use the CDatabase object to connect with the SQL server and I am just not able to avoid the deadlocks in both cases, that is single connection object or multiple connection objects. I seem to be getting this problem when a thread is executing a query and the SQL server goes down. Any suggestions? Thanks a lot, Mahadevan.