[Zope-dev] Re: SQLAlchemy integration experiment
Martijn Faassen
faassen at startifact.com
Tue Jun 17 12:05:10 EDT 2008
Brian Sutherland wrote:
[snip]
> This would probably be close to what I would write for my usecase:
>
> class Database:
>
> implements(IDatabase)
>
> def __init__(self, *args, **kw):
> self._args = args
> self._kw = kw
>
> def scopefunc(self):
> return None # use default per-thread scoping
>
> def session_factory(self):
> return sessionmaker(*self._args, **self._kw)
I don't think you can use sessionmaker, as that creates a class and you
need to create an actual session in this place. You'd need
create_session. You'd also need to implement your scopefunc, otherwise
you get *no* scoping at all, not even per thread, so return
thread.get_ident().
Like this:
class Database:
implements(IDatabase)
def __init__(self, *args, **kw):
self._args = args
self._kw = kw
def scopefunc(self):
return thread.get_ident()
def session_factory(self):
return create_session(*self._args, **self._kw)
we're ignoring the details of what creates the engine, but if you pass
'bind' along when you create Database that should take care of it.
Regards,
Martijn
More information about the Zope-Dev
mailing list