re-scoped the cursor in count() #580

The transaction must outlive the cursor.

fixes #580