2

Right now my project website starts a connection with the database as the website is loaded. I have pages on the website where a connection to a database isn't required.

Is it resourceful to maintain a connection to the database for the entire duration of a session, or keep remaking connections when an event occurs that actually requires a connection? (Such as registering an account)

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Personally, I think it is best to open up a connection to the database only when you need to utilize the database. Even if you have to open the database multiple times during a single session, your application will not be opening connections at will for no particular reason. Building your application around the idea that every database connection that opens is opened with a purpose ensures that you use your connectivity wisely rather than uselessly. Besides, opening a new database connection is not very resource-intensive, plus it's not harmful to open a few extra connections for simple functionality. If anything, you should combine database functionality

Zack Zatkin-Gold
  • 814
  • 9
  • 29