New DB connection on every request?
See original GitHub issueHi @sandeepsuvit.
I was doing some stress tests as we prepare to go live, and maybe I did something wrong, but it seems like for every new request, it’s creating a new database connection

TenancyModule.forRoot({
tenantIdentifier: 'X-TENANT-ID',
options: () => { },
uri: (tenantId: string) => {
console.log('connecting!?', tenantId);
return process.env.TENANT_MONGO_DB_CONNECTION_STRING.replace(
'{{TENANT_ID}}',
tenantId,
);
},
}),
I ran a script that calls a URL, the URL return a 500 code, which is okay. But wheneve r I ran the script, the connection to my MongoDB Atlas instance skyrockets.
Would you know why? I saw that it’s supposed to cache it: https://github.com/needle-innovision/nestjs-tenancy/issues/12. But now I’m puzzled, and trying to find the root cause of the problem.
Issue Analytics
- State:
- Created 9 months ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Should we open a new db connection for every new request
There is a basic connection pool in the database/sql package so it reuse database connections. So, you can open when you need and...
Read more >Improve database performance with connection pooling
When a new request to access data from the backend service comes in, the pool manager checks if the pool contains any unused...
Read more >Creating database connections - Do it once or for each query?
-1 The answer is rather misleading. Creating a connection per query is a very bad idea. What you probably mean is "retrieve a...
Read more >Should I keep a database connection open to handle every ...
For example a database such as MongoDB. I doubt that it is unnecessary to open and close a connection for every request. So...
Read more >Connection Strategies for Database Applications
A database connection is a physical communication pathway between a client process and a database instance. A database session is a logical entity...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@raphaelarias @elmapaul apologies for replying late on this since I only work on this during my spare time. Also @raphaelarias regarding your query the connections are stored on a map and reused if found. Refer to this line in the code.
@raphaelarias I really respect your concern, cz wondering as well how it will behave in prod mode with N users/connections. So pity, this library is not actively supported. No complains, just a thought.
Quick tip: you can easily wrap up your all messages in one with edit 😃