Fixed database connection bug

- Connect to database before each request instead of maintaining an
  open connection
- Store connection in g object, which is available in the request
  context
- Updated functions to use g.db instead of taking a connection object