File: test_4400_tpc.py

package info (click to toggle)
python-oracledb 1.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,224 kB
  • sloc: python: 17,637; sql: 1,819; makefile: 41
file content (120 lines) | stat: -rw-r--r-- 5,200 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
113
114
115
116
117
118
119
120
#------------------------------------------------------------------------------
# Copyright (c) 2021, 2022, Oracle and/or its affiliates.
#
# This software is dual-licensed to you under the Universal Permissive License
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
# either license.
#
# If you elect to accept the software under the Apache License, Version 2.0,
# the following applies:
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------

"""
4400 - Module for testing TPC (two-phase commit) transactions.
"""

import unittest

import oracledb
import test_env

@unittest.skipIf(test_env.get_is_thin(),
                 "thin mode doesn't support two-phase commit yet")
class TestCase(test_env.BaseTestCase):

    def test_4400_tpc_with_rolback(self):
        "4400 - test begin, prepare, roll back global transaction"
        self.cursor.execute("truncate table TestTempTable")
        xid = self.connection.xid(3900, "txn3900", "branchId")
        self.connection.tpc_begin(xid)
        self.assertEqual(self.connection.tpc_prepare(), False)
        self.connection.tpc_begin(xid)
        self.cursor.execute("""
                insert into TestTempTable (IntCol, StringCol1)
                values (1, 'tesName')""")
        self.assertEqual(self.connection.tpc_prepare(), True)
        self.connection.tpc_rollback()
        self.cursor.execute("select count(*) from TestTempTable")
        count, = self.cursor.fetchone()
        self.assertEqual(count, 0)

    def test_4401_tpc_with_commit(self):
        "4401 - test begin, prepare, commit global transaction"
        self.cursor.execute("truncate table TestTempTable")
        xid = self.connection.xid(3901, "txn3901", "branchId")
        self.connection.tpc_begin(xid)
        self.cursor.execute("""
                insert into TestTempTable (IntCol, StringCol1)
                values (1, 'tesName')""")
        self.assertEqual(self.connection.tpc_prepare(), True)
        self.connection.tpc_commit()
        self.cursor.execute("select IntCol, StringCol1 from TestTempTable")
        self.assertEqual(self.cursor.fetchall(), [(1, 'tesName')])

    def test_4402_tpc_multiple_transactions(self):
        "4402 - test multiple global transactions on the same connection"
        self.cursor.execute("truncate table TestTempTable")
        xid1 = self.connection.xid(3902, "txn3902", "branch1")
        xid2 = self.connection.xid(3902, "txn3902", "branch2")
        self.connection.tpc_begin(xid1)
        self.cursor.execute("""
                insert into TestTempTable (IntCol, StringCol1)
                values (1, 'tesName')""")
        self.connection.tpc_end()
        self.connection.tpc_begin(xid2)
        self.cursor.execute("""
                insert into TestTempTable (IntCol, StringCol1)
                values (2, 'tesName')""")
        self.connection.tpc_end()
        needs_commit1 = self.connection.tpc_prepare(xid1)
        needs_commit2 = self.connection.tpc_prepare(xid2)
        if needs_commit1:
            self.connection.tpc_commit(xid1)
        if needs_commit2:
            self.connection.tpc_commit(xid2)
        self.cursor.execute("""
                select IntCol, StringCol1
                from TestTempTable
                order by IntCol""")
        expected_rows = [(1, 'tesName'), (2, 'tesName')]
        self.assertEqual(self.cursor.fetchall(), expected_rows)

    def test_4403_rollback_with_xid(self):
        "4403 - test rollback with parameter xid"
        self.cursor.execute("truncate table TestTempTable")
        xid1 = self.connection.xid(3901, "txn3901", "branch1")
        xid2 = self.connection.xid(3902, "txn3902", "branch2")
        for count, xid in enumerate([xid1, xid2]):
            self.connection.tpc_begin(xid)
            self.cursor.execute("""
                    insert into TestTempTable (IntCol, StringCol1)
                    values (:id, 'tesName')""", id=count)
            self.connection.tpc_end()
        self.connection.tpc_rollback(xid1)

        self.assertRaisesRegex(oracledb.DatabaseError, "^ORA-24756",
                               self.connection.tpc_prepare, xid1)
        needs_commit = self.connection.tpc_prepare(xid2)
        if needs_commit:
            self.connection.tpc_commit(xid2)
        self.cursor.execute("""
                select IntCol, StringCol1
                from TestTempTable
                order by IntCol""")
        self.assertEqual(self.cursor.fetchall(), [(1, 'tesName')])

if __name__ == "__main__":
    test_env.run_test_cases()