my current proposal regarding the association of objects to sessions,
as well as the optional association of objects and classes to
"session contexts", is as follows:
Session - represents a current unit of work session and an
associated collection of objects. a particular object is only in one
Session at a time.
sessioncontext - a callable which returns a Session associated with
a particular context, such as a thread local context.
Daniel Miller has proposed a great implementation of a SessionContext
object which associates particular class hierarchies with particular
SessionContexts, using either a special base class or a metaclass.
The SQLALchemy everyone is familiar with has a simple "global thread
local" session context, which in 0.2 is availalbe when you import the
"threadlocal" mod.
So below, the methods that would be supplied by which to get a hold
of these various Sessions would look like:
# returns the session assocaited with the current "session context".
# the "session context" may be specific to a particular class or object
def current_session(instance=None, klass=None):
# objects/classes may implement the __sessioncontext__ method which
returns a
# sessioncontext callable
if instance is not None and hasattr(instance,
'__sessioncontext__'):
context = instance.__sessioncontext__()
return context()
elif klass is not None and hasattr(klass, '__sessioncontext__'):
context = klass.__sessioncontext__()
return context()
else:
context = _default_session_context(instance=instance,
klass=klass)
if context is not None:
return context()
else:
return None
# allows a default "session context" function to be plugged in
# at the module level. the threadlocal mod plugs in one particular
implementation
# of this function.
def _default_session_context(instance=None, klass=None):
return None
# returns the Session to which an object is currently attached. the
ORM system uses this method
# when it deals with an object instance in order to obtain the
correct Session. "_sessions" is a
# module-level WeakValueDictionary of all currently constructed
Session objects.
def object_session(instance):
hashkey = getattr(instance, '_sa_session_id', None)
if hashkey is not None:
return _sessions.get(hashkey)
else:
return None
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users