Search code examples
erlangmnesiaerlang-otp

Using gen_server to encapsulate an mnesia table?


I have a server application made in Erlang. In it I have an mnesia table that store some information on photos. In the spirit of "everything is a process" I decided to wrap that table in a gen_server module, so that the gen_server module is the only one that directly accesses the table. Querying and adding information to that table is done by sending messages to that process (which has a registered name). The idea is that there will be several client processes querying information from that table.

This works just fine, but that gen_server module has no state. Everything it requires is stored in the mnesia table. So, I wonder if a gen_server is perhaps not the best model for encapsulating that table?

Should I simply not make it a process, and instead only encapsulate the table through the functions in that module? In case of a bug in that module, that would cause the calling process to crash, which I think might be better, because it would only affect a single client, as opposed to now, when it would cause the gen_server process to crash, leaving everyone without access to the table (until the supervisor restarts it).

Any input is greatly appreciated.


Solution

  • I guess according to Occam's razor there is no need for this gen_server to exist, especially since there is absolutely no state stored in it. Such process could be needed in situations when you need access to the table (or any other resource) to be strictly sequential (for example you might want to avoid any aborted transactions at cost of a bottleneck).

    Encapsulating access to the table in a module is a good solution. It creates no additional complexity, while providing proper level of abstraction and encapsulation.