[BangPypers] sqlalchemy nested scoped session

Saju M sajuptpm at gmail.com
Sun Nov 18 08:05:34 CET 2012


Hi,

The code bellow throwing error "ObjectDeletedError: Instance 'xxx' has been
deleted." when a exception throwing from "method1".
* Error in line method1(db_obj1.id),  db_obj1.id failing.
* The rollback in method1 rollbacking transaction in main_method.
* I want to isolate transaction in main_method from rollback in method1.

Note:
This is first place I am trying to create nested session in my project. In
all other places I am directly using DBSession object like (DBSession.add,
DBSession.query, DBSession.updare, etc----)


==== Code ====

from sqlalchemy.orm import scoped_session, sessionmaker
maker = sessionmaker(autoflush=True, autocommit=False,expire_on_
commit=False,
                     extension=ZopeTransactionExtension())
zopelessmaker = sessionmaker(autoflush=True, \
                             autocommit=False, \
                             expire_on_commit=False)
*DBSession = scoped_session(maker)*



def main_method():
    db_obj1 = DBModelclass1("Hello")
    DBSession.add(db_obj1)
    DBSession.fush()

    for x in lst:
        try:
            method1(db_obj1.id)
        excpt Exception, ex:
            pass



def method1(id):
    try:
*        s1 = DBSession()
        s1.begin_nested()*
        db_obj2 = DBModelclass2("Test")
        db_obj2.refname = "name_%s" %(id)
        DBSession.add(db_obj2) // I think this is wrong, adding object to
main session
        DBSession.fush() // I think this is wrong*
*
        if some-codition:
            raise Exception("Failedd")

        *s1.commit()*
    except Exception, ex:
        *s1.rollback()*
        raise ex


Thanks,


More information about the BangPypers mailing list