Fixed issue with cancelling long running queries in server.

JsonGenerators were not properly closed in finally blocks which lead to
malformed json when queries were cancelled by the user.
Merge pull request #3385 from pontusmelke/malformed-json

Fixed issue with cancelling long running queries in server.