db0 icon indicating copy to clipboard operation
db0 copied to clipboard

Support connect/reconnect

Open ITsPro-CN opened this issue 2 months ago • 3 comments

Environment

Node.js: 22.14.0 Nuxt: 4.1.3

Reproduction

  • nuxt.config.ts
export default defineNuxtConfig({
    nitro: {
    	experimental: {
    		database: true,
    	},
    	database: {
    		default: {
    			connector: "postgresql",
    			options: {
    				host: "container-name",
    				port: 5432,
    				user: "...",
    				password: "...",
    				database: "..."
    			},
    		}
    	}
  }
})
  • /server/api/index.ts
export default defineEventHandler(async (event) => {
	const db = useDatabase();
	const {rows} = await db.sql`...`;
	return rows;
});

Describe the bug

I use Nitro's experimental features in Nuxt4 and often get errors a few hours after the service starts.

H3Error: Client has encountered a connection error and is not queryable
at /app/.output/server/node_modules/.pnpm/[email protected]/node_modules/pg/lib/client.js:545:17
  ... 5 lines matching cause stack trace ...
  at async Server.toNodeHandle (file:///app/.output/server/chunks/nitro/nitro.mjs:1934:7) {
cause: Error: Client has encountered a connection error and is not queryable
  at /app/.output/server/node_modules/.pnpm/[email protected]/node_modules/pg/lib/client.js:545:17
  at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
  at async StatementWrapper.all (file:///app/.output/server/chunks/nitro/nitro.mjs:5436:15)
  at async Object.sql (file:///app/.output/server/chunks/nitro/nitro.mjs:5338:18)
  at async Object.handler (file:///app/.output/server/chunks/routes/api/index.get.mjs:16:28)
  at async Object.handler (file:///app/.output/server/chunks/nitro/nitro.mjs:1663:19)
  at async Server.toNodeHandle (file:///app/.output/server/chunks/nitro/nitro.mjs:1934:7),

Additional context

No response

Logs


ITsPro-CN avatar Oct 25 '25 05:10 ITsPro-CN

In Nuxt4, each time I use useDatabase() returns the same instance, and I cannot find an elegant way to auto-reconnect.

Tried

const db = useDatabase();
const client = await db.getInstance();
client.on("error", async (err) => {
	console.log("An error occurred with client =>", err);
	db.dispose();
});

End in This database instance has been disposed and cannot be used.

Or

const db = useDatabase();
const client = await db.getInstance();
client.on("error", async (err) => {
	console.log("An error occurred with client =>", err);
	await client.end();
});

Also end in Error: Client has encountered a connection error and is not queryable

ITsPro-CN avatar Oct 25 '25 07:10 ITsPro-CN

Thanks for issue. We should introduce a way to allow reconnecting instances indeed.

pi0 avatar Oct 25 '25 07:10 pi0

Thanks for issue. We should introduce a way to allow reconnecting instances indeed.

Thanks a lot. I will use a proxy for now. Looking forward to the update.

ITsPro-CN avatar Oct 25 '25 07:10 ITsPro-CN