0

我在使用 gen_server、supervisor 和 mnesia 时遇到问题。我有主管:http ://pastebin.com/8rkfrq7D ,它会触发正在启动 mnesia 的服务器模块。我的问题是当我写

erl
c(superv).
superv:start_link().
//it opens fine
C^
erl
c(superv).
superv:start_link().
    ** exception exit: shutdown
//if i try again start_link() it is working

我懂了。我删除了负责启动 mnesia 的部分,它运行良好,所以我希望即时退出(通过 ctrl+c)不会正确关闭 mnesia。不幸的是,即使我调用 mnesia:stop(),在快速调用 start_link() 之前,它也会返回异常退出。请帮我解决这个问题。

4

1 回答 1

1

The not entirely right way to start mnesia is as an application.

application:start(mnesia).

before you start your application. It can be used when you are developing your system. For a real deployment, you want to generate a release with a boot-script. A release is a self-contained Erlang system you can start up on a foreign machine. You will write your own application, write a my_application_name.app file which contains a dependency on mnesia. Then you want to generate a release, typically with reltool and this release will then initialize by starting up mnesia before starting my_application_name. At least this is the real way to do it.

The tool like rebar can help you with maintaining your application and a reltool.config file for building your release.

Note that Mnesia needs a schema before it can start. A common trick is to have your release contain a default empty database which gets installed such that mnesias dir parameter points to it. Thus, if you start a newly generated system, it has a database to start from. And you can restart from scratch by re-installing the empty database. Check out FALLBACK.BUP in mnesia for hints on how to do this.

As for your errors, you can't start your server twice. The first time around, it registers itself under the atom server so a subsequent restart when it is already running will crash it. You can sometimes get a hint if you boot Erlang with the SASL application enabled. Either execute application:start(sasl) or run erlang like so:

erl -boot start_sasl

which substitutes the normal boot script with a variant that also starts SASL.

于 2012-12-29T22:28:31.197 回答