|
1 | 1 | #------------------------------------------------------------------------------ |
2 | | -# Copyright (c) 2021, 2022, Oracle and/or its affiliates. |
| 2 | +# Copyright (c) 2021, 2023, Oracle and/or its affiliates. |
3 | 3 | # |
4 | 4 | # This software is dual-licensed to you under the Universal Permissive License |
5 | 5 | # (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
@@ -116,5 +116,109 @@ def test_4403_rollback_with_xid(self): |
116 | 116 | order by IntCol""") |
117 | 117 | self.assertEqual(self.cursor.fetchall(), [(1, 'tesName')]) |
118 | 118 |
|
| 119 | + def test_4404_tpc_begin_resume(self): |
| 120 | + "4404 - test resuming a transaction" |
| 121 | + self.cursor.execute("truncate table TestTempTable") |
| 122 | + xid1 = self.connection.xid(3939, "txn3939", "branch39") |
| 123 | + xid2 = self.connection.xid(3940, "txn3940", "branch40") |
| 124 | + values = [ |
| 125 | + [xid1, (1, "User Info")], |
| 126 | + [xid2, (2, "Other User Info")] |
| 127 | + ] |
| 128 | + for xid, data in values: |
| 129 | + self.connection.tpc_begin(xid) |
| 130 | + self.cursor.execute(""" |
| 131 | + insert into TestTempTable (IntCol, StringCol1) |
| 132 | + values (:1, :2)""", data) |
| 133 | + self.connection.tpc_end() |
| 134 | + for xid, _ in values: |
| 135 | + self.assertRaisesRegex(oracledb.DatabaseError, "^ORA-24757:", |
| 136 | + self.connection.tpc_begin, xid) |
| 137 | + for xid, data in values: |
| 138 | + self.connection.tpc_begin(xid, oracledb.TPC_BEGIN_RESUME) |
| 139 | + self.cursor.execute( |
| 140 | + "select IntCol, StringCol1 from TestTempTable") |
| 141 | + res, = self.cursor.fetchall() |
| 142 | + self.assertEqual(res, data) |
| 143 | + self.connection.tpc_rollback(xid) |
| 144 | + |
| 145 | + def test_4405_tpc_begin_promote(self): |
| 146 | + "4405 - test promoting a local transaction to a tpc transaction" |
| 147 | + self.cursor.execute("truncate table TestTempTable") |
| 148 | + xid = self.connection.xid(3941, "txn3941", "branch41") |
| 149 | + values = (1, "String 1") |
| 150 | + self.cursor.execute( |
| 151 | + """insert into TestTempTable (IntCol, StringCol1) |
| 152 | + values (:1, :2)""", values) |
| 153 | + self.assertRaisesRegex(oracledb.DatabaseError, "^ORA-24776", |
| 154 | + self.connection.tpc_begin, xid) |
| 155 | + self.connection.tpc_begin(xid, oracledb.TPC_BEGIN_PROMOTE) |
| 156 | + self.cursor.execute("select IntCol, StringCol1 from TestTempTable") |
| 157 | + res, = self.cursor.fetchall() |
| 158 | + self.assertEqual(res, values) |
| 159 | + self.connection.tpc_rollback(xid) |
| 160 | + |
| 161 | + def test_4406_tpc_end_with_xid(self): |
| 162 | + "4406 - test ending a transaction with parameter xid" |
| 163 | + self.cursor.execute("truncate table TestTempTable") |
| 164 | + xid1 = self.connection.xid(4406, "txn4406a", "branch3") |
| 165 | + xid2 = self.connection.xid(4406, "txn4406b", "branch4") |
| 166 | + self.connection.tpc_begin(xid1) |
| 167 | + self.cursor.execute(""" |
| 168 | + insert into TestTempTable (IntCol, StringCol1) |
| 169 | + values (1, 'test4406a')""") |
| 170 | + self.connection.tpc_begin(xid2) |
| 171 | + self.assertRaisesRegex(oracledb.DatabaseError, "^ORA-24758:", |
| 172 | + self.connection.tpc_end, xid1) |
| 173 | + self.cursor.execute(""" |
| 174 | + insert into TestTempTable (IntCol, StringCol1) |
| 175 | + values (2, 'test4406b')""") |
| 176 | + self.connection.tpc_end(xid2) |
| 177 | + self.assertRaisesRegex(oracledb.DatabaseError, "^ORA-25352:", |
| 178 | + self.connection.tpc_end, xid1) |
| 179 | + self.connection.tpc_rollback(xid1) |
| 180 | + self.connection.tpc_rollback(xid2) |
| 181 | + |
| 182 | + def test_4407_tpc_recover(self): |
| 183 | + "4407 - test tpc_recover()" |
| 184 | + self.cursor.execute("truncate table TestTempTable") |
| 185 | + n_xids = 10 |
| 186 | + for i in range(n_xids): |
| 187 | + xid = self.connection.xid(4407 + i, f"txn4407{i}", f"branch{i}") |
| 188 | + self.connection.tpc_begin(xid) |
| 189 | + self.cursor.execute(""" |
| 190 | + insert into TestTempTable (IntCol, StringCol1) |
| 191 | + values (:1, 'test4407')""", [i + 1]) |
| 192 | + self.connection.tpc_prepare(xid) |
| 193 | + recovers = self.connection.tpc_recover() |
| 194 | + self.assertEqual(len(recovers), n_xids) |
| 195 | + |
| 196 | + self.cursor.execute("select * from DBA_PENDING_TRANSACTIONS") |
| 197 | + self.assertEqual(self.cursor.fetchall(), recovers) |
| 198 | + |
| 199 | + for format_id, txn, branch in recovers: |
| 200 | + if format_id % 2 == 0: |
| 201 | + xid = self.connection.xid(format_id, txn, branch) |
| 202 | + self.connection.tpc_commit(xid) |
| 203 | + recovers = self.connection.tpc_recover() |
| 204 | + self.assertEqual(len(recovers), n_xids // 2) |
| 205 | + |
| 206 | + for format_id, txn, branch in recovers: |
| 207 | + xid = self.connection.xid(format_id, txn, branch) |
| 208 | + self.connection.tpc_rollback(xid) |
| 209 | + recovers = self.connection.tpc_recover() |
| 210 | + self.assertEqual(len(recovers), 0) |
| 211 | + |
| 212 | + def test_4408_tpc_recover_read_only(self): |
| 213 | + "4408 - test tpc_recover() with read-only transaction" |
| 214 | + self.cursor.execute("truncate table TestTempTable") |
| 215 | + for i in range(4): |
| 216 | + xid = self.connection.xid(4408 + i, f"txn4408{i}", f"branch{i}") |
| 217 | + self.connection.tpc_begin(xid) |
| 218 | + self.cursor.execute("select * from TestTempTable") |
| 219 | + self.connection.tpc_prepare(xid) |
| 220 | + recovers = self.connection.tpc_recover() |
| 221 | + self.assertEqual(len(recovers), 0) |
| 222 | + |
119 | 223 | if __name__ == "__main__": |
120 | 224 | test_env.run_test_cases() |
0 commit comments