开发者

Sql Alchemy Duplicated Commit

I'm currently facing a problem in my Cherrypy application. Im my own custom session module, when performing session.add(), the exact same object gets updated twice.

cherrypy.request.SessionManager.user_data = user

try:

    db_session.add(cherrypy.request.SessionManager)
    db_session.commit()

Will Return

2011-06-21 09:16:48,991 INFO sqlalchemy.engine.base.Engine.0x...04cL BEGIN (implicit)
2011-06-21 09:16:49,015 INFO sqlalchemy.engine.base.Engine.0x...04cL SELECT ..... 
FROM "Clients_Users" 
WHERE "Clients_Users".username = %(username_1)s AND "Clients_Users".password = %(password_1)s 
LIMIT 1 OFFSET 0
2011-06-21 09:16:49,015 INFO sqlalchemy.engine.base.Engine.0x...04cL {'password_1': '123', 'username_1': u'1'}
2011-06-21 09:16:49,047 INFO sqlalchemy.engine.base.Engine.0x...04cL UPDATE "SYS_Sessions" SET user_data=%(user_data)s WHERE "SYS_Sessions".id = %(SYS_Sessions_id)s
2011-06-21 09:16:49,067 INFO sqlalchemy.engine.base.Engine.0x...04cL {'SYS_Sessions_id': 92L, 'user_data': <psycopg2._psycopg.Binary ob开发者_如何学运维ject at 0x8a7c06c>}
2011-06-21 09:16:49,071 INFO sqlalchemy.engine.base.Engine.0x...04cL COMMIT
2011-06-21 09:16:49,093 INFO sqlalchemy.engine.base.Engine.0x...04cL BEGIN (implicit)
2011-06-21 09:16:49,095 INFO sqlalchemy.engine.base.Engine.0x...04cL UPDATE "SYS_Sessions" SET user_data=%(user_data)s WHERE "SYS_Sessions".id = %(SYS_Sessions_id)s
2011-06-21 09:16:49,095 INFO sqlalchemy.engine.base.Engine.0x...04cL {'SYS_Sessions_id': 92L, 'user_data': <psycopg2._psycopg.Binary object at 0x8a8424c>}
2011-06-21 09:16:49,108 INFO sqlalchemy.engine.base.Engine.0x...04cL COMMIT

Has anyone seen this before ?

P.S This doesn't happen in the rest of the modules I have made.


You might try adding the user_data to the session rather than adding the SessionManager object itself to the session. This would at least make debugging more straightforward.

As it is, I'm not sure whether you have a database session inside the cherrypy SessionManager-- which would seem undesirable, but maybe sqlalchemy can handle it(?) Or perhaps are making multiple changes to the object that can't be done at exactly the same time in the database-- e.g. maybe this is how some cascades look with echo on(?). What value of cascade are you using?

I'm not sure what is being updated the second time from reading the sql. It looks like only something implicit. You probably need to show some of the code for these objects or some of the code that causes the update to get a better answer. But maybe the above intuition will be helpful. Good luck


Ok , Sorry for such a great delay but i was on vacation.

Anyway in order prevent the double commit , you need to remove the object from the session and you do that by using session.expunge().

One More Thing .

Using session.merge() Will allow you to reuse the object that was expunged,.

Hope this helps someone in need.

P.S : Keep into consideration that an object may misbehave or that any future patch to alchemy may break the compatibility.So profane is Right.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜