Four ways to start mnesia in OTP environment. Which one should one use?

Sergey Samokhin <>
Fri Jul 3 20:43:05 CEST 2009


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(_) ->

[-] 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?


Sergey Samokhin

More information about the erlang-questions mailing list