[Zope-dev] Re: SQLAlchemy integration experiment

Laurence Rowe l at lrowe.co.uk
Tue Jun 17 10:09:22 EDT 2008


Martijn Faassen wrote:
> Hi there,
> 
> On Tue, Jun 17, 2008 at 2:30 PM, Brian Sutherland
> <brian at vanguardistas.net> wrote:
> [snip]
>> Just commenting that IDatabase.engine is not used by any code external
>> to the Database object. There's no use case for it to be public.
> 
> While I am not sure we have a strong use case for engine anyway, I
> like methods that people can implement to affect behavior (especially
> when subclassing) to be public. I don't feel comfortable implementing
> something that starts with an underscore.

Why not just have:

class IDatabase(Interface):
     """A utility that specifies the database.
     """

     def session_factory():
         """Create a new session
         """

     def id():
         """Get unique id for this database configuration.

         This should be unique per site (application).
         """


class Database(grok.LocalUtility):
     grok.implements(IDatabase)

     def session_factory(self):
         engine = create_engine(
             'postgres:///experiment',
             convert_unicode=True)
         return create_session(
             bind=engine,
             autocommit=True,
             autoflush=True,
             extension=ZopeTransactionExtension())

     def id(self):
         # we use the application name as the unique id. Can we use
         # something more clever and universally working?
         return self.__parent__.__name__

> [snip]
>>> [snip]
>>>> As Laurence has suggested, the session persists, so
>>>> IDatabase.configuration will only be called once per thread. Not sure if
>>>> creating one new engine per thread is bad.
>>> I don't know either. I've assumed that one wouldn't want to recreate the
>>> engine for each thread, but perhaps that's fine and we don't break any,
>>> say, connection pooling mechanisms that way? I do not know.
>> Unfortunately I don't either...

I'm not sure connection pooling is really useful in a threaded 
environment with recycled sessions. You want n threads = n connections. 
If we started creating new sessions each request then things would be 
different.

Laurence



More information about the Zope-Dev mailing list