簡體   English   中英

在 Next.js 應用程序中使用 Prisma 播種和 Heroku Postgresql 數據庫超時獲取新連接

[英]Seeding using Prisma in Next.js app with a Heroku Postgresql db Timed out fetching a new connection

我嘗試了幾種方法來使用我擁有的 js 文件為我的數據庫播種。

數據.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 }

我查看了多種資源,但我更加面向前端並且努力弄清楚如何調整連接限制或其他方式讓我的種子工作。

有人可以幫我讓這顆種子發揮作用嗎?

據我了解 prisma 的文檔,每個查詢( create / delete / createMany等)都是在單獨的連接上執行的(對於您的場景,有 17 個可用連接)。 當它們排隊並且某些查詢花費的時間超過timeout秒時,隊列中的所有查詢最終都會超時。

prisma 文檔中有一些關於如何優化連接池以便查詢的有用技巧 特別是對於播種,您可以禁用池超時以允許 prisma 處理隊列中的所有查詢而不會超時。

但是,我強烈建議使用createMany一次創建數百條記錄。 但有時這是不可能的(比如您試圖一次將游戲記錄連接到多個會議記錄)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM