Hi -

I'm using SA in conjunction w/ Pylons, in their default (0.9.7)
approach -- i.e. using scoped session classes created by calling
something like:

sm = orm.sessionmaker(autoflush=True, autocommit=False, bind=engine)
meta.Session = orm.scoped_session(sm)

I have a base controller that is designed to handle any session
"cleanup" -- i.e. do any pending commits and rollback and then remove
the session.

This is mostly working; however, I'm getting errors about "The
transaction is invalid" when attempting to perform commits.  I'm not
sure that this is actually resulting in an error -- and while I think
a refactor is in order here anyway, I would like to better understand
what I'm doing wrong. :)

Here's my base controller:

class CommitDisposeBaseController(WSGIController):
    """ A base controller class that automatically commits any
outstanding SA changes, closes (removes) the SA Session,
    and disposes of the engine (or pool, if applicable). """
    def __call__(self, environ, start_response):
        try:
            result = WSGIController.__call__(self, environ,
start_response)
            if meta.Session.is_active:
                meta.Session.commit()
            return result
        except Exception, e:
            if meta.Session.is_active:
                meta.Session.rollback()
            _log.exception(e)
            raise
        finally:
            meta.Session.remove()
            meta.engine.dispose()

The errors ("The transaction is inactive.") are coming from my
meta.Session.commit() line.  Two questions:

1) Isn't this what meta.Session.is_active should be testing for?

2) Is there something special about using these methods as  "class
methods" instead of instance methods?  My assumption is that this
transparently grabs the current/active Session instance and executes
on that.   I do know, though, that some methods such as remove() are
not available on instances -- only on the class.  This is a bit
confusing.

Any help would be appreciated.

Thanks,
Hans
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
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