title: Ejabberd module with child process
body: <p>I created a logging module which logs messages to a mysql db, the current code is located here:
  <a href="https://github.com/amiadogroup/mod_log_chat_mysql5/blob/master/src/mod_log_chat_mysql5.erl" rel="nofollow">https://github.com/amiadogroup/mod_log_chat_mysql5/blob/master/src/mod_log_chat_mysql5.erl</a></p>
  
  <p>The Problem with the current code is, that sometimes the connection gets closed and as a result, the module doesn't work anymore.
  As you see in the code, I store the DBRef in an ets table, which is not really the good way to go.</p>
  
  <p>I asked the erlang mailinglist about this and they suggested me to do the DB Connection as an own child process of the module. This would enable the module to gracefully restart the connection upon closing of the connection.</p>
  
  <p>Now my question is: how can I implement this child process with gen_server and/or gen_mod?</p>
  
  <p>Do I need to create two files or can I do it within the same file?</p>
  
  <p>Is there any example somewhere on how I could achieve that?</p>
  
  <p><strong>Edit:</strong> As you can see in the linked github repo, I updated the code and it works now, weeha!
  Looking at the mod_Archive code helped me a lot, although I didn't decide to upgrade my ejabberd version.</p>
  
  <p>I ran into another, but related problem now. In the code you see that I do an initial query with "SET NAMES UTF8" to prevent garbling of messages. It seems that this isn't done again if the gen_server does a reconnect. Is there any hook I can call upon reconnect so that the UTF8 query is done everytime?</p>
  
  <p><strong>Edit#2:</strong>
  Now I switched to Emysql (https://github.com/Eonblast/Emysql) and it works out of the box by specifying the encoding directly on connect. 
  Code is on github.</p>
  
  <p>Thanks for your help,
  Michael</p>