[Checkins] SVN: z3c.saconfig/trunk/ Add connection pooling options to the engine directive

Martin Aspeli optilude at gmx.net
Mon Jul 5 09:56:37 EDT 2010


Log message for revision 114205:
  Add connection pooling options to the engine directive

Changed:
  U   z3c.saconfig/trunk/CHANGES.txt
  U   z3c.saconfig/trunk/src/z3c/saconfig/README.txt
  U   z3c.saconfig/trunk/src/z3c/saconfig/zcml.py

-=-
Modified: z3c.saconfig/trunk/CHANGES.txt
===================================================================
--- z3c.saconfig/trunk/CHANGES.txt	2010-07-05 12:38:03 UTC (rev 114204)
+++ z3c.saconfig/trunk/CHANGES.txt	2010-07-05 13:56:36 UTC (rev 114205)
@@ -4,6 +4,10 @@
 0.11 (unreleased)
 =================
 
+- Add pool_size, max_overflow, pool_recycle and pool_timeout options to the
+  <engine /> directive. This allows connection pooling options to be defined
+  in ZCML.
+
 - works with sqlalchemy >= 0.5
  (would'nt work with sqlalchemy > 5 prior)
 

Modified: z3c.saconfig/trunk/src/z3c/saconfig/README.txt
===================================================================
--- z3c.saconfig/trunk/src/z3c/saconfig/README.txt	2010-07-05 12:38:03 UTC (rev 114204)
+++ z3c.saconfig/trunk/src/z3c/saconfig/README.txt	2010-07-05 13:56:36 UTC (rev 114205)
@@ -353,6 +353,25 @@
   ... </configure>"""))
   got: Engine(sqlite:///:memory:)
 
+It's also possible to specify connection pooling options:
+
+  >>> xmlconfig.xmlconfig(StringIO("""
+  ... <configure xmlns="http://namespaces.zope.org/db">
+  ...   <engine name="dummy" url="sqlite:///:memory:" 
+  ...       pool_size="1"
+  ...       max_overflow="2"
+  ...       pool_recycle="3"
+  ...       pool_timeout="4"
+  ...       />
+  ... </configure>"""))
+
+  >>> engineFactory = component.getUtility(IEngineFactory, name="dummy")
+  >>> engineFactory._kw == {'convert_unicode': False, 'echo': None, 'pool_size': 1, 'max_overflow': 2, 'pool_recycle': 3, 'pool_timeout': 4}
+  True
+
+(See the SQLAlchemy documentation on connection pooling for details on how
+these arguments are used.)
+
 The session directive is provided to register a scoped session utility:
 
   >>> xmlconfig.xmlconfig(StringIO("""

Modified: z3c.saconfig/trunk/src/z3c/saconfig/zcml.py
===================================================================
--- z3c.saconfig/trunk/src/z3c/saconfig/zcml.py	2010-07-05 12:38:03 UTC (rev 114204)
+++ z3c.saconfig/trunk/src/z3c/saconfig/zcml.py	2010-07-05 13:56:36 UTC (rev 114205)
@@ -45,7 +45,31 @@
         description=u'Callback for creating mappers etc. One argument is passed, the engine',
         required=False,
         default=None)
+
+    # Connection pooling options - probably only works on SQLAlchemy 0.5 and up
     
+    pool_size = zope.schema.Int(
+        title=u"The size of the pool to be maintained",
+        description=u"Defaults to 5 in SQLAlchemy.",
+        required=False)
+    
+    max_overflow = zope.schema.Int(
+        title=u"The maximum overflow size of the pool.",
+        description=u"When the number of checked-out connections reaches the " +
+                    u"size set in pool_size, additional connections will be " +
+                    u"returned up to this limit. Defaults to 10 in SQLAlchemy",
+        required=False)
+    
+    pool_recycle = zope.schema.Int(
+        title=u"Number of seconds between connection recycling",
+        description=u"Upon checkout, if this timeout is surpassed the connection "
+                    u"will be closed and replaced with a newly opened connection",
+        required=False)
+    
+    pool_timeout = zope.schema.Int(
+        title=u"The number of seconds to wait before giving up on returning a connection.",
+        description=u"Defaults to 30 in SQLAlchemy if not set",
+        required=False)
 
 class ISessionDirective(zope.interface.Interface):
     """Registers a database scoped session"""
@@ -74,11 +98,27 @@
         required=False,
         default="z3c.saconfig.utility.GloballyScopedSession")
 
-
-def engine(_context, url, name=u"", convert_unicode=False, echo=None, setup=None, twophase=False):
-    factory = utility.EngineFactory(
-        url, echo=echo, convert_unicode=convert_unicode)
+def engine(_context, url, name=u"", convert_unicode=False, echo=None, setup=None, twophase=False,
+    pool_size=None, max_overflow=None, pool_recycle=None, pool_timeout=None):
     
+    kwargs = {
+        'echo': echo,
+        'convert_unicode': convert_unicode,
+    }
+    
+    # Only add these if they're actually set, since we want to let SQLAlchemy
+    # control the defaults
+    if pool_size is not None:
+        kwargs['pool_size'] = pool_size
+    if max_overflow is not None:
+        kwargs['max_overflow'] = max_overflow
+    if pool_recycle is not None:
+        kwargs['pool_recycle'] = pool_recycle
+    if pool_timeout is not None:
+        kwargs['pool_timeout'] = pool_timeout
+    
+    factory = utility.EngineFactory(url, **kwargs)
+    
     zope.component.zcml.utility(
         _context,
         provides=interfaces.IEngineFactory,



More information about the checkins mailing list