Re: MySQL close connection, what's the purpose?
| From: | tedd | Date: | Sun, 02 Apr 2006 01:08:08 +0000 |
| Subject: | Re: MySQL close connection, what's the purpose? | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-233073@lists.php.net to get a copy of this message | ||
At 11:07 AM +1200 4/2/06, Jasper Bryant-Greene wrote:
tedd wrote:No, I'm not. What I am suggesting is that if you are done with your query, and are not immediately asking another, then hang up. If another segment of your code wants to access the dB again, then open it as you want. The practice makes for more modular code and keeps the right connections active when they are needed.So you close it after every query and then re-open it later for the next query? I don't see that as a good idea.I always close the connection right after myquery -- force of habit. It's like leaving the toilet seat up, it's only going to get you into trouble.No, you leave it open until you're done with the database.Uh, what if you want to do more than one query in a single request? You aren't seriously suggesting you would connect and disconnect from the same database multiple times within the same request?Reading Ted's post didn't give this impression. I wanted to make sure he wasn't doing it that way.Chris et al: Actually I am. When I need something from the dB, I open it, get the information and close it. It's like opening a drawer, getting what you need, and then closing the drawer. Where's the problem?
In my experience, connecting to the database takes up more than half of the execution time of the average database-driven PHP script (I said *average*, there are exceptions). You don't want to be doing it multiple times if you don't have to.In my experience, it's better, in many ways, to put things away when you're done using them. This tread started out with "What's the purpose of closing a connection? After all, when the script finishes, doesn't the dB automatically close?" While that's true, I personally think it's a bad habit that could lead to problems. Your mileage may vary. tedd -- -------------------------------------------------------------------------------- http://sperling.com