E
E
Egor2014-02-28 13:19:36
MongoDB
Egor, 2014-02-28 13:19:36

Node.js + mongoose. Whether to keep 1 connection or open / close for each request?

Good afternoon.
I recently started learning node.js.
As a database - mongodb. I use mongoose to work with it.
Initially created a new connection for each request and closed at the end. Sometimes there were errors like "connection closed" when a request was sent using an already closed connection. It is clear that this is due to my curvature in matters of asynchronous code. But I thought about this question. Is it worth opening a new connection for each request, or is it better to create one for all?
I understand correctly that when using only 1 connection, it will be used for all users. And if you create a new one every time, then these connections will work in parallel?
Do not hit if you asked stupidity))

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Y
Yuri Shikanov, 2014-03-01
@ByKraB

Mongoose itself is smart enough to manage connections. See the documentation (section Connection pools)

Z
zxmd, 2014-02-28
@zxmd

Theholmesoffice.com/mongoose-connection-best-practice might help

K
Konstantin Kitmanov, 2014-02-28
@k12th

Why do extra work for every request? Why the extra level of callbacks?
It must be borne in mind that node.js applications live for a long time, and are not launched on every request.
You will not manually add gasoline to the tank after each stroke of the internal combustion engine?

A
Alexey, 2014-02-28
@AlexFork

Keep a pool of connections.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question