File: transaction.txt

package info (click to toggle)
gaphor 0.13.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,692 kB
  • ctags: 2,971
  • sloc: python: 19,981; xml: 247; makefile: 54; sh: 40
file content (108 lines) | stat: -rw-r--r-- 2,861 bytes parent folder | download | duplicates (2)
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
Transaction support for Gaphor
==============================

Transaction support is located in module gaphor.transaction:

    >>> from gaphor import transaction

The Transaction class is used mainly to signal the begin and end of a transaction. This is done by the TransactionBegin, TransactionCommit and TransactionRollback events:

    >>> from zope import component
    >>> @component.adapter(transaction.TransactionBegin)
    ... def transaction_begin_handler(event):
    ...     print 'tx begin'
    >>> component.provideHandler(transaction_begin_handler)

Same goes for commit and rollback events:

    >>> @component.adapter(transaction.TransactionCommit)
    ... def transaction_commit_handler(event):
    ...     print 'tx commit'
    >>> component.provideHandler(transaction_commit_handler)
    >>> @component.adapter(transaction.TransactionRollback)
    ... def transaction_rollback_handler(event):
    ...     print 'tx rollback'
    >>> component.provideHandler(transaction_rollback_handler)


A Transaction is started by initiating a Transaction instance:

    >>> tx = transaction.Transaction()
    tx begin

On success, a transaction can be committed:

    >>> tx.commit()
    tx commit

After a commit, a rollback is no longer allowed (the transaction is closed):

    >>> tx.rollback()
    ... # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    TransactionError: No Transaction on stack.


Transactions may be nested:

    >>> tx = transaction.Transaction()
    tx begin
    >>> tx2 = transaction.Transaction()
    >>> tx2.commit()
    >>> tx.commit()
    tx commit

Transactions should be closed in the right order (subtransactions first):

    >>> tx = transaction.Transaction()
    tx begin
    >>> tx2 = transaction.Transaction()
    >>> tx.commit()
    ... # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    TransactionError: Transaction on stack is not the transaction being closed.
    >>> tx2.commit()
    >>> tx.commit()
    tx commit


The transactional decorator can be used to mark functions as transactional:

    >>> @transaction.transactional
    ... def a():
    ...     print 'do something'
    >>> a()
    tx begin
    do something
    tx commit

If an exception is raised from within the decorated function a rollback is
performed:

    >>> @transaction.transactional
    ... def a():
    ...     raise IndexError, 'bla'
    >>> a()
    ... # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    IndexError: bla
    >>> transaction.Transaction._stack
    []

All transactions are marked for rollback once an exception is raised:

    >>> tx = transaction.Transaction()
    tx begin
    >>> a()
    ... # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    IndexError: bla
    >>> tx._need_rollback
    True
    >>> tx.commit()
    tx rollback