私は gen_server を書いていますが、これは単に gen_server_db と呼びますが、それほど特別なことは何もありません。使用するライブラリ (emysql) がデータベース サーバーを検索しようとしているときに (gen_server_db:init() で) 接続エラーが発生する可能性があります。例外をキャッチしてコンソールに何かを出力しようとすると、zip が表示されます。以下のコード サンプルの io:format メッセージはいずれも、コンソールに到達することはありません。ずっと前にこの理由を聞いたことを覚えているようですが、理由は思い出せません。
init(Args) ->
% I've condensed the way I actually get this stuff to one line, but if I have a
% database online I connect properly, so I know that I'm getting the Host, User, etc.
{Host, User, Password, DB, PoolSize} = application:get_env(gen_server_db, config),
init_mysql_connection(gen_server_db_pool, PoolSize, User, Password, Host, DB),
% When the net connection to the db is down, I never get here.
ok.
init_mysql_connection(bgo_db_pool, PoolSize, User, Password, Host, DB) ->
try
emysql:add_pool(bgo_db_pool, PoolSize, User, Password, Host, 3306, DB, utf8)
catch
exit:failed_to_connect_to_database ->
io:format("Cannot connect to the mysql database server. Retrying in 1 sec.~n"),
timer:sleep(1000),
init_mysql_connection(bgo_db_pool, PoolSize, User, Password, Host, DB);
Error:Reason ->
io:format("Database connection error: ~p:~p~n", [Error, Reason]),
1/0
end.