Saturday, 23 September 2023

How to preserve a database connection pool in hooks.server.js during hot reload in SvelteKit with Vite.js and mariadb?

I am using SvelteKit, Vite.js, and the mariadb package with Node.js in my application. I have the following code in the db.js file:

import mariadb from 'mariadb';

const databaseConnectionPoolConfig = {
  ...
};

let databaseConnectionPool = undefined;

export function createDatabaseConnectionPool() {
  return databaseConnectionPool ??= mariadb.createPool(databaseConnectionPoolConfig);
}

Inside the hooks.server.js file, I have the following code:

import { createDatabaseConnectionPool } from '$lib/db';

createDatabaseConnectionPool();

When a hot reload is performed, the databaseConnectionPool is reset to undefined, but the connections in the pool are not closed, and new ones are created. I checked this by running the following query:

SHOW STATUS LIKE 'Threads_connected';

Which increases each time a hot reload is performed by the number of connections specified by the databaseConnectionPoolConfig.connectionLimit property.

How can I prevent this from happening?



from How to preserve a database connection pool in hooks.server.js during hot reload in SvelteKit with Vite.js and mariadb?

No comments:

Post a Comment