On Jun 30, 2008, at 11:27 PM, Tai Tran wrote:

>
> I'm using contextual/thread-local sessions, in the last statement
> "db_session.delete(self)" of Port.destroySelf(), I always get the same
> traceback as I tried to demonstrate in the last port:
>
> "sqlalchemy.exceptions.InvalidRequestError: Instance '[EMAIL PROTECTED]'  
> is with
> key (<class '__main__.Port'>, (2,), None) already persisted with a  
> different
> identity"

if Session() is thread-local, that means you are sharing these objects  
between threads.  You'll get generally poor results when you share  
session-bound objects between threads, especially if you're relying on  
thread-local behavior to get at the current session.

> I found a solution to solve this problem by deleting the object in  
> current
> session:
>
> def destroySelf(self):
>        db_session = Session()
>        ...
>        obj = db_session.get(Port, self.id)
>        db_session.delete(obj)

besides that get() on Session is removed in 0.5 (use  
session.query(Port).get(id)), this method is OK as well since you are  
ensuring that you are getting the object from the current thread's  
Session before deleting.  I'm getting the impresion that destroySelf()  
is used from __del__ or similar here.


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to