在Next.js应用程序中使用Prisma播种,并带有Heroku Postgresql DB,该db超时获取新连接

发布于 2025-02-02 08:38:09 字数 2478 浏览 3 评论 0原文

我尝试了几种方法,用我拥有的JS文件播种我的数据库。

data.ts


    export const scheduleData: {
      homeTeam: number;
      awayTeam: number;
      homeTeamConf: number;
      awayTeamConf: number;
      scheduleDate: string;
      week: number;
      leagueId: string;
    }[] = [
      {
        homeTeam: 51,
        awayTeam: 210,
        homeTeamConf: 4,
        awayTeamConf: 17,
        scheduleDate: "2022-12-10T20:00Z",
        week: 15,
        leagueId: "4",
      },
      {
        homeTeam: 335,
        awayTeam: 342,
        homeTeamConf: 27,
        awayTeamConf: 27,
        scheduleDate: "2022-10-12T23:30Z",
        week: 7,
        leagueId: "4",
      },
      //... and so on for hundreds of objects to be inserted in the db as rows
    ]

这是我正在运行的seed.ts文件和查询:


    import { PrismaClient } from "@prisma/client";
    import { scheduleData } from "./scheduleData";
    
    const prisma = new PrismaClient();
    
    const seedSchedule = async () => {
      await Promise.all(
        scheduleData.map(async (game) => {
          return prisma.game.create({
            data: {
              homeTeam: {
                connect: { id: game.homeTeam },
              },
              awayTeam: {
                connect: { id: game.awayTeam },
              },
              scheduleDate: new Date(game.scheduleDate),
              league: {
                connect: { id: 4 },
              },
              week: game.week,
              conferences: {
                connect: [{ id: game.homeTeamConf }, { id: game.awayTeamConf }],
              },
            },
          });
        })
      );
    };
    
    seedSchedule()
      .catch((e) => {
        console.error(e);
        process.exit(1);
      })
      .finally(async () => {
        await prisma.$disconnect();
      });

这适用于前60个记录,然后我总是收到一条消息:


    Timed out fetching a new connection from the connection pool. More info: http://pris.ly/d/connection-pool (Current connection pool timeout: 10, connection limit: 17)
        at Object.request (./@prisma/client/runtime/index.js:45405:15)
        at async PrismaClient._request (./@prisma/client/runtime/index.js:46301:18)
        at async Promise.all (index 84) {
      code: 'P2024',
      clientVersion: '3.14.0',
      meta: { connection_limit: 17, timeout: 10 }

我已经查看了多个资源,但是我更面向前端和挣扎弄清楚如何调整连接限制或其他方法以使我的种子起作用。

有人可以帮我让这个种子工作吗?

I have tried several ways to seed my database with a js file that I have.

data.ts


    export const scheduleData: {
      homeTeam: number;
      awayTeam: number;
      homeTeamConf: number;
      awayTeamConf: number;
      scheduleDate: string;
      week: number;
      leagueId: string;
    }[] = [
      {
        homeTeam: 51,
        awayTeam: 210,
        homeTeamConf: 4,
        awayTeamConf: 17,
        scheduleDate: "2022-12-10T20:00Z",
        week: 15,
        leagueId: "4",
      },
      {
        homeTeam: 335,
        awayTeam: 342,
        homeTeamConf: 27,
        awayTeamConf: 27,
        scheduleDate: "2022-10-12T23:30Z",
        week: 7,
        leagueId: "4",
      },
      //... and so on for hundreds of objects to be inserted in the db as rows
    ]

this is my seed.ts file and query that i am running:


    import { PrismaClient } from "@prisma/client";
    import { scheduleData } from "./scheduleData";
    
    const prisma = new PrismaClient();
    
    const seedSchedule = async () => {
      await Promise.all(
        scheduleData.map(async (game) => {
          return prisma.game.create({
            data: {
              homeTeam: {
                connect: { id: game.homeTeam },
              },
              awayTeam: {
                connect: { id: game.awayTeam },
              },
              scheduleDate: new Date(game.scheduleDate),
              league: {
                connect: { id: 4 },
              },
              week: game.week,
              conferences: {
                connect: [{ id: game.homeTeamConf }, { id: game.awayTeamConf }],
              },
            },
          });
        })
      );
    };
    
    seedSchedule()
      .catch((e) => {
        console.error(e);
        process.exit(1);
      })
      .finally(async () => {
        await prisma.$disconnect();
      });

This works for about the first 60 records and then i always get a message:


    Timed out fetching a new connection from the connection pool. More info: http://pris.ly/d/connection-pool (Current connection pool timeout: 10, connection limit: 17)
        at Object.request (./@prisma/client/runtime/index.js:45405:15)
        at async PrismaClient._request (./@prisma/client/runtime/index.js:46301:18)
        at async Promise.all (index 84) {
      code: 'P2024',
      clientVersion: '3.14.0',
      meta: { connection_limit: 17, timeout: 10 }

I've looked at multiple resources but i'm more front end oriented and struggling to figure out how to adjust the connection limit or other ways to get my seed to work.

Can someone help me get this seed to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你不是我要的菜∠ 2025-02-09 08:38:09

据我了解,prisma的文档,每个查询(create/delete/createMany等)都在单独的连接上执行(用于您的方案,有17个可用连接)。当它们排队并且某些查询所需的时间长于timeout秒时,队列中的所有查询最终都将超时。

有一些有用的 Prisma文档关于如何优化连接池,以便查询。特别是为了播种,您可以禁用泳池超时
允许Prisma处理队列中的所有查询,而无需过时。

但是,我强烈建议使用createMany一次创建数百个记录。但是有时候是不可能的(就像您试图一次将游戏记录连接到多个会议记录一样)。

As far as I understand the docs of prisma, every query (create/delete/createMany etc.) is performed on a separate connection (for your scenario, there are 17 available connections). When they are queued and some query takes longer than timeout seconds, all the queries in the queue will eventually timeout.

There are some useful tricks in prisma docs on how to optimize the connection pool so the queries. Especially for seeding, you could disable the pool timeout
to allow prisma to process all queries in the queue without timing out.

However, I would highly suggest using createMany to create hundreds of records at once. But sometimes it's just not possible (like you're trying to connect the game record to multiple conference records at once).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文