-1

英语不好,请见谅!!!!我使用poolboy作为我的数据库连接池,我已经阅读了github上的README.md:https://github.com/devinus/poolboy 最后我不知道在哪里当我想要它启动时,我已经启动了 poolboy,然后我得到一个错误:already_started

我的项目文件: http: //pastebin.com/zus6dGdz 我使用cowboy 作为我的http 服务器,但你可以忽略它。

我这样启动程序: 1.我使用 rebar 编译 $rebar clean & make 2.然后我使用 erl 运行我的程序 $ erl -pa ebin/ -pa deps/*/ebin -s start server_start 但是我得到如下错误:

=CRASH REPORT==== 3-Feb-2015::17:47:27 ===
  crasher:
    initial call: poolboy:init/1
    pid: <0.171.0>
    registered_name: []
    exception exit: {{badmatch,{error,{already_started,<0.173.0>}}},
                     [{poolboy,new_worker,1,
                               [{file,"src/poolboy.erl"},{line,260}]},
                      {poolboy,prepopulate,3,
                               [{file,"src/poolboy.erl"},{line,281}]},
                      {poolboy,init,3,[{file,"src/poolboy.erl"},{line,143}]},
                      {gen_server,init_it,6,
                                  [{file,"gen_server.erl"},{line,306}]},
                      {proc_lib,init_p_do_apply,3,
                                [{file,"proc_lib.erl"},{line,237}]}]}
      in function  gen_server:init_it/6 (gen_server.erl, line 330)
    ancestors: [hello_erlang_sup,<0.66.0>]
    messages: []
    links: [<0.172.0>,<0.173.0>,<0.170.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 610
    stack_size: 27
    reductions: 205
  neighbours:
    neighbour: [{pid,<0.173.0>},
                  {registered_name,db_mongo_handler},
                  {initial_call,{db_mongo_handler,init,['Argument__1']}},
                  {current_function,{gen_server,loop,6}},
                  {ancestors,[<0.172.0>,mg_pool1,hello_erlang_sup,<0.66.0>]},
                  {messages,[]},
                  {links,[<0.172.0>,<0.174.0>,<0.171.0>]},
                  {dictionary,[]},
                  {trap_exit,false},
                  {status,waiting},
                  {heap_size,233},
                  {stack_size,9},
                  {reductions,86}]

请帮忙解决问题!这!

4

1 回答 1

4

您正在启动一个由 10 个具有相同注册名称的工人组成的池。当一个进程使用名称注册而另一个进程尝试使用相同名称注册时,您会收到错误消息already_started

在您的示例代码中,poolboy 的工作模块是 db_mongo_handler。Poolboy 尝试通过调用来启动 10 个工人,db_mongo_handler:start_link/1其实现为

start_link(Args) ->
    gen_server:start_link({local, ?SERVER}, ?MODULE, Args, []).

第一个工作人员可以启动,但是当第二个工作人员启动时,它会崩溃already_started

通常,许多类似工人的工人不应该有注册姓名。相反,只有池有名称,当您需要工人时,您要求 poolboypid()使用poolboy:checkout(mg_pool1).

要修复代码,请更改gen_server:start_link({local, ?SERVER}, ?MODULE, Args, [])gen_server:start_link(?MODULE, Args, []). 那么它就不会被注册一个名字。

于 2015-02-03T22:09:01.303 回答