dwyl / dwyl/learn-phoenix

** (DBConnection.OwnershipError) cannot find ownership process for #PID

Open
#128 5 comments 3 reactions 0 assignees View on GitHub
chore help wanted technical
Dominant language
No language data
Stars
664
Forks
41
PR merge metrics
No merged PRs in 30d

Description

When attempting to create/run a test that inserts data:

```elixir
defmodule UseragentTest do
use ExUnit.Case
alias Hits.Useragent

test "Useragent.insert" do
useragent = "Mozilla/5.0 (Windows; Windows NT 5.0; rv:1.1) Gecko/20020826"
ip = "127.0.0.1"
useragent_id = Useragent.insert(%Useragent{name: useragent, ip: ip})

IO.inspect(useragent_id, label: "useragent_id")
end

end
```
attempting to run it with the command:
```
mix test test/useragent_test.exs
```
I get the following error:

```
1) test Useragent.insert (UseragentTest)
test/useragent_test.exs:8
** (DBConnection.OwnershipError) cannot find ownership process for #PID<0.390.0>.

When using ownership, you must manage connections in one
of the four ways:

* By explicitly checking out a connection
* By explicitly allowing a spawned process
* By running the pool in shared mode
* By using :caller option with allowed process

The first two options require every new process to explicitly
check a connection out or be allowed by calling checkout or
allow respectively.

The third option requires a {:shared, pid} mode to be set.
If using shared mode in tests, make sure your tests are not
async.

The fourth option requires [caller: pid] to be used when
checking out a connection from the pool. The caller process
should already be allowed on a connection.

If you are reading this error, it means you have not done one
of the steps above or that the owner process has crashed.

See Ecto.Adapters.SQL.Sandbox docs for more information.
code: useragent_id = Useragent.insert(%Useragent{name: useragent, ip: ip})
stacktrace:
(ecto_sql) lib/ecto/adapters/sql.ex:602: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto_sql) lib/ecto/adapters/sql.ex:538: Ecto.Adapters.SQL.execute/5
(ecto) lib/ecto/repo/queryable.ex:153: Ecto.Repo.Queryable.execute/4
(ecto) lib/ecto/repo/queryable.ex:18: Ecto.Repo.Queryable.all/3
(ecto) lib/ecto/repo/queryable.ex:67: Ecto.Repo.Queryable.one/3
(hits) lib/hits/useragent.ex:31: Hits.Useragent.insert/1
test/useragent_test.exs:11: (test)
```

Obviously the `Useragent.insert` works when I run the app `mix phx.server` ...
But the test does not run because it does not have ownership of the connection ... 😞

Contributor guide

Open the contributing guide

Research direction

Start by running mix test test/useragent_test.exs and inspect test/useragent_test.exs alongside Hits.Useragent.insert/1 in lib/hits/useragent.ex. Read the Ecto.Adapters.SQL.Sandbox documentation to understand the missing ownership setup. Done means the test inserts the data without DBConnection.OwnershipError.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir
Domain
database, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.