I want to write a function for mysql connection to use it any where. there is tow way in my mind
1)in top of function open connection and execute query and close connection. in this way each query open connection and close it but have easy usage because no need to handle open and close connection in separate function and call them in top and bottom of code
2)write a function for opening connection and another function for closing it and call them in top an bottom of code. in this way one connection open and use for multiple query then close
my question is:
is there any difference to open and close the db connection multiple times or it doesn't matter? which way have a better performance(in second way connection may open some minutes)?
I use some programming language like java, php and ... .but my question is general