首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Next.js应用程序中使用Prisma与进行播种,超时获取一个新连接

在Next.js应用程序中使用Prisma与进行播种,超时获取一个新连接
EN

Stack Overflow用户
提问于 2022-05-28 22:04:20
回答 1查看 514关注 0票数 0

我尝试过几种方法,用我拥有的js文件为我的数据库添加种子。

data.ts

代码语言:javascript
复制
    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文件和查询:

代码语言:javascript
复制
    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张唱片,然后我总是收到一条信息:

代码语言:javascript
复制
    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 }

我看过多种资源,但我更倾向于前端,并且很难找到如何调整连接限制或其他方法来让我的种子工作。

谁能帮我把这颗种子弄到手吗?

EN

回答 1

Stack Overflow用户

发布于 2022-05-29 07:39:53

据我所知,prisma的文档,每一个查询(create/delete/createMany等等)在单独的连接上执行(对于您的场景,有17个可用的连接)。当队列中的某些查询的时间超过timeout秒时,队列中的所有查询最终都会超时。

关于如何优化连接池以便查询,有一些有用的prisma文档中的技巧。特别是对于种子,您可以使用禁用池超时允许prisma处理队列中的所有查询,而不需要超时。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72419564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档