I'm using it in 3 views, in one of them, passing to other functions as a 
> parameter. In this case, need I the scoped_session? 
>

the scoped_session just does some things with the thread to ensure you can 
grab a global or package variable.  someone can correct me if i'm wrong -- 
but it uses some similar concepts to the transaction module to ensure you 
have the right session.

if you're creating a session and explicitly passing it around, it doesn't 
need to be a scoped_session.  a regular sqlalchemy session will be fine.


I create the session inside the request, you say, at the same time, I 
> can register the add_finished_callback to avoid the try/finally in the 
> view body, correct? 
>

yes and no.  see this example:

http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/webob.html?highlight=add_finished_callback#cleaning-up-after-a-request

in that example, the DBSession is a scoped session.  if you're not using a 
scoped session, you can stash the db session onto the request and access it 
the same way.


something like this should work....

def cleanup_callback(request):
if hasattr(request,'_my_dBSession'):
request._my_dBSession.remove()


class ClassBasedView():

def expensive_view(self):

# build out a new session
_session_factory = orm.sessionmaker()
request._my_dbSession = _session_factory()
 # create a cleanup for the session
self.request.add_finished_callback( cleanup_callback )

# now do stuff with the session
try :
 except:
 the reason why i say yes and "NO", is that i like to think of the 
add_finished_callback like a safety.  it lets you register a cleanup 
function to run unconditionally -  which would remove the session ( 
implying a rollback if there was no commit ) -- and works perfectly in 
situations where you forgot to rollback, or you have an unhandled exception.

BUT i would still explicitly rollback the session within a try/except 
block, just to be safe and clear in your code.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to