:dets -- close file after each write?

When using :dets should I close the file after each write operation? I see many warnings like this:

dets: file "/my/file" not properly closed, repairing ...

they seem to go away if I close the file after each write operation but this feels like it might be slow.

Thanks for any advice!

The BEAM will close the file when the process that opened it exits - unless the exit is abnormal. ^c is an a abnormal exit so if you’re test in exs and thats how you exit then I think the error is to be expected.

Front the docs:

A file must be opened and closed. If a file has not been properly closed, the dets module will automatically repair the file. This might take some time if the file is very large. By default, files are closed if the process which opened the file terminates. If several Erlang processes open the same dets file, they will all share the file. The file is properly closed when all users has either terminated or closed the file. dets files are not properly closed if the Erlang runtime system is terminated abnormally.

1 Like

Thank you very much! I did not realize that ^c was abnormal. This explains why the warning only happened for me inside iex.

Try exiting iex with ^g followed by q. I’ve been told that is a kinder, gentler way to exit.

1 Like