|
1 | 1 | #------------------------------------------------------------------------------ |
2 | | -# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | +# Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | # |
4 | 4 | # Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved. |
5 | 5 | # |
@@ -373,5 +373,97 @@ def test_1125_transaction_begin(self): |
373 | 373 | count, = cursor.fetchone() |
374 | 374 | self.assertEqual(count, 0) |
375 | 375 |
|
| 376 | + def test_1126_multiple_transactions(self): |
| 377 | + "1126 - test multiple transactions on the same connection" |
| 378 | + connection = test_env.get_connection() |
| 379 | + with connection.cursor() as cursor: |
| 380 | + cursor.execute("truncate table TestTempTable") |
| 381 | + |
| 382 | + id_ = random.randint(0, 2 ** 128) |
| 383 | + xid = (0x1234, "%032x" % id_, "%032x" % 9) |
| 384 | + connection.begin(*xid) |
| 385 | + with connection.cursor() as cursor: |
| 386 | + cursor.execute(""" |
| 387 | + insert into TestTempTable (IntCol, StringCol) |
| 388 | + values (1, 'tesName')""") |
| 389 | + self.assertEqual(connection.prepare(), True) |
| 390 | + connection.commit() |
| 391 | + |
| 392 | + for begin_trans in (True, False): |
| 393 | + val = 3 |
| 394 | + if begin_trans: |
| 395 | + connection.begin() |
| 396 | + val = 2 |
| 397 | + with connection.cursor() as cursor: |
| 398 | + cursor.execute(""" |
| 399 | + insert into TestTempTable (IntCol, StringCol) |
| 400 | + values (:int_val, 'tesName')""", int_val=val) |
| 401 | + connection.commit() |
| 402 | + |
| 403 | + expected_rows = [(1, "tesName"), (2, "tesName"), (3, "tesName")] |
| 404 | + with connection.cursor() as cursor: |
| 405 | + cursor.execute("select IntCol, StringCol from TestTempTable") |
| 406 | + self.assertEqual(cursor.fetchall(), expected_rows) |
| 407 | + |
| 408 | + def test_1127_multiple_global_transactions(self): |
| 409 | + "1127 - test multiple global transactions on the same connection" |
| 410 | + connection = test_env.get_connection() |
| 411 | + with connection.cursor() as cursor: |
| 412 | + cursor.execute("truncate table TestTempTable") |
| 413 | + |
| 414 | + id_ = random.randint(0, 2 ** 128) |
| 415 | + xid = (0x1234, "%032x" % id_, "%032x" % 9) |
| 416 | + connection.begin(*xid) |
| 417 | + with connection.cursor() as cursor: |
| 418 | + cursor.execute(""" |
| 419 | + insert into TestTempTable (IntCol, StringCol) |
| 420 | + values (1, 'tesName')""") |
| 421 | + self.assertEqual(connection.prepare(), True) |
| 422 | + connection.commit() |
| 423 | + |
| 424 | + for begin_trans in (True, False): |
| 425 | + val = 3 |
| 426 | + if begin_trans: |
| 427 | + connection.begin() |
| 428 | + val = 2 |
| 429 | + with connection.cursor() as cursor: |
| 430 | + cursor.execute(""" |
| 431 | + insert into TestTempTable (IntCol, StringCol) |
| 432 | + values (:int_val, 'tesName')""", int_val=val) |
| 433 | + connection.commit() |
| 434 | + |
| 435 | + id2_ = random.randint(0, 2 ** 128) |
| 436 | + xid2 = (0x1234, "%032x" % id2_, "%032x" % 9) |
| 437 | + connection.begin(*xid2) |
| 438 | + with connection.cursor() as cursor: |
| 439 | + cursor.execute(""" |
| 440 | + insert into TestTempTable (IntCol, StringCol) |
| 441 | + values (4, 'tesName')""") |
| 442 | + self.assertEqual(connection.prepare(), True) |
| 443 | + connection.commit() |
| 444 | + |
| 445 | + expected_rows = [(1, "tesName"), (2, "tesName"), (3, "tesName"), |
| 446 | + (4, "tesName")] |
| 447 | + |
| 448 | + with connection.cursor() as cursor: |
| 449 | + cursor.execute("select IntCol, StringCol from TestTempTable") |
| 450 | + self.assertEqual(cursor.fetchall(), expected_rows) |
| 451 | + |
| 452 | + def test_1128_exception_creating_global_txn_after_local_txn(self): |
| 453 | + "1128 - test creating global txn after a local txn" |
| 454 | + connection = test_env.get_connection() |
| 455 | + with connection.cursor() as cursor: |
| 456 | + cursor.execute("truncate table TestTempTable") |
| 457 | + |
| 458 | + val = 2 |
| 459 | + with connection.cursor() as cursor: |
| 460 | + cursor.execute(""" |
| 461 | + insert into TestTempTable (IntCol, StringCol) |
| 462 | + values (:int_val, 'tesName')""", int_val=val) |
| 463 | + |
| 464 | + id_ = random.randint(0, 2 ** 128) |
| 465 | + xid = (0x1234, "%032x" % id_, "%032x" % 9) |
| 466 | + self.assertRaises(oracledb.DatabaseError, connection.begin, *xid) |
| 467 | + |
376 | 468 | if __name__ == "__main__": |
377 | 469 | test_env.run_test_cases() |
0 commit comments