php - Where to create and close database connection? -
i know have close db connection @ end. let's have class , there 4 methods , each consists of db queries. question
should create connection , close in every different methods or create connection in constructor of class? if create connection in constructor close it. because if close in function no other method can access it.
if create new connection every method no of connections may high. where create , close function proper way?
if open it, need close it.
as long close connection, can close want. seems maybe closing woes more design of procedures of program. because database call uses connection doesn't mean has opened , closed right there it.
say might want reuse $mysqli variable that's holding connection. can pass connection function , either return or close inside. in way, can isolate main activity of database call in body of function without having worry opening , closing of everything.
using technique, it's possible reuse connection multiple queries, contained each inside own method. maybe better off opening class , passing $mysqli connection it, needed.
this depends on program , choices how work.
if developing program, , you're curious impact of opening , closing actions, can call http://php.net/manual/en/mysqli.stat.php or similar function. if opening many threads , not closing them, see thread count escalate in answer mysqli_stat().
the calls may not close, example, if have poorly designed query runs away you; scope of connection object should close naturally end of script.
Comments
Post a Comment