Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Polling MySQL database with Timer class every millisecond for 8 hrs a day, wanted to know if I should open and close the db after each connection or should I just leave the connection open until the total process is complete?

Seems pretty inefficient to keep re-establishing a connection every millisecond but I'm only guessing. I recall reading something along the lines of the MySQL using connection pooling and actually not disposing of the connection but adding it back to the connection pool but I'm not exactly sure on that...

Can someone please enlighten me on this one?

-Dee
Posted
Updated 26-Nov-11 9:39am
v2

If the connection is created every millisecond, it would be better to keep connection open..
 
Share this answer
 
Comments
d.allen101 25-Nov-11 0:12am    
thanks! another question on a different but related topic, is it alright to share that connection object with multiple threads and if so would that also improve performance?
Nitesh Kejriwal 25-Nov-11 0:16am    
I think sharing connection object within multiple thread would be a good solution if implemented properly because there is a possibility of deadlocks too. Please check:
http://www.codeproject.com/KB/threads/multithreadedappnconnpool.aspx

As mentioned in article, if you are using shared connection, you should open/close connection everytime.
d.allen101 25-Nov-11 19:41pm    
Nitesh thanks for the article. It was VERY informative! It answered exactly what I wanted to know. Thanks again.
Nitesh Kejriwal 26-Nov-11 0:10am    
Please mark as answer if you got the solution and upvote so that it can help other people in the community.
Exactly as the above answer if you want to connect with the DataBase in every second then it is better to keep conncetion open rather than connecting with DataBase in every Second.It saves you system Resources, line of code.
 
Share this answer
 
v2
Comments
d.allen101 25-Nov-11 0:16am    
What about the related comment I posted below the solution?
RaviRanjanKr 25-Nov-11 3:04am    
Font was to small. I edited your answer to give appropriate FONT of your Answer.
I would advise to use lazy initialization pattern () to open a connection when required and keep it open. Something like this:

C#
class DatabaseWrapper {
   
    internal DatabaseWrapper(/*...*/) {
       //...
       fConnection = new //...
       //...
    }

    /*...*/Connection Connection { // SqlConnection, OleDbConnection, whatever
           get {
              if (fConnection.State != System.Data.ConnectionState.Open)
                 fConnection.Open();
              return fConnection;
           }
    }

    /*...*/ SomeMethod() {
        // here, connection will be guaranteed to open, but only once:
        /*...*/Command command = new /*...*/Command(query, Connection); // SqlCommand, OleDbCommand, whatever...
    }

    /*...*/Connection fConnection; // SqlConnection, OleDbConnection, whatever
}


—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900