eJabberd: room history is erased after server restart

Solution 1:

every time I restart the server, the history of every room is lost.

The room recent discussion history is keep only in RAM, because it isn't expected to be stored permanently. It is configured with the option history_size, and it doesn't make sense to set a high value, as it only allows a new occupant to know recent comments in the current discussion. It is not for logging, it is not for storage, not for archiving, it is only to know what the current conversation is. See https://xmpp.org/extensions/xep-0045.html#enter-history

Actually, it is still stored in the database, but the client isn't able to retrieve it anymore.

What you see stored in the database technically is not the room "discussion history" mentioned before; that is room discussion archiving, that you enabled with mam option. That stores all the room messages, persistently, in a database. So they can be consulted anytime in the future. See https://docs.ejabberd.im/admin/configuration/#mod-mam