Where and when to open a database connection

前端 未结 4 1162
無奈伤痛
無奈伤痛 2021-02-07 11:31

I am working on implementing use of the mysql class found here in an existing script. The script almost always needs to interact with the database, even if there are times when

4条回答
  •  清酒与你
    2021-02-07 11:43

    Well, if you are using a class, the connection should be opened automatically when you instantiate the class, or when the first query is performed. If you never use the class, the connection wouldn't be opened. While it is good practice to close it when you don't need it, it doesn't hurt to let it be closed when the request thread dies.

    This can be bad if you don't have a resource limits set in your php.ini file, the request could possible live forever and never close the connection.

    If you have a medium-to-high traffic site, you should be thinking about using mysql_pconnect anyways so there is always a connection open and you don't need the overhead of opening one on every request.

提交回复
热议问题