Wrapping queries in transactions