Quote:
For a PHP website, do you write mysql_connect for every page and apparently request. And then call mysql_close on the same page after the work is done. But I think this will increase the load on the server and is less reliable.
|
That's the way you typically do it, yes. I've never heard of it causing problems with either load or reliability.
It certainly won't be a problem on a single-server PHP website.
Keep in mind that with PHP, a new instance of the PHP interpreter is launched for each web request, and then closed when the request is served.
Quote:
Or do you give connection to the user when the session starts and cleanup the connection when the user session ends. What if the user does not end the session and just exits the browser? Will this leave the db connection open?
|
For a PHP website? The only to make that work would be to write a Java or Silverlight plugin to connect to the database. Web browsers don't support OBDC.
You'd typically clean up resources (such as open connections) when the application exits. I *assume* both Java and Silverlight allow you to detect if the browser is being closed. If not, then yes, you'd have a resource leak.
Another problem with this approach that it's not scalable. Connections (i.e. sockets) are a finite resource, and they can be used up. FTP servers, which also deal with clients with persistent connections, have the same problem.
(You could use web sockets too, but I don't think that's something you're going to go for. See:
http://socketo.me/)
Another possibility is to use the connection pool pattern. Here's the Wikipedia page:
http://en.wikipedia.org/wiki/Connection_pooling
SQLAlchemy, an ORM for Python, uses connection pooling. Here's the relevant part of its developer docs:
http://sqlalchemy.readthedocs.org/en...e/pooling.html
Quote:
The other possibilities exist like application crash and powercut. Does it leave any stray connections to the database?
|
Yes, and dealing with that is a database administration issue. Google for, oh, "mysql time out idle connections" without the quotes.