The below was observed with SQLAlchemy r1685 + an instrumented Session
class. It happens every time on one of the systems I have access to, and
not at all on another; the former is a x86_64 system w/ Python 2.4.2,
and the latter an i386 system running Python 2.4.3. (Both are fairly
current Gentoo setups).
Here's the behavior I'm observing:
- When the threadlocal mod is first imported, a new session is created.
- When an ActiveMapper-based class is instantiated, the created object
is bound to the session.
- When save() is called on that object, another session is created and
the object is in turn asked to bind to that new session -- but since
it's already bound to the old session, it fails.
I don't understand the expected behavior of the relevant SQLAlchemy code
well enough to understand at which point in the process the failure is
occurring.
Could anyone comment?
Incidentally -- The only difference in the test suite results between
the two systems is that the system which works gives an ok for
testmanytomanyupdate (rm.objectstore.SaveTest) (while the one which
fails shows an ERROR) and the system which works shows a FAIL for
orm.inheritance.InheritTest4 (while the one which works shows an ok).
Both have 5-6 failures/9-10 errors with threadlocal enabled, or 5-6
failures/30-31 errors with --nothreadlocal passed in to the test suite.
--------------------
[EMAIL PROTECTED] ~/tgtest $ cat test.py
import sqlalchemy.mods.threadlocal
from sqlalchemy import *
from sqlalchemy.ext.activemapper import ActiveMapper, column
class Foo(ActiveMapper):
class mapping:
bar = column(String(40))
sqlalchemy.ext.activemapper.metadata.connect('sqlite:///test.sqlite')
Foo().save()
[EMAIL PROTECTED] ~/tgtest $ python test.py
Creating a new session: 47166684066512
File "test.py", line 1, in ?
import sqlalchemy.mods.threadlocal
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/mods/threadlocal.py", line
45, in ?
install_plugin()
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/mods/threadlocal.py", line
37, in install_plugin
global_extensions.append(objectstore.mapper_extension)
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/ext/sessioncontext.py",
line 39, in _get_mapper_extension
return self._extension
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/mods/threadlocal.py", line
27, in __getattr__
return getattr(self.current, key)
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/ext/sessioncontext.py",
line 29, in get_current
return self.registry()
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/util.py", line 246,
in __call__
return self.registry.setdefault(key, self.createfunc())
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 70, in __init__
traceback.print_stack() ## creating new session
Attaching <__main__.Foo object at 0x2ae5d9771650> to session
<sqlalchemy.orm.session.Session object at 0x2ae5d972f2d0> [hash
47166684066512]
File "test.py", line 12, in ?
Foo().save()
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/mapper.py",
line 514, in init
session._register_new(self)
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 357, in _register_new
self._attach(obj)
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 390, in _attach
traceback.print_stack() ## attaching to session
Creating a new session: 47166684337872
File "test.py", line 12, in ?
Foo().save()
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/ext/assignmapper.py", line
12, in do
session = ctx.current
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/ext/sessioncontext.py",
line 29, in get_current
return self.registry()
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/util.py", line 246,
in __call__
return self.registry.setdefault(key, self.createfunc())
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 70, in __init__
traceback.print_stack() ## creating new session
Traceback (most recent call last):
File "test.py", line 12, in ?
Foo().save()
File
"/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/ext/assignmapper.py", line
16, in do
return getattr(session, name)(self, *args, **kwargs)
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 282, in save
self._save_impl(object, entity_name=entity_name)
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 339, in _save_impl
self._register_new(object)
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 357, in _register_new
self._attach(obj)
File "/home/cduffy/pkgs/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 374, in _attach
raise exceptions.InvalidRequestError("Object '%s' is already
attached to session '%s' (this is %s [%s])" % (repr(obj), old,
repr(self), id(self)))
sqlalchemy.exceptions.InvalidRequestError: Object '<__main__.Foo object
at 0x2ae5d9771650>' is already attached to session '47166684066512'
(this is <sqlalchemy.orm.session.Session object at 0x2ae5d97716d0>
[47166684337872])
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users