[erlang-questions] Four ways to start mnesia in OTP environment. Which one should one use?

Paul Mineiro <>
Mon Jul 6 03:20:08 CEST 2009


I ended up going with #4, included application, on a large project and was
satisfied.  The mnesia bits were open sourced, you can check out
http://code.google.com/p/schemafinder/ for details.  The main reason for
going with included application was to be able to ensure that nodes were
joining the distributed schema in a reasonable state.

We also started our nodes with a shell script that checked all sorts of
preconditions before invoking erl.  That seems a fact of life to me under
all scenarios (e.g., where are you putting standard out and standard error
from the emulator?).

For a simpler hobby project I'm working on starting mnesia as an app has
been working fine, mnesia is really good about letting you do all sorts of
manipulations while it is running so letting it start on it's own is not
that limiting.

-- p

On Fri, 3 Jul 2009, Sergey Samokhin wrote:

> Hello.
>
> I've found four ways to start mnesia in OTP environment. Which one
> should I use? I was a bit puzzled to decide which of them is better.
>
> 1) Boot scripts
>
> It seems to be the default approach when it comes to making sure that
> all the dependencies of your app will be started.
>
> But there are some disadvantages which make it quite difficult to use
> boot scripts as some kind of "dependencies controller":
>
> [-] Runtime doesn't give you a standard way to prepare the system
> before a dependency has been started. E.g. it isn't possible for me to
> ensure that a directory pointed by "-mnesia dir" is really exist
> before mnesia has been started. To make sure that the appropriate
> directory exists I have to move the corresponding code to my
> escript-based CLI. I don't think a developer will be happy to know
> that such a part of code as checks is no longer part of an Erlang
> Application. This code can't be executed if the application is started
> manually under the fresh erl-shell.
> [-] If you use a boot script you have to generate a new one for every
> OTP release your application is supposed to work with.
> [-] Mnesia isn't automatically stopped after you application has been
> terminated. I'm not sure if it's a problem, but if one is going to
> start another application which is supposed to use mnesia in the same
> VM, it's a good idea to restart mnesia with a fresh schema generated.
>
> 2) erl -run mnesia
>
> To start mnesia one can also decide to use either "-run" or "-s"
> parameters to erl. Disadvantages of this approach are the same as of
> the previous one except that in this case you don't have to regenerate
> boot scripts every time you upgrade your erlang package (because there
> is no any boot scripts =).
>
> 3) From init/1 function of a gen_server using mnesia.
>
> You just write something like this to control when mnesia is started:
>
> init(_) ->
>     mnesia:start(),
>     %...
>
> [-] It seems a good solution, but you can't stop mnesia in terminate/1
> function (mnesia:stop() call hangs). It seems a bit inconsistent to
> leave mnesia running after your application has been stopped.
>
> 4) Start mnesia as included application
>
> Although how to work with Included Applications is described in OTP
> Design Principles I don't see this way being used widely. Maybe
> because of the following questions:
>
> [-] Is it safe to start mnesia as included application by starting
> mnesia_sup:start/0 from standard OTP supervsor?
> [-] Is it safe to set mnesia options (like dir) using set_env before
> your_sup:init() has been returned?
>
> Although I've noted only disadvantages (it's more difficult for me to
> estimate advantages of those ways right now) I try to use them all.
> Which one do you prefer and why?
>
> Thanks.
>
> --
> Sergey Samokhin
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>



More information about the erlang-questions mailing list