0

Sorry for my pool Englsih!:-) My code is here: http://pastebin.com/zus6dGdz I just want to use the poolboy as my database connection pools,and i use the mongodb-erlang as my driver to communicate with the mongodb.

After running it,from the shell report,I am sure that the mongodb-erlang has created the pools for me

***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.174.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.178.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.180.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.182.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.184.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.186.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.188.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.190.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.192.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.194.0>

and I use the pools like this: code snap here(in hello_handler.erl,can find it through the the URL above):

get_user(Name, Req) ->
  Collection = <<"user">>,
  Selector = {name, Name},
  Worker = poolboy:checkout(?DB_Conn_Pool),
  io:format("***************************Debug*********************************~n  Module:~p ~n  Line:~p ~n  Worker:~p ~n", [?MODULE, ?LINE, Worker]),
  Request = {get_user, {Collection, Selector}},
  UserInfo = gen_server:call(Worker, Request),
  io:format("***************************Debug*********************************~n  Module:~p ~n  Line:~p ~n  UserInfo:~p ~n", [?MODULE, ?LINE, UserInfo]),
  cowboy_req:reply(200, [
    {<<"content-type">>, <<"text/plain">>}
  ], UserInfo, Req).

And the the worker process will handle the request in the function handle_call/3 in the db_mongo_handler.erl

code snap:

handle_call({get_user, {Collection, Selector}}, _From, #state{connection = Connection} = State) ->
  io:format("***************************Debug*********************************~n
  Module:~p ~n  Line:~p ~n   Handle_call Connection:~p ~n Collection:~p ~n
  Selector:~p ~n Connection is is_pid()? ~p ~n Collection is binary()? ~p ~n",
    [?MODULE, ?LINE, Connection, Collection, Selector, is_pid(Connection), is_binary(Collection)]),
  Cursor = mongo:find(Connection, Collection, Selector),
  io:format("***************************Debug*********************************~n Module:~p ~n  Line:~p ~n
  Cursor:~p ~n", [?MODULE, ?LINE, Cursor]),
  Result = mc_cursor:rest(Cursor),
  [Head | _] = Result,
  {_, _, _, NameValue, _, AgeValue} = Head,
  io:format("***************************Debug*********************************~n Module:~p ~n  Line:~p ~n
  NameValue:~p ~n  AgeValue:~p ~n ", [?MODULE, ?LINE, NameValue, AgeValue]),
  BackData = binary_to_list(<<NameValue/bits, <<":">>/bits, AgeValue/bits>>),
  {reply, BackData, State}.

However,it throw the error from this code:

Cursor = mongo:find(Connection, Collection, Selector),

And the error report is :

***************************Debug*********************************

  Module:db_mongo_handler
  Line:104
   Handle_call Connection:<0.194.0>
 Collection:<<"user">>

  Selector:{name,<<"three">>}
 Connection is is_pid()? true
 Collection is binary()? true

=ERROR REPORT==== 4-Feb-2015::14:48:39 ===
** Generic server <0.194.0> terminating
** Last message in was {query,false,false,false,false,<<"user">>,0,0,
                              {name,<<"zhk">>},
                              []}
** When Server state == {state,#Port<0.2666>,
                               {dict,0,16,16,8,80,48,
                                     {[],[],[],[],[],[],[],[],[],[],[],[],[],
                                      [],[],[]},
                                     {{[],[],[],[],[],[],[],[],[],[],[],[],[],
                                       [],[],[]}}},
                               <<>>,
                               {conn_state,unsafe,master,"user"}}
** Reason for termination ==
** {function_clause,
       [{mongo_protocol,binarize,
            ["user"],
            [{file,"src/core/mongo_protocol.erl"},{line,108}]},
        {mongo_protocol,dbcoll,2,
            [{file,"src/core/mongo_protocol.erl"},{line,44}]},
        {mongo_protocol,put_message,3,
            [{file,"src/core/mongo_protocol.erl"},{line,74}]},
        {mc_worker_logic,'-encode_requests/2-fun-0-',3,
            [{file,"src/connection/mc_worker_logic.erl"},{line,22}]},
        {lists,foldl,3,[{file,"lists.erl"},{line,1261}]},
        {mc_worker_logic,make_request,3,
            [{file,"src/connection/mc_worker_logic.erl"},{line,73}]},
        {mc_worker,handle_call,3,
            [{file,"src/connection/mc_worker.erl"},{line,75}]},
        {gen_server,try_handle_call,4,[{file,"gen_server.erl"},{line,607}]}]}

I have try this code in erlang shell,the

Cursor = mongo:find(Connection, Collection, Selector),

can run successfully,and i have check the mongodb,the variable type,but finally i have no idea how to solve this problem.Please help me !!:->

1 Answer 1

1

The problem seems to be related to the conn_state record,

-record(conn_state, {
    write_mode = unsafe :: write_mode(),
    read_mode = master :: read_mode(),
    database :: database()
}).

-type database() :: binary | atom().

And you have:

{conn_state,unsafe,master,"user"}

where user is a string() not a binary or atom.

The mongo_protocol:binarize/1 function only accepts params as binaries or atoms.

Change in hello_erlang.app.src the database value to:

{database, user},

or {database, <<"user">>},

Regards

2
  • Yes,you are right,and when i encounter the problem.I alway wonder the meaning of the report.1.Such as this time,can you tell me what's the meaning of the {conn_state,unsafe,master,"user"} ,the meaning of the conn_state,,the meaning of the unsafe,the meaning of the master ???2.And where can i find such meaning of the report?www.erlang.org???? Feb 4, 2015 at 8:37
  • Read de mongodb-erlang driver source code to know de meaning of diferent parameters.
    – mdaguete
    Feb 4, 2015 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.