P
Prisma2mo ago
Borcio

Problem with first connection to database

Hi i created cront task app in js and i use prisma in in to save some data and log to database, and i have problem with first connection to database, i set my app to retry tast so i know that when i run same task second time it works fine without error... i added to my connection string ?schema=public&connect_timeout=30&pool_timeout=30&socket_timeout=30 and on app first run i added
const { prisma, initializePrisma } = require("./constants/prisma");
const { ErrorType } = require("./constants/enums");
const dbLogger = require("./utils/dbLogger");

async function startServer() {
try {
await initializePrisma();
const { prisma, initializePrisma } = require("./constants/prisma");
const { ErrorType } = require("./constants/enums");
const dbLogger = require("./utils/dbLogger");

async function startServer() {
try {
await initializePrisma();
and this is my prisma.js
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function initializePrisma() {
await prisma.$connect();
}

module.exports = { prisma, initializePrisma };
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function initializePrisma() {
await prisma.$connect();
}

module.exports = { prisma, initializePrisma };
No description
Solution:
```js if (updatedProductsData.length !== 0) { for (let product of updatedProductsData) { promises.push( prisma.Product.updateMany({...
Jump to solution
11 Replies
Papa Johns
Papa Johns2mo ago
I think it is not able to fetch the db creds from the env
Borcio
Borcio2mo ago
but only on first try of task? in logs i have info when cron start (SyncStock...) on error in abort it and retry and on retry it is working
Papa Johns
Papa Johns2mo ago
can you check if it is connecting to the database
Borcio
Borcio2mo ago
in 1 min it will retry this task so i will show it
Papa Johns
Papa Johns2mo ago
okay it can be that that, the connection is not happening for that particular file or execution you might have to check if the prisma instance is accessible for hat file.
Borcio
Borcio2mo ago
No description
Borcio
Borcio2mo ago
i restated app with 30s task timer on first run error, on retry its working on test with 15m timer it error 2 times so its like connection to bd is to long and it close it and when its short time bt connections it works on second try first connection to DB is too long? and it auto close it when nothing happend for too long?
Papa Johns
Papa Johns2mo ago
import { PrismaClient } from '@prisma/client'

declare global {
// eslint-disable-next-line no-var, no-unused-vars
var cachedPrisma: PrismaClient
}

let prisma: PrismaClient
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if (!global.cachedPrisma) {
global.cachedPrisma = new PrismaClient()
}
prisma = global.cachedPrisma
}

export const db = prisma
import { PrismaClient } from '@prisma/client'

declare global {
// eslint-disable-next-line no-var, no-unused-vars
var cachedPrisma: PrismaClient
}

let prisma: PrismaClient
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient()
} else {
if (!global.cachedPrisma) {
global.cachedPrisma = new PrismaClient()
}
prisma = global.cachedPrisma
}

export const db = prisma
you can try something like this And also check if the db url has required parameters for the connection like keepAlive etc. I think that might help.
Borcio
Borcio2mo ago
k thx, i will try ok i had stupid bug (my fault)
Solution
Borcio
Borcio2mo ago
if (updatedProductsData.length !== 0) {
for (let product of updatedProductsData) {
promises.push(
prisma.Product.updateMany({
// some code
},
})
);
}
}
if (updatedProductsData.length !== 0) {
for (let product of updatedProductsData) {
promises.push(
prisma.Product.updateMany({
// some code
},
})
);
}
}
Borcio
Borcio2mo ago
... I forgot await before prisma ... It's funny that it was only on prod that this error occurred, and locally I tested it a million times and never had this error xd so thx, and can be closed ; )