it is. I'm not familiar with a codepath which can cause that to happen,
and in fact even if the session isn't active, it still should be totally
fine to call commit(). Try this as an example:
from sqlalchemy import *
from sqlalchemy.orm import *
engine = create_engine("mysql://scott:tiger@localhost/test", echo=True)
s = scoped_session(sessionmaker(bind=engine))
s.commit()
s.rollback()
s.commit()
s.commit()
s.rollback()
s.rollback()
s.rollback()
s.commit()
s.commit()
It seems like some unhandled exception on the connection itself might have
occurred, i.e. after acquiring it via session.connection(), and the
session wasn't notified of this failure (i.e. via rollback()).
I don't ever use is_active myself, its something we put there for the
benefit of TurboGears. To my mind its really not needed for anything.
My recommendation would be to not call is_active() and to only call
commit() within controller methods as needed. you also can call
rollback() any number of times so the is_active() isnt needed there.
>
> 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.
this is the expected usage if the session is a scoped_session() which is
the case in a pylons setup.
> I do know, though, that some methods such as remove() are
> not available on instances -- only on the class. This is a bit
> confusing.
remove() is a method on scoped_session() which is not a session. It is a
thread local container object with proxying behavior. If it makes it
less confusing, you can use scoped_session like this:
# create session registry
my_scoped_session = scoped_session(sessionmaker())
# acquire the session from the registry
session = my_scoped_session()
# use the session
session.commit()
# remove "session" from the current scope
my_scoped_session.remove()
After using the above pattern for about five minutes, you'll probably see
the utility of scoped_session() proxying common methods to the underlying
session.