tags:

views:

56

answers:

3

I have a website where at the top of each page I include a page that opens a mysql_connect connection, so I don't have to open up the connection each time I need to modify or insert stuff into the database. Is this safe, or should I open and close each connection once I'm done using it?

require_once("../include/mysqlconn.php");

$sql = "SELECT * FROM blah";
$query = mysql_query($sql);
A: 

Personally, I create a new connection each time I make a query, then close it. If you have multiple databases, you can clash if you leave it open.

Jason
That's a really bad practice as it may slow down your server. Opening connections (be it via TCP or UNIX socket) has some overhead. If you have multiple databases or multiple servers, name the connection link variables accordingly. In any case keeping the connection open during execution of just one script is fine.
mojuba
How much overhead are we talking about?
Jason
If you run just one simple query that returns a few rows, it may be, say 2 packets vs 7-8 exchanged between the DB server and the application, and that's for each request. But then of course it depends on the overall load of your application server.
mojuba
Not so much overhead but it's still bad practice. Your reason to open if for the every query is just ridiculous.
Col. Shrapnel
Interesting. I've taught myself to program, so I did not know too much about overheard. I like the peace of mind of not having an open database connection. Thanks for the input, I'll have to change it because I open and close alot. haha.
Jason
@Jason - shame you're not coding .NET otherwise we'd all be giving you a pat on the back and telling you what a great job of self teaching you'd done - http://www.developer.com/net/article.php/3729831/Using-Connection-Pooling-in-ADONET.htm or http://aspalliance.com/1099. I just guess connection pooling in apache/mysql works differently so connections with page vs. method level affinity are the way to go for scalability here :P
f00
I was going for scalability. In the long run I want people to be able to have plugins, and I am a little eerie about leaving a connection open so that a developer can willy-nilly query a password or something. I guess, upon reflection, if a plugin developer wants a database entry, he will get it.
Jason
@Jason - in .NET "connection open" and "fetch connection from pool" are synonymous as are "connection close" and "return connection to pool" so your approach of opening/closing or rather fetching/returning connections before and after each db call is actually BEST PRACTICE for developing high performance, highly scalable enterprise level applications in MS technologies. As far as developing similar apps in LAMP this approach is bad practice due to lack of proper connection pooling mechanism, although 3rd party conenction pooling extensions are available - so might be worth investigating...
f00
A: 

This is OK as the connection will be closed automatically at the end of each script execution (actually all resources are freed at that point).

Another possibility is to use mysql_pconnect() which does connection caching for you and may speed up your site a bit.

In either case there is nothing to worry about security wise, as long as the password is kept in a place not accessible from outside.

mojuba
The only sensible answer out of 3 so far but still -1 for mentioning pconnect.
Col. Shrapnel
Care to explain what's wrong with pconnect?
mojuba
Care to explain what's good with pconnect? Have you ever used it? Measure that "speed up"? Ever experienced Too many connections error?
Col. Shrapnel
Um, unless the connection is secured via SSL/TLS, the authentication information is being sent in plain text.
Gumbo
pconnect() saves you the overhead of opening and closing connections. If you get "Too many connections" AND switching to connect() solves the problem, it means only one thing: it's a result of leaving some database locks unreleased. Otherwise both connect() and pconnect() use roughly the same number of channels for the same site. Honestly, I doubt you know what you are talking about. Sorry.
mojuba
@Gumbo if mysqld runs on the server as apache, which is the case in the vast majority of cases, then no need to encrypt your connection with mysql.
mojuba
connect() and pconnect() cannot use the same number of channels because some pconnects will be always hang out unemployed. "Connection caching" means that. Just google for too many connections.
Col. Shrapnel
Col., the whole point of pconnect() keeping the channels open is reusing the channels without reopening them. With careful programming (such as never leaving locks) pconnect() gives you some performance gain. Don't use it though if you are unsure.
mojuba
A: 

well closing it is safer. you can also include a page that closes the connection to mysql at the end of every page you open the connection.

burntblark
Connections are always closed when the script is done being executed, no matter what.
Vincent Savard