next.js: Next.js 13 app dir: Fast Refresh (hot reloading) causes database connection exhaustion
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103
Binaries:
Node: 19.4.0
npm: 9.2.0
Yarn: 1.22.19
pnpm: 7.25.1
Relevant packages:
next: 13.1.6
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
- Fast Refresh (hot reloading)
- app directory
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/autumn-mountain-0efmhn
To Reproduce
Run the project in CodeSandbox
https://codesandbox.io/p/sandbox/autumn-mountain-0efmhn
Start editing and saving any of these files => database/connect.ts / app/page.tsx
Check the console and watch the database connection count increase as the changes are saved
Describe the Bug
With Fast Refresh (hot reloading) during the development mode, a new database connection is initiated on every refresh, Thereby causing a database connection slot error like remaining connection slots are reserved for non-replication superuser connections in PostgreSQL
In the CodeSandbox linked above, a database connection is made similarly to how the docs section Sharing data between Server Components suggests:
export const db = new DatabaseConnection(...);
Screenshot of the error on the browser

Screenshot of the error in CLI

When a file that imports a database code is edited and saved, the Fast Refresh makes a new connection to the database on every refresh, rapidly increasing the database connection count. The database connection slot error is triggered when the count is above 90
The videos below show the process that triggers the error and the database connection count increment
I also left a comment on another similar discussion, but this discussion is not about the app directory: https://github.com/vercel/next.js/discussions/26427
Expected Behavior
It is expected that the development mode Fast Refresh (hot reloading) doesn’t attempt reconnecting to the database but instead uses the previous connection.
Workaround
Using the globalThis singleton workaround described here https://github.com/vercel/next.js/discussions/26427#discussioncomment-898067, I was able to prevent this behavior.
import postgres from 'postgres';
declare module globalThis {
let postgresSqlClient: ReturnType<typeof postgres> | undefined;
}
// Connect only once to the database
// https://github.com/vercel/next.js/discussions/26427#discussioncomment-898067
function connectOnceToDatabase() {
if (!globalThis.postgresSqlClient) globalThis.postgresSqlClient = postgres();
return globalThis.postgresSqlClient;
}
export const sql = connectOnceToDatabase();
The video below shows that the database connection count is not increasing above 13-14 counts regardless of how many reloads happen
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 10
- Comments: 16 (6 by maintainers)
Commits related to this issue
- Add a reference to https://github.com/vercel/next.js/issues/45483#discussioncomment-898067 — committed to TU-Darmstadt-APQ/kraken_webui by PatrickBaus 2 years ago
- Add a reference to https://github.com/vercel/next.js/issues/45483#discussioncomment-898067 — committed to TU-Darmstadt-APQ/kraken_webui by PatrickBaus 2 years ago
- Add a reference to https://github.com/vercel/next.js/issues/45483#discussioncomment-898067 — committed to TU-Darmstadt-APQ/kraken_webui by PatrickBaus 2 years ago
- Add a reference to https://github.com/vercel/next.js/issues/45483#discussioncomment-898067 — committed to TU-Darmstadt-APQ/kraken_webui by PatrickBaus 2 years ago
The workaround solution does not work anymore
Seconded that this appears to not work anymore (14.2). Is there really no functioning way to create a singleton object in NextJS (in dev mode)? This seems like a pretty simple use case that I’m shocked is impossible, or even requires a workaround.
Pretty important issue, +1 on it. Super important for any non-trivial apps which use any global context or services (e.g. a Message Queue consumer, or gRPC clients / Protobuf parsers).
Few extra observations:
globalContextis also getting reinstantiated based on some funny rules. Every singleton has to define its own extension ofglobalContextand export its owngetSingletonInstance()-kind-of-a-function.instance ofoperator doesn’t work at least in dev mode. If you created a custom exception class, for example, you cannot usex instance of MyException, since multiple versions of the same class may be present.cookiesetc). Next is not able to find it’s async context.