Hi, using scoped_session(sessionmaker()) to create my sessions, if I
hammer a
request (using Pylons) by repeatedly clicking on a link that uses the
ORM
somewhat extensively for the relevant request, it seems that another
thread is
getting involved with SQLAlchemy internals and pulling the rug out
from under
its feet. Here's the change I made to stop the exception from
happening, but
we're just wondering if I've done something else wrong or if this is
an actual
bug in SQLAlchemy:

Index: state.py
===================================================================
--- state.py    (revision 5974)
+++ state.py    (working copy)
@@ -170,9 +170,14 @@
                 attr.impl.key in self.expired_attributes and
                 attr.impl.key in unmodified
             ])
-        for k in self.expired_attributes:
-            self.callables.pop(k, None)
-        del self.expired_attributes
+        try:
+            for k in self.expired_attributes:
+                self.callables.pop(k, None)
+            del self.expired_attributes
+        except AttributeError:
+            # XXX: self.expired_attributes can be del'ed by another
thread
+            # which raises an AttributeError here
+            pass
         return ATTR_WAS_SET

     @property

Here's the original traceback before the change:

File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/
prospectspace/controllers/company.py', line 206 in index
  return self.render_response('company.mako', t_pars)
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/
prospectspace/lib/base.py', line 372 in render_response
  page = tmpl.render(**kargs)
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/lib/
python2.5/site-packages/Mako-0.1.8-py2.5.egg/mako/template.py', line
114 in render
  return runtime._render(self, self.callable_, args, data)
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/lib/
python2.5/site-packages/Mako-0.1.8-py2.5.egg/mako/runtime.py', line
287 in _render
  _render_context(template, callable_, context, *args,
**_kwargs_for_callable(callable_, data))
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/lib/
python2.5/site-packages/Mako-0.1.8-py2.5.egg/mako/runtime.py', line
304 in _render_context
  _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/lib/
python2.5/site-packages/Mako-0.1.8-py2.5.egg/mako/runtime.py', line
337 in _exec_template
  callable_(context, *args, **kwargs)
File 'prospectmaster_mako', line 61 in render_body
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/lib/
python2.5/site-packages/SQLAlchemy-0.5.4p1-py2.5.egg/sqlalchemy/orm/
attributes.py', line 158 in __get__
  return self.impl.get(instance_state(instance), instance_dict
(instance))
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/lib/
python2.5/site-packages/SQLAlchemy-0.5.4p1-py2.5.egg/sqlalchemy/orm/
attributes.py', line 374 in get
  value = callable_()
File '/home/bob/src/prospectspace/branches/sqlalchemy-eon-merge/lib/
python2.5/site-packages/SQLAlchemy-0.5.4p1-py2.5.egg/sqlalchemy/orm/
state.py', line 175 in __call__
  del self.expired_attributes
AttributeError: expired_attributes

And here's how I'm dealing with creating the sessions:

threadlocal = threading.local()

Session = scoped_session(sessionmaker(autocommit=True))
Session.metadata = None

def setup_db():
    if hasattr(threadlocal, 'engine'):
        return
    uri = config['main.engine.dburi']
    threadlocal.engine = create_engine(uri)
    Session.configure(bind=threadlocal.engine)
    if Session.metadata is None:
        Session.metadata = MetaData(threadlocal.engine)
        model.initialise(Session.metadata)


And then each request does this:

        setup_db()
        environ['dbsession'] = Session()

The reason for including this bit is because I'm not convinced I'm
doing it
correctly, so I want to make sure that, if I am doing it wrong, this
isn't
what's causing the problem in SQLAlchemy, i.e. it may not be a bug at
all.

Please let me know if you need any more code/info - thanks a lot for
any help.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to