Question

I'm having some issue inserting data in a Mnesia database.

Here's the code : (schema already exists)

-module(mnesia_test).

-record(messages_queue, {id, ack, order, message}).

-export([start/0, add/0]).

start() ->
    mnesia:start(),
    mnesia:delete_table(messages_queue),
    mnesia:create_table(messages_queue, [{attributes, record_info(fields, messages_queue)}, {type, bag}, {record_name, messages_queue}]).

add() ->
    M = #messages_queue{id = "11223344", ack = [0, 5, 32, 91, 23, 106], order= 0, message="Hello world !"},
    mnesia:write(M).

The write get's aborted :

Erlang R16B (erts-5.10.1) [source] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.1  (abort with ^G)
1> mnesia_test:start().
{atomic,ok}
2> mnesia_test:add().
** exception exit: {aborted,no_transaction}
     in function  mnesia:abort/1 (mnesia.erl, line 309)
Was it helpful?

Solution

I got it...

The write must be wrapped in a mnesia:transaction()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top