| Author |
Message |
< Erlang ~ Strange function call failure |
| brett hallett |
Posted: Sun Dec 02, 2007 2:52 am |
|
|
|
User
Joined: 21 Aug 2007
Posts: 21
|
I'm encountering a problem with calling a function from another function
( both reside in the same program).
Observe the account "harry" , below, his unique transaction counter ( table "sequence" ), is 3, I
manually deposit 25 grotnicks , the counter is now 4, his deposit is advanced. OK.
However in the same module I have a function "generate" which APPEARS to do the correct processing, from the trace prints ( see below), but NO deposits are updated and transaction sequence is not advanced even tho I'm
using the same deposit function called from the konsole.
Note how two : // Create deposit for "harry" with <some value> value // : messages appear, but NO corresponding
: update "harry" message ???
Any help appreciated.
==========================================
erl interactive konsole 'trace'
=========================================
Account : AccName: Balance:
beatie 1330.82
harry 1824.43
bill 463.68
max 676.44
fred 2204.90
Total : 6500.26
** end printhdr ***
{aborted,{badarg,ok,[],infinity,mnesia}}
4> ets:tab2list(sequence).
[{sequence,"beatie",13},
{sequence,"harry",3},
{sequence,"bill",4},
{sequence,"max",35},
{sequence,"fred",3}]
5> cu_client:deposit("harry",25).
Create deposit for "harry" with 25 value
update "harry"
{atomic,1849.43}
6> ets:tab2list(sequence).
[{sequence,"beatie",13},
{sequence,"harry",4},
{sequence,"bill",4},
{sequence,"max",35},
{sequence,"fred",3}]
7> cu_client:generate("harry",2).
Create transaction for "harry" with 92.3009 value
Create deposit for "harry" with 92.3009 value
Create transaction for "harry" with 443.585 value
Create deposit for "harry" with 443.585 value
generate "harry" records finished
{aborted,{badarg,ok,[],infinity,mnesia}}
8> ets:tab2list(sequence).
[{sequence,"beatie",13},
{sequence,"harry",4},
{sequence,"bill",4},
{sequence,"max",35},
{sequence,"fred",3}]
9>
===========================
code extraction
===========================
-module(cu).
-export([generate/2 , deposit/2, withdraw/2, balance/1, clear/1,
accounts/0, showAccs/0, accTrans/1]).
-include("creditUnion.hrl").
-include("/home/brett/Erlang/otp_src_R11B-1/lib/stdlib/include/qlc.hrl").
dateTime() ->
{{Year,Month,Day},{Hour,Min,Sec}} = calendar:local_time(),
io:fwrite("\n\ ~2B/~2B/~4B ~2B:~2.10.0B:~2.10.0B\n\n",
[Day, Month, Year, Hour, Min, Sec]).
%% =========================
deposit(AccNo, X) ->
io:fwrite("Create deposit for ~p with ~p value \n", [AccNo, X] ),
fun() ->
case mnesia:read({account, AccNo}) of
[] ->
io:fwrite("create ~p \n", [AccNo] ),
%% no account , so make one
Entry = #account{accNum=AccNo, balance=X},
mnesia:write(Entry),
log_trans(AccNo, X),
X;
[E] ->
io:fwrite("update ~p \n", [AccNo] ),
%% update with a deposit
Old = E#account.balance,
New = Old + X,
E1 = E#account{balance=New},
mnesia:write(E1),
log_trans(AccNo, X),
New
end
end.
%%% ============= log_trans ========
log_trans(AccNo, X) ->
SeqNo = sequence:sequence(AccNo),
%% returns next sequence number for key : AccNo
%% a record in sequence table will be created if required
TransTime = calendar:local_time(),
mnesia:write(#acctrans { transtime = TransTime ,
seq = SeqNo,
accNo = AccNo, amount =X }).
%%% ============= generate =======
%%% generate an account with numTrans entries
generate(AccNO,0) -> io:fwrite("generate ~p records finished \n", [AccNO]);
generate(AccNO,NumTrans) ->
Value = random:uniform() * 1000,
io:fwrite("Create transaction for ~p with ~p value \n", [AccNO, Value] ),
deposit(AccNO, Value ),
generate(AccNO, (NumTrans - 1) ).
%%% =========================== |
|
|
| Back to top |
|
| francesco |
Posted: Sun Dec 02, 2007 9:04 am |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
Hi Bret,
a bit too early on a Sunday morning for me, but at first glance, where do you call mnesia:transaction(Fun) in you code? You seem to create the funs which encapsulate the transactions, but you never run them.
You do seem to have it somewhere, however, as you are getting badmatch errors and aborted transactions.
My first instinct is that the code you are running is not the code you are looking at. Try recompiling, restart the node, and check the compilation date with m(Module).
Also investigate the aborted transaction because of a badarg. (You or one of the libraries you use is calling a BIF with an invalid argument).
Hope the above helps,
Francesco
--
http://www.erlang-consulting.com |
|
|
| Back to top |
|
| brett hallett |
Posted: Mon Dec 03, 2007 9:51 pm |
|
|
|
User
Joined: 21 Aug 2007
Posts: 21
|
>>a bit too early on a Sunday morning for me, but at first glance, where do you call mnesia:transaction(Fun) in you >>code? You seem to create the funs which encapsulate the transactions, but you never run them.
The code is actually in three parts ( is based upon Joe Armstrongs 'bank' example tutorial "a fault tolerant server" of 13-02-2003. A server, a client and the actual app code. After getting his version working I started adding my code, but did not
change his base system (much). The lack of mnesia:transaction(Fun) calls are probably handled in the server ( but I really dont know . However at your suggestion I did add mnesia:transaction(Fun) calls in functions deposit,withdraw, balance - my original problem now appears solved. Thanks for the suggestion.
>>You do seem to have it somewhere, however, as you are getting badmatch errors and aborted transactions.
I've (naturally) seen these errors, but have no idea where they are generated -- still working on this
>>My first instinct is that the code you are running is not the code you are looking at. Try recompiling, restart the >>node, and check the compilation date with m(Module).
No - I very carefully place the problem into its own directory, and run complete compiles ( erlc *.erl ) at least once
in a development session. And restart erl every tiime.
>>Also investigate the aborted transaction because of a badarg. (You or one of the libraries you use is calling a BIF >>with an invalid argument).
Again, i'm unsure where these are being generated. Is'nt debugging "wonderful" ????
>>Hope the above helps,
Very much, thanks for your useful input.
Francesco |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|