ConnectionError
Failed to log access: ConnectionError: The session was terminated unexpectedly
0|deno-oak-app | at Connection.#readMessage (https://deno.land/x/postgres@v0.17.0/connection/connection.ts:171:13)
I am getting this error when I am connecting deno to database, I worked fine for 5-10 min then I automatically get this error.
4 Replies
rival-black•2mo ago
Hey @saw_ji, can you try and connect via psql or use the SQL editor on console.neon.tech? Do other connection methods also fail?
typical-coral•2mo ago
I am using deno to connect it, most probaby after sometime it autoamtically disconnects and I got errors in my api
I am using free plan of neon database
Its connect normal everywhere to again there it automatically disconnects
rival-black•2mo ago
I assume you're not using our serverless driver? Regular DB connections should prevent the DB from scaling to zero and should not disconnect. So this is worth debugging to figure out what's happening. Could you share the code snippet (without connection string) that you're using to establish the connection?
typical-coral•2mo ago
db.ts file
import { Pool } from "postgres";
import "jsr:@std/dotenv/load";
import config from './default.ts';
import { log } from "../../utils/logger.ts";
// Get environment from .env
const ENVIRONMENT = Deno.env.get("ENVIRONMENT") || "DEVELOPMENT";
// Select database credentials based on environment
const DB_DETAILS = config.DATABASE; // This will use either development or production DATABASE config
// Database configuration
let dbConfig = {
hostname: DB_DETAILS.HOST_NAME,
port: DB_DETAILS.PORT,
user: DB_DETAILS.USER,
password: DB_DETAILS.PASSWORD,
database: DB_DETAILS.DATABASE,
max: 5,
};
const pool = new Pool(dbConfig, dbConfig.max, true);
export async function connectDB() {
const client = await pool.connect(); // Acquire a client from the pool
try {
console.log(ENVIRONMENT+ " " + dbConfig.database + " Database connected !");
// You can now use the
client
to execute queries
} catch (err) {
console.error("Unexpected error on idle client", err);
await log("error", {
message: "Unexpected error on idle client",
error: err,
});
Deno.exit(1); // Exit the process in case of an error
} finally {
client.release(); // Release the client back to the pool
}
}
// Function to test the database connection using the pool
export async function databaseConnectionTest() {
const client = await pool.connect(); // Acquire a client from the pool
try {
console.log("Database connected successfully!");
return true; // Return true if the connection is successful
} catch (err) {
console.error("Unexpected error on idle client", err);
await log("error", {
message: "Unexpected error on idle client",
error: err,
});
return false; // Return false if there is an error
} finally {
client.release(); // Release the client back to the pool
}
}
// Export the pool for querying
export default pool;
This is my deno code for db.ts file, which makes database connections
I am not using serverlessDriver, I am using regular postgres library