Why do we need to connect and disconnect the database each time?

tdd

#1

Question

why do we need to connect and disconnect the database each time?

Answer

The main debate is about scalability which has to do with the amount of work that is being processed at the time, leaving an open connection to the database might become taxing for the running program while it is also executing other tasks, that said, we do not really need to worry much unless we have a database with thousands of documents and multiple users at once. There is also the mention of having one open connection for multiple models or instances of the model, but since our exercises deal with one task at a time, the open, execute callback, close pattern works in a way we get used to always disconnect from the database.

For example, in our create exercise we use our runWithDatabase function to insert in the collection all the documents from items.js, not just one. That way we performed all the tasks necessary with the database in one function, and once we will not use the database anymore, we disconnected from it, because there is no more need to have that connection using execution space.

When we talk about execution, it is referred to a space in memory used momentarily to run programs, commonly known as RAM, if we use a lot of that finite space with the execution, the computer slows down.

So, we connect and disconnect the database to not keep on using execution space after we are done interacting with the database.