Someone posted a docs suggestion to -devel, which made me look at the 
current sqlalchemy scaffold.


I'm not sure it's 'correct' 


a few weeks ago I asked Mike Bayer (sqlalchemy) what the best practices for 
webapps were ( 
https://groups.google.com/forum/#!topic/sqlalchemy/ZsHxDzlATCQ )


he responded with two links:


fairly recently i wrote up as much as I could come up with on this, which 
> you can see first in the Session FAQ:
>
> http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#session-frequently-asked-questions
>
>
> and then regarding scoped_session in:
>
> http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#using-thread-local-scope-with-web-applications
>  



the big thing that i'm picking up off the current scaffold, is that there's 
a single scoped_session as DBSession in models.py ( 
https://github.com/Pylons/pyramid/blob/master/pyramid/scaffolds/alchemy/%2Bpackage%2B/models.py_tmpl
 
) , while the views.py uses that same session ( 
https://github.com/Pylons/pyramid/blob/master/pyramid/scaffolds/alchemy/%2Bpackage%2B/views.py_tmpl
 
)

the scaffold seems to not invalidate the local sqlalchemy session , or 
suggest that a new one is created.

It's my understanding that a few things should happen instead ( which are 
basically covered in the cookbook )


a) if a 'global' `DBSession` is used within a view, is should be explicitly 
instantiated to create a new thread/request local instance `DBSession()`

and/or

b) you could opt to be even more explicit and use tween to prep the session 
( or use add_request_property ) -- but that setup action should also run a 
add_finished_callback to `remove` (invalidate) the session after the 
request.


in pseudocode , something like....

    def ScopedSession_cleanup(request):
         request.ScopedSession.remove()

    def tween_factory(handler, registry):
        def tween(request):
              request.ScopedSession = ScopedSession()
              request.add_finished_callback(ScopedSession_cleanup)
        return tween

getting to the nuts and bolts:

1- 
http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/database/sqlalchemy.html#using-a-non-global-session

    "def cleanup():" would be better to have `session.remove()`, not 
`session.close()`

2- 
https://github.com/Pylons/pyramid/blob/master/pyramid/scaffolds/alchemy/%2Bpackage%2B/views.py_tmpl

     bad :
               "one = DBSession.query("

     better :
                "local_session = DBSession()"
                "local_session.query("

it might also make sense to replace `DBSession` with `DBSessionFactory`.  

thoughts ?



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


Reply via email to