mnesia question: create table in a transaction

Eugen Sobchenko esobchenko@REDACTED
Fri Sep 18 15:07:02 CEST 2009


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?



More information about the erlang-questions mailing list