Hermann Himmelbauer wrote:
Am Donnerstag, 5. Juni 2008 19:38 schrieb Laurence Rowe:
Hermann Himmelbauer wrote:
Am Mittwoch, 4. Juni 2008 22:09 schrieb Laurence Rowe:
Hermann Himmelbauer wrote:
In my application, I then use getSASession() to retrieve my session.
However, what I think is not that beautiful is the "s.bind = engine"
part. Are there any suggestions how to improve this?
You have two options
If you ever need to mix objects from different `sites` into the same
session, you should use an adapter on your root object like:
If you don't need to mix objects from different `sites` then you can
register a local utility for ISessionConfig.
def scope():
return getUtility(ISessionConfig).uid, thread.get_ident()
def factory():
engine = Engine(getUtility(ISessionConfig).url)
return create_session(
transactional=True, autoflush=True, bind=engine
extension=ZopeTransactionExtension(),
))
Session = scoped_session(factory, scopefunc=scope)
Then you can just import Session and use:
session = Session()
Ok, great, thanks for help. The only thing I don't understand is what
"uid" from the SessionConfig utility is. Below is my full database
integration code which works for me, perhaps this is helpful to someone
else.
uid is some id that distinguishes your various application instances. On
zope 2 I would probably use getPhysicalPath(). I don't know what the
zope3 equivalent is.
Hmmm, maybe it's:
from zope.traversing.api import getPath
from zope.app.component.hooks import getSite
@property
def uid(self):
return getPath(getSite())
Looking at your code, why did you decide to store the engine on a _v_
attribute? I don't think you need to save it at all. You can access a
connection through session.connection()
Ok, but in case I create the engine in the session_factor, e.g.:
def session_factory():
engine = createEngine(....)
return create_session(transactional = True,
autoflush = True,
bind = engine,
extension = ZopeTransactionExtension())
Wouldn't the engine be created for every request, as the scope changes and the
factory is called? In my case, the engine is created when the first session
is fetched. After that it will be recreated only if the DSN changes.
The engines are created the same number of times either way.
zope.sqlalchemy uses session.close() rather than session.remove(), so
the session/engine is only created once per thread, then recycled. This
is the same as storing it on a _v_ attribute, which are per thread as
active objects live in the connection cache.
Laurence
_______________________________________________
Zope-Dev maillist - Zope-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zope-dev
** No cross posts or HTML encoding! **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope )