Created
September 3, 2014 12:18
-
-
Save erszcz/f1981124e04cae0d1adc to your computer and use it in GitHub Desktop.
Mnesia Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(mnesia_helper). | |
-compile([export_all]). | |
-record(some_record, {name=default, value=undefined}). | |
setup() -> | |
application:start(sasl), | |
application:start(mnesia), | |
{atomic, ok} = mnesia:create_table(some_record, [{attributes, | |
record_info(fields, some_record)}]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(mnesia_listener). | |
-compile([export_all]). | |
-define(EVENT_TYPE, {table, some_record, simple}). | |
start() -> | |
Pid = spawn(?MODULE, init, []), | |
register(?MODULE, Pid). | |
stop() -> | |
?MODULE ! stop. | |
init() -> | |
mnesia:subscribe(?EVENT_TYPE), | |
loop(). | |
loop() -> | |
receive | |
stop -> | |
mnesia:unsubscribe(?EVENT_TYPE), | |
ok; | |
{mnesia_table_event, {Oper, Record, ActivityId}} -> | |
error_logger:info_msg("Mnesia operation ~p on ~p in activity ~p~n~n", | |
[Oper, Record, ActivityId]), | |
loop(); | |
Msg -> | |
error_logger:info_msg("Unexpected message received: ~p~n~n", [Msg]), | |
loop() | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c(mnesia_helper), c(mnesia_listener). | |
rr(mnesia_helper). | |
mnesia_helper:setup(). | |
mnesia_listener:start(). | |
F = fun() -> mnesia:write(#some_record{name=john, value=doe}) end. | |
mnesia:transaction(F). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment