【发布时间】:2018-08-13 12:22:49
【问题描述】:
有没有人在用erlang odbc查询的时候遇到过这个错误:
{:error, :process_not_owner_of_odbc_connection}
我正在编写一个连接池,并且 :ODBC.connect('conn string) 在 genserver 内运行,并且返回的 pid 进入状态...但是当我从 gen 服务器中获取该 PID 时,尝试使用该 pid 运行 :odbc.param_query 时出现上述错误...如果我们可以使用在 genserver 中创建的 pid,我们应该如何创建连接池?
有人有什么想法吗?
GenServer:
defmodule ItemonboardingApi.ConnectionPoolWorker do
use GenServer
def start_link([]) do
IO.puts "Starting Connection Pool"
GenServer.start_link(__MODULE__, nil, [])
end
def init(_) do
:odbc.start
{ok, pid} = :odbc.connect('DSN=;UID=;PWD=', [])
IO.inspect pid
{:ok, pid}
end
def handle_call(:get_connection, _from, state) do
IO.puts "Inspecting State:"
IO.inspect state
IO.puts("process #{inspect(self())} getting odbc connection")
{:reply, state, state}
end
end
调用函数:
def get_standards_like(standard) do
:poolboy.transaction(
:connection_pool,
fn pid ->
connection = GenServer.call(pid, :get_connection)
IO.inspect connection
val =
String.upcase(standard)
|> to_char_list
# Call Stored Proc Statement
spstmt = '{call invlibr.usp_vm_getStandards (?)}'
case :odbc.param_query(connection, spstmt, [
{{:sql_char, 50}, [val]}
])
|> map_to_type(ItemonboardingCore.Standard)
do
{:ok, mapped} ->
{:ok, mapped}
{:updated, affected_count} ->
{:ok, affected_count}
{:executed, o, a} ->
{:ok, o, a}
{:error, _} ->
{:bad}
end
end
)
end
我已确认 genserver 中的 pid 感染了正确的 odbc pid,并且 GenServer.Call 返回其中一个。
****编辑**** 这是我为解决此问题所做的工作。我不知道创建连接的进程必须是运行查询的进程。对我的工作人员进行细微更改以将查询传递给已解决了我的问题。这是一个艰难的第一步,还有一些事情需要对工人做。
defmodule ItemonboardingApi.ConnectionPoolWorker do
use GenServer
def start_link([]) do
IO.puts "Starting Connection Pool"
GenServer.start_link(__MODULE__, nil, [])
end
def init(_) do
:odbc.start(:app)
{ok, pid} = :odbc.connect('DSN=;UID=;PWD=', [])
IO.inspect pid
{:ok, pid}
end
def handle_call(:get_connection, _from, state) do
{:reply, state, state}
end
def handle_call({:query, %{statement: statement, params: params}}, _from, state) do
#TODO Check if pid is alive and start if needed.
{:reply, :odbc.param_query(state, to_charlist(statement), params), state}
end
end
【问题讨论】:
-
如果连接更改为新连接,您还需要更新状态,如果无法建立连接,则需要更新状态。这与我所做的类似。
-
@RomanRabinovich 你有例子吗?
-
在下面查看我的答案