Statistics gathering performance improvements.

Wrapped the saving of TaskCats and DatasetCats in a transcation.
On my computer (which has a spinning disk), this reduces the time
to save query stats from 5 minutes to 5 seconds.

Also made sure all access to categories is non-lazy, and wrapped it
in transctions too. Hopefully this will improve the reliability of
the database connection overall.