I have a method in a web-application with a decent amount of code. In almost opposite ends of the method, I have database interaction.
Is it best practice to open/close its connection multiple times or open its connection when first needed/close it when last needed?
Multiple Times
connection.Open();
//execute db interaction
connection.Close();
//execute business logic
connection.Open();
//execute db interaction
connection.Close();
//execute business logic
connection.Open();
//execute db interaction
connection.Close();
//etc...
Open First/Close Last
connection.Open();
//execute db interaction
//execute business logic
//etc...
//execute db interaction
connection.Close();