aldjemy: Connection closed exception


Traceback (most recent call last):
  File "/Users/erik/Dropbox/home/git/proj/api/tests/test_user.py", line 211, in test_post_list_bgt_validation
    resp = self.api_client.post(self.list_url, format='json', data=self.post_data, HTTP_AUTHORIZATION=self.get_credentials())
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/test.py", line 90, in post
    return self.generic('POST', path, data, content_type, **extra)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/compat.py", line 150, in generic
    return self.request(**r)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/test.py", line 157, in request
    return super(APIClient, self).request(**kwargs)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/test.py", line 109, in request
    request = super(APIRequestFactory, self).request(**kwargs)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/django/test/client.py", line 440, in request
    six.reraise(*exc_info)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/viewsets.py", line 79, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/views.py", line 403, in dispatch
    response = self.handle_exception(exc)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/views.py", line 400, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/mixins.py", line 52, in create
    if serializer.is_valid():
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/serializers.py", line 550, in is_valid
    return not self.errors
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/serializers.py", line 542, in errors
    ret = self.from_native(data, files)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/serializers.py", line 1021, in from_native
    instance = super(ModelSerializer, self).from_native(data, files)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/serializers.py", line 370, in from_native
    attrs = self.perform_validation(attrs)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/rest_framework/serializers.py", line 303, in perform_validation
    attrs = validate_method(attrs, source)
  File "/Users/erik/Dropbox/home/git/proj/api/serializers.py", line 267, in validate_bgt
    portal.validators.validate_user_bgt(bgt, margin, attrs['user'], self.object)
  File "/Users/erik/Dropbox/home/git/proj/portal/validators.py", line 35, in validate_user_bgt
    net_expsr = LedgerAccount.objects.calculate_user_net_expsr(user, instance_pk_list, adjusted_new_bgt or decimal.Decimal('0'))
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/erik/Dropbox/home/git/proj/portal/managers.py", line 997, in calculate_user_net_expsr
    gross_expsr = gross_expsr.scalar() or decimal.Decimal(0)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2429, in scalar
    ret = self.one()
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2398, in one
    ret = list(self)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2441, in __iter__
    return self._execute_and_instances(context)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2456, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 841, in execute
    return meth(self, multiparams, params)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 322, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 938, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1005, in _execute_context
    None, None)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1271, in _handle_dbapi_exception
    exc_info
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1001, in _execute_context
    context = constructor(dialect, self, conn, *args)
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 561, in _init_compiled
    self.cursor = self.create_cursor()
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 442, in create_cursor
    return self._dbapi_connection.cursor()
  File "/Users/erik/.virtualenvs/proj/lib/python2.7/site-packages/sqlalchemy/pool.py", line 770, in cursor
    return self.connection.cursor(*args, **kwargs)
  File "/Users/erik/Dropbox/home/git/aldjemy/aldjemy/wrapper.py", line 23, in __call__
    self.obj = self.obj(*a, **kw)
InterfaceError: (InterfaceError) connection already closed u'SELECT sum(portal_user.bgt - portal_user.bgt * (portal_user.margin / %(margin_1)s)) AS sum_1 \nFROM portal_user \nWHERE portal_user.status = %(status_1)s AND portal_user.user_id = %(user_id_1)s' [immutabledict({})]

----------------------------------------------------------------------
Ran 17 tests in 13.537s

FAILED (errors=1)

Django 1.7.8 aldjemy 0.4.1 sqlalchemy 0.9.9 (also tested 1.0.0 and 1.0.6). psycopg2 2.6.1 Postgres 9.4

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

@akheron As far as I remember, I also had this problem in production. I added django.core.signals for closing connections upon request finished. Here is the code I have in the end of urls.py:

from django.core import signals as core_signals

def close_connection(**kwargs):
    for conn in connections.all():
        if hasattr(conn, 'sa_session'):
            conn.sa_session.close()

core_signals.request_finished.connect(close_connection)