erlangerlangweb

where start the poolboy privately?erlang database connection pools


英语不好,请见谅!!!! I use the poolboy as my database connection pools,i have read the README.md on the github:https://github.com/devinus/poolboy But at last i do not konw where i have started the poolboy when i want it to start,then i got an error:already_started

My project's files:http://pastebin.com/zus6dGdz I use the cowboy to be my http server,but you can ignore it.

I start the program like this: 1.I use the rebar to compile $rebar clean & make 2.then i use the erl to run my program $ erl -pa ebin/ -pa deps/*/ebin -s start server_start But i got the errors as follows:

=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}]

Please help to solve the problem!Ths!


Solution

  • You are starting a pool of 10 workers with the same registered name. When a process is registered with a name and another process tries to register with the same name, you get the error already_started.

    In your example code, the worker module for poolboy is db_mongo_handler. Poolboy tries to start 10 workers by calling db_mongo_handler:start_link/1 which is implemented as

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

    The first worker can start but when the second worker starts it crashes with already_started.

    Normally the workers of a pool of many similar workers should not have a registered name. Instead, only the pool has a name and when you need a worker, you ask poolboy to deliver a pid() of one of the workers using poolboy:checkout(mg_pool1).

    To fix the code, change gen_server:start_link({local, ?SERVER}, ?MODULE, Args, []) to gen_server:start_link(?MODULE, Args, []). Then it will not be registered with a name.