~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to doc/core/pooling.html

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ożarowski
  • Date: 2011-08-01 23:18:16 UTC
  • mfrom: (1.4.15 upstream) (16.1.14 experimental)
  • Revision ID: james.westby@ubuntu.com-20110801231816-6lx797pi3q1fpqst
Tags: 0.7.2-1
* New upstream release
* Bump minimum required python-mako version to 0.4.1 (closes: 635898)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
        
8
8
        <title>
9
9
                Connection Pooling
10
 
             &mdash; SQLAlchemy 0.6.8 Documentation</title>
 
10
             &mdash; SQLAlchemy 0.7 Documentation</title>
11
11
        
12
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
13
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
15
15
    <script type="text/javascript">
16
16
      var DOCUMENTATION_OPTIONS = {
17
17
          URL_ROOT:    '../',
18
 
          VERSION:     '0.6.8',
 
18
          VERSION:     '0.7.2',
19
19
          COLLAPSE_MODINDEX: false,
20
20
          FILE_SUFFIX: '.html'
21
21
      };
27
27
    <link rel="index" title="Index" href="../genindex.html" />
28
28
    <link rel="search" title="Search" href="../search.html" />
29
29
        <link rel="copyright" title="Copyright" href="../copyright.html" />
30
 
    <link rel="top" title="SQLAlchemy 0.6.8 Documentation" href="../index.html" />
 
30
    <link rel="top" title="SQLAlchemy 0.7 Documentation" href="../index.html" />
31
31
        <link rel="up" title="SQLAlchemy Core" href="index.html" />
32
32
        <link rel="next" title="Schema Definition Language" href="schema.html" />
33
33
        <link rel="prev" title="Working with Engines and Connections" href="connections.html" />
38
38
 
39
39
 
40
40
 
41
 
<h1>SQLAlchemy 0.6.8 Documentation</h1>
 
41
<h1>SQLAlchemy 0.7 Documentation</h1>
42
42
 
43
43
<div id="search">
44
44
Search:
50
50
</div>
51
51
 
52
52
<div class="versionheader">
53
 
    Version: <span class="versionnum">0.6.8</span> Last Updated: 06/05/2011 13:10:26
 
53
    Release: <span class="versionnum">0.7.2</span> | Release Date: July 31, 2011
54
54
</div>
55
55
<div class="clearboth"></div>
56
56
 
72
72
        </ul>
73
73
    </div>
74
74
    <div id="navbanner">
75
 
        <a class="totoc" href="../index.html">SQLAlchemy 0.6.8 Documentation</a>
 
75
        <a class="totoc" href="../index.html">SQLAlchemy 0.7 Documentation</a>
76
76
                » <a href="index.html" title="SQLAlchemy Core">SQLAlchemy Core</a>
77
77
        » 
78
78
                Connection Pooling
90
90
<li><a class="reference internal" href="#using-a-custom-connection-function">Using a Custom Connection Function</a></li>
91
91
<li><a class="reference internal" href="#constructing-a-pool">Constructing a Pool</a></li>
92
92
<li><a class="reference internal" href="#pool-event-listeners">Pool Event Listeners</a></li>
93
 
<li><a class="reference internal" href="#builtin-pool-implementations">Builtin Pool Implementations</a></li>
 
93
<li><a class="reference internal" href="#api-documentation-available-pool-implementations">API Documentation - Available Pool Implementations</a></li>
94
94
<li><a class="reference internal" href="#pooling-plain-db-api-connections">Pooling Plain DB-API Connections</a></li>
95
95
</ul>
96
96
</li>
130
130
<div class="highlight-python"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;postgresql://me@localhost/mydb&#39;</span><span class="p">,</span>
131
131
                       <span class="n">pool_size</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">max_overflow</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></pre></div>
132
132
</div>
133
 
<p>In the case of SQLite, a <a class="reference internal" href="#sqlalchemy.pool.SingletonThreadPool" title="sqlalchemy.pool.SingletonThreadPool"><tt class="xref py py-class docutils literal"><span class="pre">SingletonThreadPool</span></tt></a> is provided instead,
134
 
to provide compatibility with SQLite&#8217;s restricted threading model, as well
135
 
as to provide a reasonable default behavior to SQLite &#8220;memory&#8221; databases,
136
 
which maintain their entire dataset within the scope of a single connection.</p>
 
133
<p>In the case of SQLite, the <a class="reference internal" href="#sqlalchemy.pool.SingletonThreadPool" title="sqlalchemy.pool.SingletonThreadPool"><tt class="xref py py-class docutils literal"><span class="pre">SingletonThreadPool</span></tt></a> or
 
134
<a class="reference internal" href="#sqlalchemy.pool.NullPool" title="sqlalchemy.pool.NullPool"><tt class="xref py py-class docutils literal"><span class="pre">NullPool</span></tt></a> are selected by the dialect to provide
 
135
greater compatibility with SQLite&#8217;s threading and locking
 
136
model, as well as to provide a reasonable default behavior
 
137
to SQLite &#8220;memory&#8221; databases, which maintain their entire
 
138
dataset within the scope of a single connection.</p>
137
139
<p>All SQLAlchemy pool implementations have in common
138
140
that none of them &#8220;pre create&#8221; connections - all implementations wait
139
141
until first use before creating a connection.   At that point, if
230
232
upon first connect, upon each new connection, and upon checkout and
231
233
checkin of connections.   See <a class="reference internal" href="interfaces.html#sqlalchemy.interfaces.PoolListener" title="sqlalchemy.interfaces.PoolListener"><tt class="xref py py-class docutils literal"><span class="pre">PoolListener</span></tt></a> for details.</p>
232
234
</div>
233
 
<div class="section" id="builtin-pool-implementations">
234
 
<h2>Builtin Pool Implementations<a class="headerlink" href="#builtin-pool-implementations" title="Permalink to this headline">¶</a></h2>
 
235
<div class="section" id="api-documentation-available-pool-implementations">
 
236
<h2>API Documentation - Available Pool Implementations<a class="headerlink" href="#api-documentation-available-pool-implementations" title="Permalink to this headline">¶</a></h2>
235
237
<dl class="class">
236
238
<dt id="sqlalchemy.pool.Pool">
237
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">Pool</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.Pool" title="Permalink to this definition">¶</a></dt>
 
239
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">Pool</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em>, <em>events=None</em>, <em>_dispatch=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.Pool" title="Permalink to this definition">¶</a></dt>
238
240
<dd><p>Abstract base class for connection pools.</p>
239
241
<dl class="method">
240
242
<dt id="sqlalchemy.pool.Pool.__init__">
241
 
<tt class="descname">__init__</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.Pool.__init__" title="Permalink to this definition">¶</a></dt>
 
243
<tt class="descname">__init__</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em>, <em>events=None</em>, <em>_dispatch=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.Pool.__init__" title="Permalink to this definition">¶</a></dt>
242
244
<dd><p>Construct a Pool.</p>
243
245
<table class="docutils field-list" frame="void" rules="none">
244
246
<col class="field-name" />
273
275
connections returned to the pool.  This is typically a
274
276
ROLLBACK to release locks and transaction resources.
275
277
Disable at your own peril.  Defaults to True.</li>
276
 
<li><strong>listeners</strong> &#8211; A list of
 
278
<li><strong>events</strong> &#8211; a list of 2-tuples, each of the form
 
279
<tt class="docutils literal"><span class="pre">(callable,</span> <span class="pre">target)</span></tt> which will be passed to event.listen()
 
280
upon construction.   Provided here so that event listeners
 
281
can be assigned via <tt class="docutils literal"><span class="pre">create_engine</span></tt> before dialect-level
 
282
listeners are applied.</li>
 
283
<li><strong>listeners</strong> &#8211; Deprecated.  A list of
277
284
<a class="reference internal" href="interfaces.html#sqlalchemy.interfaces.PoolListener" title="sqlalchemy.interfaces.PoolListener"><tt class="xref py py-class docutils literal"><span class="pre">PoolListener</span></tt></a>-like objects or
278
285
dictionaries of callables that receive events when DB-API
279
286
connections are created, checked out and checked in to the
280
 
pool.</li>
 
287
pool.  This has been superseded by 
 
288
<a class="reference internal" href="event.html#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">listen()</span></tt></a>.</li>
281
289
</ul>
282
290
</td>
283
291
</tr>
320
328
<dt id="sqlalchemy.pool.QueuePool">
321
329
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">QueuePool</tt><big>(</big><em>creator</em>, <em>pool_size=5</em>, <em>max_overflow=10</em>, <em>timeout=30</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.QueuePool" title="Permalink to this definition">¶</a></dt>
322
330
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.pool.Pool</span></tt></a></p>
323
 
<p>A Pool that imposes a limit on the number of open connections.</p>
 
331
<p>A <a class="reference internal" href="#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt></a> that imposes a limit on the number of open connections.</p>
 
332
<p><a class="reference internal" href="#sqlalchemy.pool.QueuePool" title="sqlalchemy.pool.QueuePool"><tt class="xref py py-class docutils literal"><span class="pre">QueuePool</span></tt></a> is the default pooling implementation used for 
 
333
all <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Engine" title="sqlalchemy.engine.base.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> objects, unless the SQLite dialect is in use.</p>
324
334
<dl class="method">
325
335
<dt id="sqlalchemy.pool.QueuePool.__init__">
326
336
<tt class="descname">__init__</tt><big>(</big><em>creator</em>, <em>pool_size=5</em>, <em>max_overflow=10</em>, <em>timeout=30</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.QueuePool.__init__" title="Permalink to this definition">¶</a></dt>
397
407
<p>A Pool that maintains one connection per thread.</p>
398
408
<p>Maintains one connection per each thread, never moving a connection to a
399
409
thread other than the one which it was created in.</p>
400
 
<p>This is used for SQLite, which both does not handle multithreading by
401
 
default, and also requires a singleton connection if a :memory: database
402
 
is being used.</p>
403
410
<p>Options are the same as those of <a class="reference internal" href="#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt></a>, as well as:</p>
404
411
<table class="docutils field-list" frame="void" rules="none">
405
412
<col class="field-name" />
410
417
</tr>
411
418
</tbody>
412
419
</table>
 
420
<p><a class="reference internal" href="#sqlalchemy.pool.SingletonThreadPool" title="sqlalchemy.pool.SingletonThreadPool"><tt class="xref py py-class docutils literal"><span class="pre">SingletonThreadPool</span></tt></a> is used by the SQLite dialect
 
421
automatically when a memory-based database is used.
 
422
See <a class="reference internal" href="../dialects/sqlite.html"><em>SQLite</em></a>.</p>
413
423
<dl class="method">
414
424
<dt id="sqlalchemy.pool.SingletonThreadPool.__init__">
415
425
<tt class="descname">__init__</tt><big>(</big><em>creator</em>, <em>pool_size=5</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.SingletonThreadPool.__init__" title="Permalink to this definition">¶</a></dt>
421
431
<dt id="sqlalchemy.pool.AssertionPool">
422
432
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">AssertionPool</tt><big>(</big><em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.AssertionPool" title="Permalink to this definition">¶</a></dt>
423
433
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.pool.Pool</span></tt></a></p>
424
 
<p>A Pool that allows at most one checked out connection at any given
 
434
<p>A <a class="reference internal" href="#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt></a> that allows at most one checked out connection at any given
425
435
time.</p>
426
436
<p>This will raise an exception if more than one connection is checked out
427
437
at a time.  Useful for debugging code that is using more connections
428
438
than desired.</p>
 
439
<p><a class="reference internal" href="#sqlalchemy.pool.AssertionPool" title="sqlalchemy.pool.AssertionPool"><tt class="xref py py-class docutils literal"><span class="pre">AssertionPool</span></tt></a> also logs a traceback of where
 
440
the original connection was checked out, and reports
 
441
this in the assertion error raised (new in 0.7).</p>
429
442
</dd></dl>
430
443
 
431
444
<dl class="class">
432
445
<dt id="sqlalchemy.pool.NullPool">
433
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">NullPool</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.NullPool" title="Permalink to this definition">¶</a></dt>
 
446
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">NullPool</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em>, <em>events=None</em>, <em>_dispatch=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.NullPool" title="Permalink to this definition">¶</a></dt>
434
447
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.pool.Pool</span></tt></a></p>
435
448
<p>A Pool which does not pool connections.</p>
436
449
<p>Instead it literally opens and closes the underlying DB-API connection
438
451
<p>Reconnect-related functions such as <tt class="docutils literal"><span class="pre">recycle</span></tt> and connection
439
452
invalidation are not supported by this Pool implementation, since
440
453
no connections are held persistently.</p>
 
454
<p><a class="reference internal" href="#sqlalchemy.pool.NullPool" title="sqlalchemy.pool.NullPool"><tt class="xref py py-class docutils literal"><span class="pre">NullPool</span></tt></a> is used by the SQlite dilalect automatically
 
455
when a file-based database is used (as of SQLAlchemy 0.7).
 
456
See <a class="reference internal" href="../dialects/sqlite.html"><em>SQLite</em></a>.</p>
441
457
</dd></dl>
442
458
 
443
459
<dl class="class">
444
460
<dt id="sqlalchemy.pool.StaticPool">
445
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">StaticPool</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.StaticPool" title="Permalink to this definition">¶</a></dt>
 
461
<em class="property">class </em><tt class="descclassname">sqlalchemy.pool.</tt><tt class="descname">StaticPool</tt><big>(</big><em>creator</em>, <em>recycle=-1</em>, <em>echo=None</em>, <em>use_threadlocal=False</em>, <em>logging_name=None</em>, <em>reset_on_return=True</em>, <em>listeners=None</em>, <em>events=None</em>, <em>_dispatch=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.pool.StaticPool" title="Permalink to this definition">¶</a></dt>
446
462
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.pool.Pool" title="sqlalchemy.pool.Pool"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.pool.Pool</span></tt></a></p>
447
463
<p>A Pool of exactly one connection, used for all requests.</p>
448
464
<p>Reconnect-related functions such as <tt class="docutils literal"><span class="pre">recycle</span></tt> and connection