[erlang-questions] mnesia question: create table in a transaction

Ulf Wiger ulf.wiger@REDACTED
Fri Sep 18 15:26:19 CEST 2009


Schema transactions are more conservative. To the extent
that use of these functions is at all recommended, you
should not use them within normal transactions.

An additional problem is that you cannot write to the
table just created within the same transaction, as the
table itself doesn't materialize until the transaction
is committed.

I made a little hack to allow that in the rdbms project.
If memory serves, it was done in an experimental version
of (the already fairly experimental) rdbms, which I never
committed to any outside store. This particular feature
seemed to work well, though.

As mnesia hacks go, this one was... well, medium - 4 monkey
wrenches, simplest mnesia hacks starting at 3.

BR,
Ulf W

Eugen Sobchenko wrote:
> create_table/2 inside a regular mnesia transaction is prohibited.
> There is an error on nested transaction:
> 
> add_table1() ->
> 	F = fun() ->
> 		% ...
> 		mnesia:create_table( test1,
> 			[
> 				{ram_copies, [node()]},
> 				{attributes, record_info(fields, test)},
> 				{record_name, test}
> 			]
> 		)
> 	end,
> 	mnesia:transaction(F).
> 
> 4> test:add_table1().
> {atomic,{aborted,nested_transaction}}
> 
> After a brief search I found a solution using undocumented features of
> mnesia_schema.erl:
> 
> add_table2() ->
> 	F = fun() ->
> 		% ...
> 		Props = [
> 			{ram_copies, [node()]},
> 			{attributes, record_info(fields, test)},
> 			{record_name, test}
> 		],
> 		Cs = mnesia_schema:list2cs([{name, test2}|Props]),
> 		mnesia_schema:do_create_table(Cs)
> 	end,
> 	mnesia_schema:schema_transaction(F).
> 
> 5> test:add_table2().
> {atomic,ok}
> 
> However, using of mnesia_schema:do_create_table with
> mnesia:transaction works too:
> 
> add_table3() ->
> 	F = fun() ->
> 		% ...
> 		Props = [
> 			{ram_copies, [node()]},
> 			{attributes, record_info(fields, test)},
> 			{record_name, test}
> 		],
> 		Cs = mnesia_schema:list2cs([{name, test3}|Props]),
> 		mnesia_schema:do_create_table(Cs)
> 	end,
> 	mnesia:transaction(F).
> 
> 6> test:add_table3().
> {atomic,ok}
> 
> Is there a practical difference between the second and third version?
> Can I use second or third version in my production code?
> 
> 
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
> 


-- 
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com


More information about the erlang-questions mailing list