Here is the relevent portion of my code
N = node(),
%set MNesia dir
application:set_env(mnesia, dir, "/var/mnesia/"),
%dont check status here, OK if already exists
%stop if running, can't create schema if it is
mnesia:stop(),
%erlang:display(mnesia:delete_schema([N])),
mnesia:create_schema([N]),
%start MNesia, assert it works
ok = mnesia:start(), %start MNesia, bomb if alreay started, should not happen
lager:info("Mnesia started"),
%erlang:display(mnesia:delete_table(application)),
case mnesia:create_table(application, [{attributes, record_info(fields, application)}, {disc_copies, [N]}]) of
{aborted,{already_exists,application}} ->
lager:info("Application table already exists");
{atomic,ok} ->
lager:info(io_lib:format("Created application table on ~s", [N]))
end,
erlang:display(mnesia:table_info(application, all)),
{atomic, Apps} = mnesia:transaction(fun() -> mnesia:match_object(application, {application, '_', '_', '_', '_', '_', '_', '_' , '_', '_'}, read) end),
erlang:display(Apps),
When I start my application for the very first time, everything is ok:
14:34:57.736 [info] Mnesia started
14:34:57.736 [info] Application mnesia started on node cdapbroker@6371550eb22c
[{access_mode,read_write},{active_replicas,['cdapbroker@6371550eb22c']},{all_nodes,['cdapbroker@6371550eb22c']},{arity,10},{attributes,[appname,apptype,namespace,healthcheckurl,metricsurl,url,connectionurl,serviceendpoints,creationtime]},{checkpoints,[]},{commit_work,[]},{cookie,{{1489761297736523772,-576460752303422911,1},'cdapbroker@6371550eb22c'}},{cstruct,{cstruct,application,set,[],['cdapbroker@6371550eb22c'],[],[],0,read_write,false,[],[],false,application,[appname,apptype,namespace,healthcheckurl,metricsurl,url,connectionurl,serviceendpoints,creationtime],[],[],[],{{1489761297736523772,-576460752303422911,1},'cdapbroker@6371550eb22c'},{{2,0},[]}}},{disc_copies,['cdapbroker@6371550eb22c']},{disc_only_copies,[]},{external_copies,[]},{frag_properties,[]},{index,[]},{index_info,{index,set,[]}},{load_by_force,false},{load_node,'cdapbroker@6371550eb22c'},{load_order,0},{load_reason,{dumper,create_table}},{local_content,false},{majority,false},{master_nodes,[]},{memory,298},{ram_copies,[]},{record_name,application},{record_validation,{application,10,set}},{size,0},{snmp,[]},{storage_properties,[]},{storage_type,disc_copies},{subscribers,[]},{type,set},{user_properties,[]},{version,{{2,0},[]}},{where_to_commit,[{'cdapbroker@6371550eb22c',disc_copies}]},{where_to_read,'cdapbroker@6371550eb22c'},{where_to_wlock,{['cdapbroker@6371550eb22c'],false}},{where_to_write,['cdapbroker@6371550eb22c']},{wild_pattern,{application,'_','_','_','_','_','_','_','_','_'}}]
14:34:57.802 [info] Created application table on cdapbroker@6371550eb22c
[]
When I STOP and then START my application again, It shows the application table details but then in the next line claims it doesn't exist!
14:36:44.168 [info] Mnesia started
14:36:44.168 [info] Application mnesia started on node cdapbroker@6371550eb22c
[{access_mode,read_write},{active_replicas,[]},{all_nodes,['cdapbroker@6371550eb22c']},{arity,10},{attributes,[appname,apptype,namespace,healthcheckurl,metricsurl,url,connectionurl,serviceendpoints,creationtime]},{checkpoints,[]},{commit_work,[]},{cookie,{{1489761297736523772,-576460752303422911,1},'cdapbroker@6371550eb22c'}},{cstruct,{cstruct,application,set,[],['cdapbroker@6371550eb22c'],[],[],0,read_write,false,[],[],false,application,[appname,apptype,namespace,healthcheckurl,metricsurl,url,connectionurl,serviceendpoints,creationtime],[],[],[],{{1489761297736523772,-576460752303422911,1},'cdapbroker@6371550eb22c'},{{2,0},[]}}},{disc_copies,['cdapbroker@6371550eb22c']},{disc_only_copies,[]},{external_copies,[]},{frag_properties,[]},{index,[]},{index_info,{index,set,[]}},{load_by_force,false},{load_node,unknown},{load_order,0},{load_reason,unknown},{local_content,false},{majority,false},{master_nodes,[]},{memory,undefined},{ram_copies,[]},{record_name,application},{record_validation,{application,10,set}},{size,undefined},{snmp,[]},{storage_properties,[]},{storage_type,disc_copies},{subscribers,[]},{type,set},{user_properties,[]},{version,{{2,0},[]}},{where_to_commit,[]},{where_to_read,nowhere},{where_to_write,[]},{wild_pattern,{application,'_','_','_','_','_','_','_','_','_'}}]
14:36:44.169 [info] Application table already exists
14:36:44.178 [error]
Error Stacktrace:
application_master:start_it_old/4 line 273
cdapbroker_app:start/2 line 65
error:{badmatch,{aborted,{no_exists,application}}}
14:36:44.179 [error] CRASH REPORT Process <0.145.0> with 0 neighbours exited with reason: {badmatch,{aborted,{no_exists,application}}} in application_master:init/4 line 134
14:36:44.179 [info] Application cdapbroker exited with reason: {badmatch,{aborted,{no_exists,application}}}
Eshell V8.2.1 (abort with ^G)
I'm trying to have my table persist after stop start but I don't understand the above.
This was a timing problem. I think the MNesia error could be more specific..
I had to add:
ok = mnesia:wait_for_tables([application], 30000),
I guess when the table is created (on first start) the table is available immediately, but after a stop/start it wasn't "ready yet".