Creating Connections to MySQL Database
Posted on 2006-12-01
Suppose I have a web application developed using JSP that must use a MySQL database to run queries and update values in the database.
Then I would need to create a connection to the database. (I know how to do this).
Since connections to a database is "expensive", we only want to create a connection when it is absolutely necessary, correct? (in other words, we don't want to create say 10 connections just for one user to run one query).
So here's my question --
When do you create a new connection to a database?
Do you always create a connection to a database for each user that logs in to the web application?
Or do you only ever have ONE single connection and pass this connection along multiple users of the web application?
What is the industry norm?
Also, how do you close the connection to the database? (I know how to do it in code)
You want to close it whenever the user logs off, but the user may simply close the browser.
In such a case, how does the connection get closed other than timing out?