Where and when to open a database connection

前端 未结 4 1168
無奈伤痛
無奈伤痛 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:45

    Because connections are rather expensive, as others have pointed out, I'd recommend using a "lazy connect" technique in your database layer. If you have structured your application effectively, your application logic should not be concerned with when connections are opened and closed as this would be encapsulated in the database layer. The database layer, when asked to perform a query, would first check to see if it has an active connection and if not, create one. This way you'll avoid opening connections that are never used and you'll also have a nice separation of logic between your application and the database code.

提交回复
热议问题