File: transactions.rst

package info (click to toggle)
mysql-connector-python 9.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 28,308 kB
  • sloc: python: 86,729; sql: 47,030; ansic: 3,494; cpp: 860; sh: 394; makefile: 208; javascript: 2
file content (112 lines) | stat: -rw-r--r-- 2,836 bytes parent folder | download
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
Transactions
============

Savepoints
----------

The ``SAVEPOINT`` statement sets a named transaction allowing parts of a transaction to be rolled back before ``COMMIT``.

Get the collection object
^^^^^^^^^^^^^^^^^^^^^^^^^

Assuming the existence of ``test_schema.test_collection`` collection.

.. code-block:: python

   [{
       "_id": 1,
       "name": "Fred",
       "age": 21
   }]


Get the collection object.

.. code-block:: python

   session = mysqlx.get_session("root:@localhost:33060")
   schema = session.get_schema("test_schema")
   collection = schema.get_collection("test_collection")

Set and rollback to a named transaction savepoint
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A savepoint name can be provided to create a transaction savepoint, which can later be used to rollback.

A :class:`mysqlx.OperationalError` will be raised if the savepoint name is an invalid string or if a nonexistent savepoint is being used in :func:`mysqlx.Session.rollback_to`.

.. code-block:: python

   # Start transaction
   session.start_transaction()

   collection.add({"name": "Wilma", "age": 33}).execute()
   assert(2 == collection.count())

   # Create a savepoint
   session.set_savepoint("sp")

   collection.add({"name": "Barney", "age": 42}).execute()
   assert(3 == collection.count())

   # Rollback to a savepoint
   session.rollback_to("sp")

   assert(2 == collection.count())

   # Commit all operations
   session.commit()

Set and rollback to an unnamed transaction savepoint
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If a savepoint name is not provided, :func:`mysqlx.Session.release_savepoint` will return a generated savepoint name.

.. code-block:: python

   # Start transaction
   session.start_transaction()

   collection.add({"name": "Wilma", "age": 33}).execute()
   assert(2 == collection.count())

   # Create a savepoint
   savepoint = session.set_savepoint()

   collection.add({"name": "Barney", "age": 42}).execute()
   assert(3 == collection.count())

   # Rollback to a savepoint
   session.rollback_to(savepoint)

   assert(2 == collection.count())

   # Commit all operations
   session.commit()

Releasing a transaction savepoint
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A :class:`mysqlx.OperationalError` will be raised if a nonexistent savepoint is being used in :func:`mysqlx.Session.release_savepoint`.

.. code-block:: python

   # Start transaction
   session.start_transaction()

   collection.add({"name": "Wilma", "age": 33}).execute()
   assert(2 == collection.count())

   # Create a savepoint
   session.set_savepoint("sp")

   collection.add({"name": "Barney", "age": 42}).execute()
   assert(3 == collection.count())

   # Release a savepoint
   session.release_savepoint("sp")

   assert(3 == collection.count())

   # Commit all operations
   session.commit()