Fix writing messages to database