Skip to content

Commit

Permalink
Open and close database connection explicitly.
Browse files Browse the repository at this point in the history
  • Loading branch information
passiomatic committed Jan 1, 2017
1 parent 6e20756 commit abd1edd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
4 changes: 4 additions & 0 deletions coldsweat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from webob import Request, Response
from webob.exc import *

from models import connect, close

from utilities import *
from coldsweat import *

Expand Down Expand Up @@ -70,9 +72,11 @@ def __call__(self, environ, start_response):
self.request = request
self.application_url = request.application_url

connect()
response = handler(*args)
if not response:
response = Response() # Provide an empty response
close()

return response(environ, start_response)

Expand Down
7 changes: 6 additions & 1 deletion coldsweat/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,14 @@ def run():

command_name, command_args = args[0].lower(), args[1:]

connect()
cc = CommandController()
try:
cc.run_command(command_name, command_options, command_args)
except CommandError, ex:
parser.error(ex)

finally:
close()
# Flush and close all logging handlers
import logging
logging.shutdown()
12 changes: 2 additions & 10 deletions coldsweat/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@
from fetcher import *


class BaseController(object):

def __init__(self):
connect()

def __del__(self):
close()

class UserController(BaseController):
class UserController(object):
'''
Base user controller class. Derived classes may need to override the user property
'''
Expand Down Expand Up @@ -155,7 +147,7 @@ def _q(*select):



class FeedController(BaseController):
class FeedController(object):
'''
Feed controller class
'''
Expand Down
3 changes: 2 additions & 1 deletion coldsweat/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ def session_response(status, headers, exc_info=None):
if manager.is_new:
return initial_response(environ, start_response)
return self.app(environ, start_response)
# Always close session
finally:
# Always close session and database connection
manager.close()
close()

class SessionManager(object):

Expand Down

0 comments on commit abd1edd

Please sign in to comment.