When updating the database records, first perform all update and inserts and then check the timeout for expired records -> records to be deleted.
Doing first the delete may hide records "expired but to be updated".
Credits go to Nick Altmann
Closes bug #112.

(cherry picked from commit 3c1164a012204e92150c5df5101aba709e4cb123)