Packages
db_connection
2.4.2
2.10.2
2.10.1
2.10.0
2.9.0
2.8.1
2.8.0
2.7.0
2.6.0
2.5.0
2.4.3
2.4.2
2.4.1
2.4.0
2.3.1
2.3.0
2.2.2
2.2.1
2.2.0
2.1.1
2.1.0
2.0.6
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-rc.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-rc.5
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Database connection behaviour for database transactions and connection pooling
Current section
Files
Jump to
Current section
Files
lib/db_connection/task.ex
defmodule DBConnection.Task do
@moduledoc false
@name __MODULE__
require DBConnection.Holder
def run_child(mod, state, fun, opts) do
arg = [fun, self(), opts]
{:ok, pid} = Task.Supervisor.start_child(@name, __MODULE__, :init, arg)
ref = Process.monitor(pid)
_ = DBConnection.Holder.update(pid, ref, mod, state)
{pid, ref}
end
def init(fun, parent, opts) do
try do
Process.link(parent)
catch
:error, :noproc ->
exit({:shutdown, :noproc})
end
receive do
{:"ETS-TRANSFER", holder, ^parent, {:checkin, ref, _extra}} ->
Process.unlink(parent)
pool_ref = DBConnection.Holder.pool_ref(pool: parent, reference: ref, holder: holder)
checkout = {:via, __MODULE__, pool_ref}
_ = DBConnection.run(checkout, make_fun(fun), [pool: __MODULE__] ++ opts)
exit(:normal)
end
end
def checkout({:via, __MODULE__, pool_ref}, _callers, _opts) do
{:ok, pool_ref, _mod = :unused, _idle_time = nil, _state = :unused}
end
defp make_fun(fun) when is_function(fun, 1) do
fun
end
defp make_fun(mfargs) do
fn conn ->
{mod, fun, args} = mfargs
apply(mod, fun, [conn | args])
end
end
end