When is it better to open MySQL connection?
Is better to leave it open throughout the life of the application, or session?
Is better to open when the application or the session starting, and close when the application or session finish?
Or it is better to open the connection only when you have to use it. Example when push in a button and then close it again immediately...
I tried in different ways:
- Open when Session start and close when finish. In this way the operations are faster because every time I save the time it takes to connect to MySQL. A new connection is created for each new session. It means a connection for each visit. But does leaving the connection open for each visit take up a lot of resources?
- Open when Application start and close when finish. Fast as during the test in Session. A new connection is created one time at start. It means only one unique connection for all visit. But does leaving the connection open throughout the Application life take up a lot of resources?
- Open and close the connection whenever I need it, for example by pushing a button. Very slow, every time I have to wait for the connection to MySQL. Surely it is the method that makes me consume less resources, but it is the slowest one.
What do you think?