Skip to content
Snippets Groups Projects
Commit cf1e5ab0 authored by David Smadja's avatar David Smadja
Browse files

Improve db connection pool management

parent b1c927dc
Branches
Tags
No related merge requests found
import { dbConnect } from "$lib/server/db-connect"
import { getDbPool } from "$lib/server/db-connect"
import type { Handle } from "@sveltejs/kit"
export const handle: Handle = async ({ event, resolve }) => {
if (!event.locals.sql) {
const sql = await dbConnect()
event.locals = { sql }
event.locals.sql = await getDbPool()
}
return await resolve(event)
}
import config from "$lib/server/config"
import postgres from "postgres"
import config from "$lib/server/config"
let sql: ReturnType<typeof postgres> | null = null
export async function dbConnect() {
try {
const sql = postgres({
export async function getDbPool() {
if (!sql) {
sql = postgres({
host: config.db.host,
database: config.db.database,
port: config.db.port,
username: config.db.username,
password: config.db.password,
max: 20,
idle_timeout: 30,
connection: {
application_name: "legi-ui",
},
})
}
return sql
} catch (error: unknown) {
if (typeof error === "string") {
console.log(
"An error occured while calling oracledb.createPool:\n",
error,
)
} else if (error instanceof Error) {
console.log(
"An error occured while calling oracledb.createPool:\n",
error.stack,
)
}
throw error
export async function closeDbPool() {
if (sql) {
await sql.end()
sql = null
}
}
......@@ -10,10 +10,10 @@ export const GET: RequestHandler = async ({ params, locals }) => {
const { id } = params as { id: string }
const { sql } = locals
const dbConnection = await sql.reserve()
switch (true) {
case id.startsWith("LEGITEXT"): {
const dbConnection = await sql.reserve()
const structDataFromDb: queryDataStructData[] = await dbConnection<JSON>`
select jsonb_path_query(data, '$.STRUCT') AS struct_data
from textelr
......@@ -35,6 +35,8 @@ export const GET: RequestHandler = async ({ params, locals }) => {
}
}
case id.startsWith("LEGISCTA"): {
const dbConnection = await sql.reserve()
const structDataFromDb: queryDataStructData[] = await dbConnection<JSON>`
select jsonb_path_query(data, '$.STRUCTURE_TA') AS struct_data
from section_ta
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment