Erlang boss_db hello world?
-
02-07-2021 - |
Question
Recently I tried to install and run a demo of boss_db ORM for Erlang.
Here is what I did:
- clone the repository
cd boss_db/
rebar get-deps
put a simple mydb.erl file into
src/
:-module(mydb). -compile(export_all). start() ->
DBOptions = [{adapter, pgsql}, {db_host, "localhost"}, {db_port, 5432}, {db_username, "postgres"}, {db_password, "mypass"},
{cache_enable, false}, {cache_exp_time, 0}],boss_db:start(DBOptions).
rebar compile
cd ebin/
- run
erl
mydb:start()
Here is what I get:
** exception exit: shutdown
Whatam I doing wrong here? How is it supposed to be run?
P.S. I tried to run application:start(boss_db) as well, but the result is the same.
P.P.S. I have read the documentation twice, but I still have no idea how to run the whole thing.
Solution
git clone git://github.com/evanmiller/boss_db.git
cd boss_db/
rebar get-deps
emacs src/mydb.erl
rebar compile
erl -pa ./ebin ./deps/*/ebin
These steps worked for me. code:is_loaded()
will not work right after you run erl. Only when the module is invoked, the code is loaded.
I do get an application shutdown, but it is because the code is trying to connect to a Postgres DB and can't connect to it.
Erlang R15B02 (erts-5.9.2) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9.2 (abort with ^G)
1> mydb:start().
** exception exit: shutdown
=ERROR REPORT==== 9-Oct-2012::12:13:07 ===
** Generic server <0.35.0> terminating
** Last message in was {'EXIT',<0.34.0>,
{{badmatch,
{error,
{{badmatch,
{error,
{{badmatch,{error,econnrefused}},
[{pgsql_sock,init,1,
[{file,"src/pgsql_sock.erl"},{line,51}]},
{gen_server,init_it,6,
[{file,"gen_server.erl"},{line,304}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,227}]}]}}},
[{boss_db_controller,init,1,
[{file,"src/boss_db_controller.erl"},{line,31}]},
{gen_server,init_it,6,
[{file,"gen_server.erl"},{line,304}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,227}]}]}}},
[{poolboy,new_worker,2,
[{file,"src/poolboy.erl"},{line,348}]},
{poolboy,prepopulate,4,
[{file,"src/poolboy.erl"},{line,370}]},
{poolboy,init,2,
[{file,"src/poolboy.erl"},{line,74}]},
{gen_fsm,init_it,6,
[{file,"gen_fsm.erl"},{line,361}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,227}]}]}}
** When Server state == {state,
{<0.35.0>,poolboy_sup},
simple_one_for_one,
[{child,undefined,boss_db_controller,
{boss_db_controller,start_link,
[[{size,5},
{max_overflow,10},
{adapter,pgsql},
{db_host,"localhost"},
{db_port,5432},
{db_username,"postgres"},
{db_password,"mypass"},
{cache_enable,false},
{cache_exp_time,0}]]},
temporary,brutal_kill,worker,
[boss_db_controller]}],
{set,0,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
[]},
{{[],[],[],[],[],[],[],[],[],[],[],[],[],[],
[],[]}}},
0,1,[],poolboy_sup,
{boss_db_controller,
[{size,5},
{max_overflow,10},
{adapter,pgsql},
{db_host,"localhost"},
{db_port,5432},
{db_username,"postgres"},
{db_password,"mypass"},
{cache_enable,false},
{cache_exp_time,0}]}}
** Reason for termination ==
** {{badmatch,
{error,
{{badmatch,
{error,
{{badmatch,{error,econnrefused}},
[{pgsql_sock,init,1,
[{file,"src/pgsql_sock.erl"},{line,51}]},
{gen_server,init_it,6,
[{file,"gen_server.erl"},{line,304}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,227}]}]}}},
[{boss_db_controller,init,1,
[{file,"src/boss_db_controller.erl"},{line,31}]},
{gen_server,init_it,6,[{file,"gen_server.erl"},{line,304}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,227}]}]}}},
[{poolboy,new_worker,2,[{file,"src/poolboy.erl"},{line,348}]},
{poolboy,prepopulate,4,[{file,"src/poolboy.erl"},{line,370}]},
{poolboy,init,2,[{file,"src/poolboy.erl"},{line,74}]},
{gen_fsm,init_it,6,[{file,"gen_fsm.erl"},{line,361}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,227}]}]}
OTHER TIPS
You are not adding dependencies to the code path. That's why an exception is thrown when they are not found. Don't cd into ebin and erl. Instead run
erl -pa ebin -pa deps/*/ebin
from the current directory.
Btw this is not a good way to do this kind of stuff. Instead create a new empty app using rebar. Add boss_db as a dependency to your app in rebar.config. Put your own source files under src. And then
rebar get-deps compile
erl -pa ebin -pa deps/*/ebin