4
votes

I am currently using Prisma on a large scale project. When performing a complex query, the Prisma cluster often errors out with the following message in the docker-logs (I'm redacting the error for readability):

    {"key":"error/unhandled","requestId":"local:ck7ditux500570716cl5f8x3r","clientId":"default$default","payload":{"exception":"java.util.concurrent.RejectedExecutionException: Task slick.basic.BasicBackend$DatabaseDef$$anon$3@552b85a4 rejected from slick.util.AsyncExecutor$$anon$1$$anon$2@1d4391f7[Running, pool size = 9, active threads = 9, queued tasks = 1000, completed tasks = 43440]","query":"query ($where: TaskWhereUniqueInput!)
{\n  task(where: $where) {\n    workflow {\n      tasks {\n        id\n        state\n        parentReq\n        frozen\n        parentTask {\n          id\n          state\n        }\n      }\n    }\n  }\n}\n","variables":
"{\"where\":{\"id\":\"ck6twx873bs550874ne867066\"}}",
"code":"0","stack_trace":"java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)\\n
java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
...
"message":"Task slick.basic.BasicBackend$DatabaseDef$$anon$3@552b85a4 rejected from slick.util.AsyncExecutor$$anon$1$$anon$2@1d4391f7[Running, pool size = 9, active threads = 9, queued tasks = 1000, completed tasks = 43440]"}}

This is a frequent error when handling large queries. Has anyone come up with a way to configure Prisma or do internal batch operations to avoid this concurrency error?

1

1 Answers

1
votes

Option 1:

I ran into this issue when running a prisma mutation in a loop with a lot of data which seemed to have created too many concurrent database operations.

My solution was to throttle the requests:

thingToLoop.map(() => {
  await new Promise(resolve => setTimeout(resolve, 1000));
  // Prisma operation here. Essentially this just makes the operation wait for one second between each operation.
})

Option 2

I've read other posts that say that you can set a limit to the number of concurrent connections as well.

See: https://v1.prisma.io/docs/1.25/prisma-server/database-connector-POSTGRES-jgfr/#overview

Essentially, this restricts the number of connections when too many are being created. See the following prisma config as an example:

PRISMA_CONFIG: |
  managementApiSecret: __YOUR_MANAGEMENT_API_SECRET__
  port: 4466
  databases:
    default:
      connector: postgres
      migrations: __ENABLE_DB_MIGRATIONS__
      host: __YOUR_DATABASE_HOST__
      port: __YOUR_DATABASE_PORT__
      user: __YOUR_DATABASE_USER__
      password: __YOUR_DATABASE_PASSWORD__
      connectionLimit: __YOUR_CONNECTION_LIMIT__