Entire SectionCodeTo use the mnesia database to implement sequence functions similar to those in Oracle.
The original code is as follows:
% Author: Administrator <br/> % created: 2012-2-16 <br/> % Description: Todo: Add description to id_generator <br/>-module (id_generator ). <br/>-behavior (gen_server ). <br/> % include files <br/> % </P> <p> % <br/> % exported functions <br/ >%% <br/>-Export ([start_link/0, getnewid/1]). <br/>-Export ([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). <br/>-record (IDs, {I Dtype, IDS }). <br/>-record (State ,{}). <br/> % API functions <br/> % <br/> start_link ()-> <br/> gen_server: start_link ({local ,? Module },? Module, [], []) <br/>. </P> <p> Init ([])-> <br/> mnesia: Start (), <br/> IO: Format ("started "), <br/> mnesia: create_schema ([node ()]), <br/> case mnesia: create_table (IDs, [{type, ordered_set}, <br/> {attributes, record_info (fields, IDS) },< br/> {disc_copies, []} <br/>]) of <br/> {atomic, OK}-> <br/> {atomic, OK}; <br/> {error, reason}-> <br/> IO: Format ("CREATE TABLE error ") <br/> end, <br/> {OK, # state {}}< br/>. </P> <p> getne WID (idtype)-> <br/> IO: Format ("Waiting ~ N "), <br/> % case mnesia: wait_for_tables ([tbl_clientid], 5000) of <br/> % OK-> <br/> % gen_server: Call (? Module, {GETID, idtype}); <br/>%{ timeout, _ badlist}-> <br/>%{ timeout, _ badlist }; <br/> % {error, reason}-> <br/>%{ error, reason} <br/> % end <br/> mnesia: force_load_table (IDS ), <br/> gen_server: Call (? Module, {GETID, idtype}) <br/>. </P> <p> % generate new ID with given type <br/> handle_call ({GETID, idtype}, from, state) -> <br/> F = fun ()-> <br/> result = mnesia: Read (IDs, idtype, write ), <br/> case result of <br/> [s]-> <br/> IO: Format ("have ~ N "), <br/> id = s # IDs. IDs, <br/> newclumn = s # IDs {IDs = ID + 1}, <br/> mnesia: Write (IDs, newclumn, write), <br/> ID; <br/> []-> <br/> IO: Format ("None ~ N "), <br/> newclumn = # IDs {idtype = idtype, IDS = 2}, <br/> mnesia: Write (IDs, newclumn, write ), <br/> 1 <br/> end, <br/> case mnesia: transaction (F) of <br/> {atomic, id}-> <br/> {atomic, Id}; <br/> {aborted, reason}-> <br/> IO: Format ("Run transaction error ~ 1000. p ~ N ", [reason]), <br/> id = 0, <br/> _ Els-> <br/> id = 1000 <br/> end, <br/> {reply, ID, State} <br/>. </P> <p> handle_cast (_ from, state)-> <br/> OK. </P> <p> handle_info (_ from, state)-> <br/> OK. </P> <p> terminate (_ from, state)-> <br/> OK. </P> <p> code_change (_ oldver, state, ext)-> <br/> {OK, State }. <br/> % local functions <br/> % </P> <p>
Usage:
Id_generator: start_link ().
Id_generator: getnewid (AAA ).
Summary:
1. Be sure to write the parameter pair when creating the mnsia table
2. Define a table column as a record, and the second field has the same name as the table.
3. The table is not created successfully, or mnesia: wait_for_tables times out when there is a problem. It is best not to use mnesia: wait_for_tables ([mytable], infinity). It is important to end quickly after an error occurs.
4. Make multiple judgments on possible exceptions and handle exceptions.