write to db through a background task

This keeps the number of database connections from growing