elixir-ecto / db_connection

Database connection behaviour
http://hexdocs.pm/db_connection/DBConnection.html
306 stars 113 forks source link

Connection Error when exists concurrency inside a transaction. #245

Closed williamff11 closed 3 years ago

williamff11 commented 3 years ago

Hello! I have a Task.async_stream() called inside of a Multi.run. When this Task.async_stream() is called, a error is hurled:

** (DBConnection.ConnectionError) could not checkout the connection owned by #PID<0.897.0>. When using the sandbox, connections are shared, so this may imply another process is using a connection. Reason: connection not available and request was dropped from queue after 931ms. You can configure how long requests wait in the queue using :queue_target and :queue_interval. See DBConnection.start_link/2 for more information.

But, when I call this Task.async_stream() outside Multi.run, the code runs successfully.

josevalim commented 3 years ago

What is your Elixir and db_connection versions?

williamff11 commented 3 years ago

Elixir: 1.10.4 db_connection: 2.3.1

josevalim commented 3 years ago

Ok, so the reason this is happening is because the sandbox is a single connection shared by all of your task async streams. So if you have a lot of task async stream, they will fight over the same connection and eventually they will time out checking it out. You can increase the queue_target and queue_interval for your test environment in your repo configuration OR reduce the task async stream concurrency during test (or both).