open and close mysql connection multiple time
<p>I want to write a function for mysql connection to use it any where.
there is tow way in my mind</p>

<p>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</p>

<p>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</p>

<p>my question is:</p>

<p>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)?</p>

<p>I use some programming language like java, php and ... .but my question is general </p>