aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2008-03-02 14:10:38 -0500
committerBrian Cully <github.20.shmit@spamgourmet.com>2008-03-02 14:10:38 -0500
commit3aaf4a5fe7782c0ac94d51b6a6c835ed1af37cef (patch)
treec59bac65cb10af774f5bb251723972647af5efb7 /src
parentac2a3d11382d8d97975b9b10ae4c148ef9ae3a5c (diff)
downloadmysqlerl-3aaf4a5fe7782c0ac94d51b6a6c835ed1af37cef.tar.gz
mysqlerl-3aaf4a5fe7782c0ac94d51b6a6c835ed1af37cef.zip
Link with owner and shutdown clean if owner dies.
Diffstat (limited to 'src')
-rw-r--r--src/mysqlerl_connection.erl22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/mysqlerl_connection.erl b/src/mysqlerl_connection.erl
index 4341aaa..a0b3062 100644
--- a/src/mysqlerl_connection.erl
+++ b/src/mysqlerl_connection.erl
@@ -18,10 +18,11 @@ start_link(Owner, Host, Port, Database, User, Password, Options) ->
User, Password, Options], []).
stop(Pid) ->
- gen_server:cast(Pid, stop).
+ gen_server:call(Pid, stop).
init([Owner, Host, Port, Database, User, Password, Options]) ->
process_flag(trap_exit, true),
+ link(Owner),
Cmd = lists:flatten(io_lib:format("~s ~s ~w ~s ~s ~s ~s",
[helper(), Host, Port, Database,
User, Password, Options])),
@@ -40,13 +41,26 @@ terminate(Reason, State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
+handle_call(Request, From, #state{owner = Owner} = State)
+ when Owner /= element(1, From) ->
+ error_logger:warning_msg("Request from ~p (owner: ~p): ~p",
+ [element(1, From), Owner, Request]),
+ {reply, {error, process_not_owner_of_odbc_connection}, State};
+handle_call(stop, _From, State) ->
+ {stop, normal, State};
handle_call(Request, _From, State) ->
{reply, make_request(State#state.ref, Request), State}.
-handle_cast(stop, State) ->
- {stop, normal, State}.
+handle_cast(_Request, State) ->
+ {noreply, State}.
-handle_info({'EXIT', _Ref, Reason}, State) ->
+handle_info({'EXIT', Pid, _Reason}, State)
+ when Pid == State#state.owner ->
+ io:format("DEBUG: owner ~p shut down.~n", [Pid]),
+ {stop, normal, State};
+handle_info({'EXIT', Ref, Reason}, State)
+ when Ref == State#state.ref ->
+ io:format("DEBUG: Port ~p closed on ~p.~n", [Ref, State]),
{stop, #port_closed{reason = Reason}, State}.
helper() ->