Fix with-mongodb hot-reload issue and race condition (#17666)
This PR fixes 2 issues with the mongodb example: ### 1. Fallable Caching Strategy Calling `connectToDatabase()` multiple times before it's cached results in multiple connections being created. The latest one created was becoming the "cached" one and the others dissappear into the background. This is now fixed by using **promise sharing** so that only one connection can ever be created. ### 2. Problematic Hot Reload During development you can monitor your database connections and see that it continues to create more and more connections over time. Some users have reported their [databases reaching maximum connection limits](https://github.com/vercel/next.js/discussions/12229). This is resolved by using `global` to store the cached connection. It's not ideal but it is necessary.
Showing
Please
register
or
sign in
to comment