2017-06-22 11 views
1

私はpostgresデータベースに接続し、クエリを実行して切断する方法を理解しようとしています。Postgrex接続を切断するには?

Postgrexを見て、私は

{:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres") 

を使用して接続を確立それから私は

Postgrex.query!(pid, "SELECT user_id, text FROM comments", []) 

を使用して、私のクエリを実行しかし、その後、どのように私は抜いていますか?

私はN個のデータベースをループしており、すべてのデータベースで同じクエリを実行しているため、接続を切断したいと考えています。

プロセスを終了しようとしました。 Process.exit(pid, :bye)でも、それはstart_link/3で始まっているため、産卵プロセスも終了します。 Postgrexにはstart/3の機能が見つかりません。

答えて

3

Postgrex.start_linkによって返さpidGenServerであるとして、あなたはGenServer.stop/1使用することができます。

iex(1)> {:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres") 
{:ok, #PID<0.277.0>} 
iex(2)> GenServer.stop(pid)                         
:ok 
iex(3)> GenServer.stop(pid) 
** (exit) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started 
    (stdlib) proc_lib.erl:797: :proc_lib.stop/3 
+0

感謝を!このようにpostgrexの実装の詳細に依存する危険はありますか? –

関連する問題