This file is indexed.

/usr/share/doc/python-repoze.tm2-doc/html/index.html is in python-repoze.tm2-doc 2.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Documentation for repoze.tm2 (repoze.tm fork) &mdash; repoze.tm2 2.0b1 documentation</title>
    
    <link rel="stylesheet" href="_static/repoze.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '2.0b1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="repoze.tm2 2.0b1 documentation" href="#" />
    <link rel="next" title="repoze.tm" href="api.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="api.html" title="repoze.tm"
             accesskey="N">next</a> |</li>
        <li><a href="#">repoze.tm2 2.0b1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="documentation-for-repoze-tm2-repoze-tm-fork">
<h1>Documentation for repoze.tm2 (<tt class="docutils literal"><span class="pre">repoze.tm</span></tt> fork)<a class="headerlink" href="#documentation-for-repoze-tm2-repoze-tm-fork" title="Permalink to this headline"></a></h1>
<div class="section" id="overview">
<h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline"></a></h2>
<p><tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> is WSGI middleware which uses the <tt class="docutils literal"><span class="pre">ZODB</span></tt> package&#8217;s
transaction manager to wrap a call to its pipeline children inside a
transaction.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> is equivalent to the <a class="reference internal" href="api.html#module-repoze.tm" title="repoze.tm"><tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm</span></tt></a>
package (it was forked from <a class="reference internal" href="api.html#module-repoze.tm" title="repoze.tm"><tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm</span></tt></a>), except it has a
dependency only on the <tt class="docutils literal"><span class="pre">transaction</span></tt> package rather than a
dependency on the entire <tt class="docutils literal"><span class="pre">ZODB3</span></tt> package (<tt class="docutils literal"><span class="pre">ZODB3</span></tt> 3.8 ships
with the <tt class="docutils literal"><span class="pre">transaction</span></tt> package right now).  It is an error to
install both repoze.tm and repoze.tm2 into the same environment, as
they provide the same entry points and import points.</p>
</div>
</div>
<div class="section" id="behavior">
<h2>Behavior<a class="headerlink" href="#behavior" title="Permalink to this headline"></a></h2>
<p>When this middleware is present in the WSGI pipeline, a new
transaction will be started once a WSGI request makes it to the
middleware.  If any downstream application raises an
exception, the transaction will be aborted, otherwise the transaction
will be committed.  Any &#8220;data managers&#8221; participating in the
transaction will be aborted or committed respectively.  A ZODB
&#8220;connection&#8221; is an example of a data manager.</p>
<p>Since this is a tiny wrapper around the ZODB transaction module, and
the ZODB transaction module is &#8220;thread-safe&#8221; (in the sense that its
default policy is to create a new transaction for each thread), it
should be fine to use in either multiprocess or multithread
environments.</p>
</div>
<div class="section" id="purpose-and-usage">
<h2>Purpose and Usage<a class="headerlink" href="#purpose-and-usage" title="Permalink to this headline"></a></h2>
<p>The ZODB transaction manager is a completely generic transaction
manager.  It can be used independently of the actual &#8220;object database&#8221;
part of ZODB.  One of the purposes of creating <a class="reference internal" href="api.html#module-repoze.tm" title="repoze.tm"><tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm</span></tt></a> was to
allow for systems other than Zope to make use of two-phase commit
transactions in a WSGI context.</p>
<p>Let&#8217;s pretend we have an existing system that places data into a
relational database when someone submits a form.  The system has been
running for a while, and our code handles the database commit and
rollback for us explicitly; if the form processing succeeds, our code
commits the database transaction.  If it fails, our code rolls back
the database transaction.  Everything works fine.</p>
<p>Now our customer asks us if we can also place data into another
separate relational database when the form is submitted as well as
continuing to place data in the original database.  We need to put
data in both databases, and if we want to ensure that no records exist
in one that don&#8217;t exist in the other as a result of a form submission,
we&#8217;re going to need to do a pretty complicated commit and rollback
dance in each place in our code which needs to write to both data
stores.  We can&#8217;t just blindly commit one, then commit the other,
because the second commit may fail and we&#8217;ll be left with &#8220;orphan&#8221;
data in the first, and we&#8217;ll either need to clean it up manually or
leave it there to trip over later.</p>
<p>A transaction manager helps us ensure that no data is committed to
either database unless both participating data stores can commit.
Once the transaction manager determines that both data stores are
willing to commit, it will commit them both in very quick succession,
so that there is only a minimal chance that the second data store will
fail to commit.  If it does, the system will raise an error that makes
it impossible to begin another transaction until the system restarts,
so the damage is minimized.  In practice, this error almost never
occurs unless the code that interfaces the database to the transaction
manager has a bug.</p>
</div>
<div class="section" id="adding-repoze-tm2-to-your-wsgi-pipeline">
<h2>Adding <tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> To Your WSGI Pipeline<a class="headerlink" href="#adding-repoze-tm2-to-your-wsgi-pipeline" title="Permalink to this headline"></a></h2>
<p>Via <tt class="docutils literal"><span class="pre">PasteDeploy</span></tt> .INI configuration:</p>
<div class="highlight-python"><div class="highlight"><pre>[pipeline:main]
 pipeline =
         egg:repoze.tm2#tm
         myapp
</pre></div>
</div>
<p>Via Python:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">otherplace</span> <span class="kn">import</span> <span class="n">mywsgiapp</span>

<span class="kn">from</span> <span class="nn">repoze.tm</span> <span class="kn">import</span> <span class="n">TM</span>
<span class="n">new_wsgiapp</span> <span class="o">=</span> <span class="n">TM</span><span class="p">(</span><span class="n">mywsgiapp</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="using-a-commit-veto">
<h2>Using A Commit Veto<a class="headerlink" href="#using-a-commit-veto" title="Permalink to this headline"></a></h2>
<p>If you&#8217;d like to veto commits based on the status code returned by the
downstream application, use a commit veto callback.</p>
<p>First, define the callback somewhere in your application:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">commit_veto</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">header_name</span><span class="p">,</span> <span class="n">header_value</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">header_name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;x-tm&#39;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">header_value</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s">&#39;commit&#39;</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">False</span>
            <span class="k">return</span> <span class="bp">True</span>
    <span class="k">for</span> <span class="n">bad</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;4&#39;</span><span class="p">,</span> <span class="s">&#39;5&#39;</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">status</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">bad</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">True</span>
    <span class="k">return</span> <span class="bp">False</span>
</pre></div>
</div>
<p>Then configure it into your middleware.</p>
<p>Via Python:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">otherplace</span> <span class="kn">import</span> <span class="n">mywsgiapp</span>
<span class="kn">from</span> <span class="nn">my.package</span> <span class="kn">import</span> <span class="n">commit_veto</span>

<span class="kn">from</span> <span class="nn">repoze.tm</span> <span class="kn">import</span> <span class="n">TM</span>
<span class="n">new_wsgiapp</span> <span class="o">=</span> <span class="n">TM</span><span class="p">(</span><span class="n">mywsgiapp</span><span class="p">,</span> <span class="n">commit_veto</span><span class="o">=</span><span class="n">commit_veto</span><span class="p">)</span>
</pre></div>
</div>
<p>Via PasteDeploy:</p>
<div class="highlight-ini"><div class="highlight"><pre><span class="k">[filter:tm]</span>
<span class="na">commit_veto</span> <span class="o">=</span> <span class="s">my.package:commit_veto</span>
</pre></div>
</div>
<p>In the PasteDeploy example, the path is a Python dotted name, where the dots
separate module and package names, and the colon separates a module from its
contents.  In the above example, the code would be implemented as a
&#8220;commit_veto&#8221; function which lives in the &#8220;package&#8221; submodule of the &#8220;my&#8221;
package.</p>
<p>A variant of the commit veto implementation shown above as an example is
actually present in the <tt class="docutils literal"><span class="pre">repoze.tm2</span></tt> package as
<tt class="docutils literal"><span class="pre">repoze.tm.default_commit_veto</span></tt>.  It&#8217;s fairly general, so you needn&#8217;t
implement one yourself.  Instead just use it.</p>
<p>Via Python:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">otherplace</span> <span class="kn">import</span> <span class="n">mywsgiapp</span>
<span class="kn">from</span> <span class="nn">repoze.tm</span> <span class="kn">import</span> <span class="n">default_commit_veto</span>

<span class="kn">from</span> <span class="nn">repoze.tm</span> <span class="kn">import</span> <span class="n">TM</span>
<span class="n">new_wsgiapp</span> <span class="o">=</span> <span class="n">TM</span><span class="p">(</span><span class="n">mywsgiapp</span><span class="p">,</span> <span class="n">commit_veto</span><span class="o">=</span><span class="n">default_commit_veto</span><span class="p">)</span>
</pre></div>
</div>
<p>Via PasteDeploy:</p>
<div class="highlight-ini"><div class="highlight"><pre><span class="k">[filter:tm]</span>
<span class="na">commit_veto</span> <span class="o">=</span> <span class="s">repoze.tm:default_commit_veto</span>
</pre></div>
</div>
<p>API documentation for <tt class="docutils literal"><span class="pre">default_commit_veto</span></tt> exists at
<a class="reference internal" href="api.html#repoze.tm.default_commit_veto" title="repoze.tm.default_commit_veto"><tt class="xref py py-func docutils literal"><span class="pre">repoze.tm.default_commit_veto()</span></tt></a>.</p>
</div>
<div class="section" id="mocking-up-a-data-manager">
<h2>Mocking Up A Data Manager<a class="headerlink" href="#mocking-up-a-data-manager" title="Permalink to this headline"></a></h2>
<p>The piece of code you need to write in order to participate in ZODB
transactions is called a &#8216;data manager&#8217;.  It is typically a class.
Here&#8217;s the interface that you need to implement in the code for a data
manager:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">IDataManager</span><span class="p">(</span><span class="n">zope</span><span class="o">.</span><span class="n">interface</span><span class="o">.</span><span class="n">Interface</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Objects that manage transactional storage.</span>

<span class="sd">    These objects may manage data for other objects, or they</span>
<span class="sd">    may manage non-object storages, such as relational</span>
<span class="sd">    databases.  For example, a ZODB.Connection.</span>

<span class="sd">    Note that when some data is modified, that data&#39;s data</span>
<span class="sd">    manager should join a transaction so that data can be</span>
<span class="sd">    committed when the user commits the transaction.  &quot;&quot;&quot;</span>

    <span class="n">transaction_manager</span> <span class="o">=</span> <span class="n">zope</span><span class="o">.</span><span class="n">interface</span><span class="o">.</span><span class="n">Attribute</span><span class="p">(</span>
        <span class="sd">&quot;&quot;&quot;The transaction manager (TM) used by this data</span>
<span class="sd">        manager.</span>

<span class="sd">        This is a public attribute, intended for read-only</span>
<span class="sd">        use.  The value is an instance of ITransactionManager,</span>
<span class="sd">        typically set by the data manager&#39;s constructor.  &quot;&quot;&quot;</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">abort</span><span class="p">(</span><span class="n">transaction</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Abort a transaction and forget all changes.</span>

<span class="sd">        Abort must be called outside of a two-phase commit.</span>

<span class="sd">        Abort is called by the transaction manager to abort transactions</span>
<span class="sd">        that are not yet in a two-phase commit.</span>
<span class="sd">        &quot;&quot;&quot;</span>

    <span class="c"># Two-phase commit protocol.  These methods are called by</span>
    <span class="c"># the ITransaction object associated with the transaction</span>
    <span class="c"># being committed.  The sequence of calls normally follows</span>
    <span class="c"># this regular expression: tpc_begin commit tpc_vote</span>
    <span class="c"># (tpc_finish | tpc_abort)</span>

    <span class="k">def</span> <span class="nf">tpc_begin</span><span class="p">(</span><span class="n">transaction</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;Begin commit of a transaction, starting the</span>
<span class="sd">        two-phase commit.</span>

<span class="sd">        transaction is the ITransaction instance associated with the</span>
<span class="sd">        transaction being committed.</span>
<span class="sd">        &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">commit</span><span class="p">(</span><span class="n">transaction</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;Commit modifications to registered objects.</span>

<span class="sd">        Save changes to be made persistent if the transaction</span>
<span class="sd">        commits (if tpc_finish is called later).  If tpc_abort</span>
<span class="sd">        is called later, changes must not persist.</span>

<span class="sd">        This includes conflict detection and handling.  If no</span>
<span class="sd">        conflicts or errors occur, the data manager should be</span>
<span class="sd">        prepared to make the changes persist when tpc_finish</span>
<span class="sd">        is called.  &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">tpc_vote</span><span class="p">(</span><span class="n">transaction</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Verify that a data manager can commit the transaction.</span>

<span class="sd">        This is the last chance for a data manager to vote &#39;no&#39;.  A</span>
<span class="sd">        data manager votes &#39;no&#39; by raising an exception.</span>

<span class="sd">        transaction is the ITransaction instance associated with the</span>
<span class="sd">        transaction being committed.</span>
<span class="sd">        &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">tpc_finish</span><span class="p">(</span><span class="n">transaction</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;Indicate confirmation that the transaction is done.</span>

<span class="sd">        Make all changes to objects modified by this</span>
<span class="sd">        transaction persist.</span>

<span class="sd">        transaction is the ITransaction instance associated</span>
<span class="sd">        with the transaction being committed.</span>

<span class="sd">        This should never fail.  If this raises an exception,</span>
<span class="sd">        the database is not expected to maintain consistency;</span>
<span class="sd">        it&#39;s a serious error.  &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">tpc_abort</span><span class="p">(</span><span class="n">transaction</span><span class="p">):</span>

        <span class="sd">&quot;&quot;&quot;Abort a transaction.</span>

<span class="sd">        This is called by a transaction manager to end a</span>
<span class="sd">        two-phase commit on the data manager.  Abandon all</span>
<span class="sd">        changes to objects modified by this transaction.</span>

<span class="sd">        transaction is the ITransaction instance associated</span>
<span class="sd">        with the transaction being committed.</span>

<span class="sd">        This should never fail.</span>
<span class="sd">        &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">sortKey</span><span class="p">():</span>

        <span class="sd">&quot;&quot;&quot;Return a key to use for ordering registered</span>
<span class="sd">        DataManagers.</span>

<span class="sd">        ZODB uses a global sort order to prevent deadlock when</span>
<span class="sd">        it commits transactions involving multiple resource</span>
<span class="sd">        managers.  The resource manager must define a</span>
<span class="sd">        sortKey() method that provides a global ordering for</span>
<span class="sd">        resource managers.  &quot;&quot;&quot;</span>
        <span class="c"># Alternate version:</span>
        <span class="c">#&quot;&quot;&quot;Return a consistent sort key for this connection.</span>
        <span class="c"># #This allows ordering multiple connections that use</span>
        <span class="n">the</span> <span class="n">same</span> <span class="n">storage</span> <span class="ow">in</span> <span class="c">#a consistent manner. This is</span>
        <span class="n">unique</span> <span class="k">for</span> <span class="n">the</span> <span class="n">lifetime</span> <span class="n">of</span> <span class="n">a</span> <span class="n">connection</span><span class="p">,</span> <span class="c">#which is</span>
        <span class="n">good</span> <span class="n">enough</span> <span class="n">to</span> <span class="n">avoid</span> <span class="n">ZEO</span> <span class="n">deadlocks</span><span class="o">.</span>  <span class="c">#&quot;&quot;&quot;</span>
</pre></div>
</div>
<p>Let&#8217;s implement a mock data manager.  Our mock data manager will write
data to a file if the transaction commits.  It will not write data to
a file if the transaction aborts:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MockDataManager</span><span class="p">:</span>

    <span class="n">transaction_manager</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">path</span>

    <span class="k">def</span> <span class="nf">abort</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transaction</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">tpc_begin</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transaction</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">commit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transaction</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">tempfile</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tempfn</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mktemp</span><span class="p">()</span>
        <span class="n">temp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tempfn</span><span class="p">,</span> <span class="s">&#39;wb&#39;</span><span class="p">)</span>
        <span class="n">temp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
        <span class="n">temp</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
        <span class="n">temp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">tpc_vote</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transaction</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">os</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tempfn</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> doesnt exist&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">tempfn</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;file already exists&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tpc_finish</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transaction</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">os</span>
        <span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tempfn</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tpc_abort</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">transaction</span><span class="p">):</span>
        <span class="kn">import</span> <span class="nn">os</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tempfn</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="k">pass</span>
</pre></div>
</div>
<p>We can create a datamanager and join it into the currently running
transaction:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">dm</span> <span class="o">=</span> <span class="n">MockDataManager</span><span class="p">(</span><span class="s">&#39;heres the data&#39;</span><span class="p">,</span>  <span class="s">&#39;/tmp/file&#39;</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">transaction</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">transaction</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="n">t</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">dm</span><span class="p">)</span>
</pre></div>
</div>
<p>When the transaction commits, a file will be placed in &#8216;/tmp/file&#8217;
containing &#8216;heres the data&#8217;.  If the transaction aborts, no file will
be created.</p>
<p>If more than one data manager is joined to the transaction, all of
them must be willing to commit or the entire transaction is aborted
and none of them commit.  If you can imagine creating two of the mock
data managers we&#8217;ve made within application code, if one has a problem
during &#8220;tpc_vote&#8221;, neither will actually write a file to the ultimate
location, and thus your application consistency is maintained.</p>
</div>
<div class="section" id="integrating-your-data-manager-with-repoze-tm2">
<h2>Integrating Your Data Manager With <tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt><a class="headerlink" href="#integrating-your-data-manager-with-repoze-tm2" title="Permalink to this headline"></a></h2>
<p>The <tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> transaction management machinery has an implicit
policy.  When it is in the WSGI pipeline, a transaction is started
when the middleware is invoked.  Thus, in your application code,
calling &#8220;import transaction; transaction.get()&#8221; will return the
transaction object created by the <tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> middleware.  You
needn&#8217;t call t.commit() or t.abort() within your application code.
You only need to call t.join, to register your data manager with the
transaction.  <tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> will abort the transaction if an
exception is raised by your application code or lower middleware
before it returns a WSGI response.  If your application or lower
middleware raises an exception, the transaction is aborted.</p>
</div>
<div class="section" id="cleanup">
<h2>Cleanup<a class="headerlink" href="#cleanup" title="Permalink to this headline"></a></h2>
<p>When the <tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> middleware is in the WSGI pipeline, a boolean
key is present in the environment (<tt class="docutils literal"><span class="pre">repoze.tm.active</span></tt>).  A utility
function named <tt class="xref py py-func docutils literal"><span class="pre">repoze.tm.isActive()</span></tt> can be imported and passed the
WSGI environment to check for activation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">repoze.tm</span> <span class="kn">import</span> <span class="n">isActive</span>
<span class="n">tm_active</span> <span class="o">=</span> <span class="n">isActive</span><span class="p">(</span><span class="n">wsgi_environment</span><span class="p">)</span>
</pre></div>
</div>
<p>If an application needs to perform an action after a transaction ends, the
<a class="reference internal" href="api.html#repoze.tm.after_end" title="repoze.tm.after_end"><tt class="xref py py-attr docutils literal"><span class="pre">repoze.tm.after_end</span></tt></a> registry may be used to register a callback.
This object is an instance fo the <a class="reference internal" href="api.html#repoze.tm.AfterEnd" title="repoze.tm.AfterEnd"><tt class="xref py py-class docutils literal"><span class="pre">repoze.tm.AfterEnd</span></tt></a> class.  The
<tt class="xref py py-meth docutils literal"><span class="pre">repoze.tm.AfterEnd.register()</span></tt> method accepts a callback (accepting no
arguments) and a transaction instance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">repoze.tm</span> <span class="kn">import</span> <span class="n">after_end</span>
<span class="kn">import</span> <span class="nn">transaction</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">transaction</span><span class="o">.</span><span class="n">get</span><span class="p">()</span> <span class="c"># the current transaction</span>
<span class="k">def</span> <span class="nf">func</span><span class="p">():</span>
    <span class="k">pass</span> <span class="c"># close a connection, etc</span>
<span class="n">after_end</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
</pre></div>
</div>
<p>&#8220;after_end&#8221; callbacks should only be registered when the transaction
manager is active, or a memory leak will result (registration cleanup
happens only on transaction commit or abort, which is managed by
<tt class="xref py py-mod docutils literal"><span class="pre">repoze.tm2</span></tt> while in the pipeline).</p>
</div>
<div class="section" id="further-documentation">
<h2>Further Documentation<a class="headerlink" href="#further-documentation" title="Permalink to this headline"></a></h2>
<p>Many database adapters written for Zope (e.g. for Postgres, MySQL,
etc) use this transaction manager, so it should be possible to take a
look in these places to see how to implement a more real-world
transaction-aware database connector that uses this module in non-Zope
applications:</p>
<ul class="simple">
<li><a class="reference external" href="http://svn.zope.org/ZODB/branches/3.7/src/transaction/">http://svn.zope.org/ZODB/branches/3.7/src/transaction/</a></li>
<li><a class="reference external" href="http://svn.zope.org/ZODB/branches/3.8/src/transaction/">http://svn.zope.org/ZODB/branches/3.8/src/transaction/</a></li>
<li><a class="reference external" href="http://mysql-python.sourceforge.net/">http://mysql-python.sourceforge.net/</a> (ZMySQLDA)</li>
<li><a class="reference external" href="http://www.initd.org/svn/psycopg/psycopg2/trunk/">http://www.initd.org/svn/psycopg/psycopg2/trunk/</a> (ZPsycoPGDA)</li>
</ul>
</div>
<div class="section" id="contacting">
<h2>Contacting<a class="headerlink" href="#contacting" title="Permalink to this headline"></a></h2>
<p>The <a class="reference external" href="http://lists.repoze.org/mailman/listinfo/repoze-dev">repoze-dev maillist</a> should be used
for communications about this software.</p>
</div>
<div class="section" id="api-docs">
<h2>API Docs<a class="headerlink" href="#api-docs" title="Permalink to this headline"></a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="api.html"><tt class="docutils literal"><span class="pre">repoze.tm</span></tt></a></li>
</ul>
</div>
</div>
<div class="section" id="change-logs">
<h2>Change Logs<a class="headerlink" href="#change-logs" title="Permalink to this headline"></a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="changes.html"><tt class="docutils literal"><span class="pre">repoze.tm</span></tt> Change History</a></li>
<li class="toctree-l1"><a class="reference internal" href="changes.html#changelog">Changelog</a><ul>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id1">2.0 (2013-06-26)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#b1-2013-04-05">2.0b1 (2013-04-05)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id2">1.0 (2012-03-24)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#b2-2011-07-18">1.0b2 (2011-07-18)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#b1-2011-01-19">1.0b1 (2011-01-19)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#a5-2009-09-07">1.0a5 (2009-09-07)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#a4-2009-01-06">1.0a4 (2009-01-06)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#a3-2008-08-03">1.0a3 (2008-08-03)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#a2-2008-07-15">1.0a2 (2008-07-15)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#a1-2008-01-09">1.0a1 (2008-01-09)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id3">0.8 (2007-10-11)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id4">0.7 (2007-09-25)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id5">0.6 (2007-09-21)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id6">0.5 (2007-09-18)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id7">0.4 (2007-09-17)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id8">0.3 (2007-09-14)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id9">0.2 (2007-09-13)</a></li>
<li class="toctree-l2"><a class="reference internal" href="changes.html#id10">0.1 (2007-09-10)</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="indices-and-tables">
<h2>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline"></a></h2>
<ul class="simple">
<li><a class="reference internal" href="genindex.html"><em>Index</em></a></li>
<li><a class="reference internal" href="py-modindex.html"><em>Module Index</em></a></li>
<li><a class="reference internal" href="search.html"><em>Search Page</em></a></li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="#">
              <img class="logo" src="_static/logo_hi.gif" alt="Logo"/>
            </a></p>
  <h3><a href="#">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Documentation for repoze.tm2 (<tt class="docutils literal"><span class="pre">repoze.tm</span></tt> fork)</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#behavior">Behavior</a></li>
<li><a class="reference internal" href="#purpose-and-usage">Purpose and Usage</a></li>
<li><a class="reference internal" href="#adding-repoze-tm2-to-your-wsgi-pipeline">Adding <tt class="docutils literal"><span class="pre">repoze.tm2</span></tt> To Your WSGI Pipeline</a></li>
<li><a class="reference internal" href="#using-a-commit-veto">Using A Commit Veto</a></li>
<li><a class="reference internal" href="#mocking-up-a-data-manager">Mocking Up A Data Manager</a></li>
<li><a class="reference internal" href="#integrating-your-data-manager-with-repoze-tm2">Integrating Your Data Manager With <tt class="docutils literal"><span class="pre">repoze.tm2</span></tt></a></li>
<li><a class="reference internal" href="#cleanup">Cleanup</a></li>
<li><a class="reference internal" href="#further-documentation">Further Documentation</a></li>
<li><a class="reference internal" href="#contacting">Contacting</a></li>
<li><a class="reference internal" href="#api-docs">API Docs</a></li>
<li><a class="reference internal" href="#change-logs">Change Logs</a></li>
<li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
</ul>
</li>
</ul>

  <h4>Next topic</h4>
  <p class="topless"><a href="api.html"
                        title="next chapter"><tt class="docutils literal"><span class="pre">repoze.tm</span></tt></a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/index.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="api.html" title="repoze.tm"
             >next</a> |</li>
        <li><a href="#">repoze.tm2 2.0b1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2008-2013, Repoze Developers &lt;repoze-dev@lists.repoze.org&gt;.
      Last updated on Oct 05, 2014.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
    </div>
  </body>
</html>