Database now closes connection after each query and reconnects if closed to ensure no more connection and locked database problems.

Fixed more internal 500 errors.
fix for tornado error handler
Merge remote-tracking branch 'origin/dev'