Michael Mileusnich wrote:
> Hello,
>
> I know there is documentation on this but I am still fuzzy on certain
> practices when using the session. In my application I have a function
> that
> returns a new non scoped session. In one method (method a) I get object o
> from this session. I call another method (method b) that needs to use o
> and
> potentially make changes to it. Is it best practice to pass the session
> and
> the object as a parameter to method b or should should I pass only the
> object and use session = Session.object_session(o) to grab that session?
> Is there some other option (like re-fetching that object every time which
> is
> what I am trying to avoid).
usually there's a ScopedSession per application that everyone can call
upon to give the session that they want. If I had an application that
had multiple session configurations, I might use decorators to reconfigure
the ScopedSession global as needed, such as:
@uses_session('write_master')
def do_something(x, y, z, ...)
@uses_session('read_slave')
def do_something(q, p, r, ...)
The case for object_session() is usually for ad-hoc per-object activities,
like a query method on the object itself which wants to say
object_session(self).query(...).
The other options are passing the Session around to all functions, which
could be appropriate in some cases. Calling object_session(o) on the
passed object is kind of similar to that, i.e. instead of passing "o" to
your functions you might pass a variable like "context" which references
the Session as well as the object or objects in question for a particular
operation. At least that way you give yourself room for expansion.
>
> --
> 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.
>
>
--
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.