|
9 | 9 | import java.sql.SQLException; |
10 | 10 | import java.sql.Statement; |
11 | 11 |
|
| 12 | +import static org.testng.Assert.*; |
| 13 | + |
12 | 14 | public class TestTransaction { |
13 | 15 |
|
14 | 16 | @BeforeTest |
15 | 17 | public void setUp() |
16 | 18 | throws SQLException { |
17 | | - // create table |
18 | | - Connection c = Utils.createConnection(); |
19 | | - c.createStatement().execute("drop database if exists test_txn"); |
20 | | - c.createStatement().execute("create database test_txn"); |
21 | | - c.createStatement().execute("create table test_txn.table1(i int)"); |
| 19 | + // create db |
| 20 | + try (Connection c = Utils.createConnection()) { |
| 21 | + c.createStatement().execute("create or replace database test_txn"); |
| 22 | + } |
22 | 23 | } |
23 | 24 |
|
24 | | - @Test |
25 | | - public void testRollback() |
26 | | - throws SQLException { |
27 | | - Connection c = Utils.createConnection(); |
28 | | - c.createStatement().execute("delete from test_txn.table1"); |
29 | | - try (Statement statement = c.createStatement()) { |
30 | | - statement.execute("begin"); |
31 | | - ResultSet r = statement.getResultSet(); |
32 | | - } |
| 25 | + @Test(groups = {"IT"}) |
| 26 | + public void testCommit() throws SQLException { |
| 27 | + try ( Connection c1 = Utils.createConnection(); |
| 28 | + Connection c2 = Utils.createConnection(); |
| 29 | + Connection c3 = Utils.createConnection() |
| 30 | + ) { |
33 | 31 |
|
34 | | - try (Statement statement = c.createStatement()) { |
35 | | - statement.execute("select 11"); |
36 | | - // txn_state = Auto_Commit, not correct, should be Active |
37 | | - ResultSet r = statement.getResultSet(); |
38 | | - while (r.next()) { |
39 | | - } |
40 | | - } |
| 32 | + c1.createStatement().execute("create or replace table test_txn.table1(i int)"); |
41 | 33 |
|
42 | | - try (Statement statement = c.createStatement()) { |
43 | | - statement.execute("insert into test_txn.table1 values(3)"); |
44 | | - ResultSet r = statement.getResultSet(); |
45 | | - } |
46 | | - try (Statement statement = c.createStatement()) { |
47 | | - statement.execute("rollback"); |
48 | | - ResultSet r = statement.getResultSet(); |
49 | | - } |
| 34 | + try (Statement statement = c1.createStatement()) { |
| 35 | + statement.execute("begin"); |
| 36 | + statement.execute("insert into test_txn.table1 values(4)"); |
| 37 | + statement.execute("select * from test_txn.table1"); |
| 38 | + ResultSet rs = statement.getResultSet(); |
| 39 | + assertTrue(rs.next()); |
| 40 | + Assert.assertEquals(4, rs.getInt(1)); |
| 41 | + assertFalse(rs.next()); |
50 | 42 |
|
51 | | - try (Statement statement = c.createStatement()) { |
52 | | - statement.execute("select * from test_txn.table1"); |
53 | | - ResultSet rs = statement.getResultSet(); |
54 | | - while (rs.next()) { |
55 | | - Assert.assertEquals(0, rs.getInt(1)); |
56 | 43 | } |
57 | | - } |
58 | | - } |
59 | 44 |
|
60 | | - @Test |
61 | | - public void testCommit() throws SQLException { |
62 | | - Connection c1 = Utils.createConnection(); |
63 | | - Connection c2 = Utils.createConnection(); |
64 | | - c1.createStatement().execute("delete from test_txn.table1"); |
65 | | - try (Statement statement = c1.createStatement()) { |
66 | | - statement.execute("create or replace table test_txn.table1(i int)"); |
67 | | - statement.execute("begin"); |
68 | | - statement.execute("insert into test_txn.table1 values(4)"); |
69 | | - statement.execute("select * from test_txn.table1"); |
70 | | - ResultSet rs = statement.getResultSet(); |
71 | | - while (rs.next()) { |
| 45 | + try (Statement statement = c2.createStatement()) { |
| 46 | + statement.execute("begin"); |
| 47 | + statement.execute("insert into test_txn.table1 values(5)"); |
| 48 | + statement.execute("select * from test_txn.table1"); |
| 49 | + ResultSet rs = statement.getResultSet(); |
| 50 | + assertTrue(rs.next()); |
| 51 | + Assert.assertEquals(5, rs.getInt(1)); |
| 52 | + assertFalse(rs.next()); |
| 53 | + } |
| 54 | + c1.commit(); |
| 55 | + |
| 56 | + try (Statement statement = c3.createStatement()) { |
| 57 | + statement.execute("select * from test_txn.table1"); |
| 58 | + ResultSet rs = statement.getResultSet(); |
| 59 | + assertTrue(rs.next()); |
72 | 60 | Assert.assertEquals(4, rs.getInt(1)); |
| 61 | + assertFalse(rs.next()); |
73 | 62 | } |
74 | | - } |
| 63 | + c2.commit(); |
75 | 64 |
|
76 | | - try (Statement statement = c2.createStatement()) { |
77 | | - statement.execute("begin"); |
78 | | - statement.execute("insert into test_txn.table1 values(5)"); |
79 | | - statement.execute("select * from test_txn.table1"); |
80 | | - ResultSet rs = statement.getResultSet(); |
81 | | - while (rs.next()) { |
| 65 | + try (Statement statement = c3.createStatement()) { |
| 66 | + statement.execute("select * from test_txn.table1 order by i"); |
| 67 | + ResultSet rs = statement.getResultSet(); |
| 68 | + assertTrue(rs.next()); |
| 69 | + Assert.assertEquals(4, rs.getInt(1)); |
| 70 | + assertTrue(rs.next()); |
82 | 71 | Assert.assertEquals(5, rs.getInt(1)); |
| 72 | + assertFalse(rs.next()); |
83 | 73 | } |
84 | 74 | } |
85 | | - c1.commit(); |
86 | | - Connection c3 = Utils.createConnection(); |
87 | | - try (Statement statement = c3.createStatement()) { |
88 | | - statement.execute("select * from test_txn.table1"); |
89 | | - ResultSet rs = statement.getResultSet(); |
90 | | - while (rs.next()) { |
91 | | - Assert.assertEquals(4, rs.getInt(1)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test(groups = {"IT"}) |
| 78 | + public void testRollback() |
| 79 | + throws SQLException { |
| 80 | + try (Connection c = Utils.createConnection()) { |
| 81 | + try (Statement statement = c.createStatement()) { |
| 82 | + c.createStatement().execute("create or replace table test_txn.table2(i int)"); |
| 83 | + statement.execute("begin"); |
| 84 | + statement.execute("select 11"); |
| 85 | + statement.execute("insert into test_txn.table2 values(3)"); |
| 86 | + statement.execute("rollback"); |
| 87 | + statement.execute("select * from test_txn.table2"); |
| 88 | + ResultSet rs = statement.getResultSet(); |
| 89 | + assertFalse(rs.next()); |
92 | 90 | } |
93 | 91 | } |
94 | 92 | } |
95 | 93 |
|
| 94 | + @Test(groups = {"IT"}) |
| 95 | + public void testConflict() throws SQLException { |
| 96 | + try (Connection c1 = Utils.createConnection(); Connection c2 = Utils.createConnection()) { |
| 97 | + Statement statement1 = c1.createStatement(); |
| 98 | + Statement statement2 = c2.createStatement(); |
| 99 | + |
| 100 | + statement1.execute("create or replace table test_txn.table3(i int, j int)"); |
| 101 | + statement1.execute("insert into test_txn.table3 values (1, 11)"); |
| 102 | + |
| 103 | + statement1.execute("begin"); |
| 104 | + statement1.execute("UPDATE test_txn.table3 set j = 111 where i=1"); |
| 105 | + |
| 106 | + statement2.execute("begin"); |
| 107 | + statement2.execute("UPDATE test_txn.table3 set j = 222 where i=1"); |
| 108 | + c2.commit(); |
| 109 | + |
| 110 | + java.sql.SQLException exception = Assert.expectThrows( |
| 111 | + java.sql.SQLException.class, |
| 112 | + () -> statement1.execute("commit") |
| 113 | + ); |
| 114 | + // e.g. Unresolvable conflict detected for table 2249 |
| 115 | + Assert.assertTrue(exception.getMessage().toLowerCase().contains("conflict")); |
| 116 | + |
| 117 | + |
| 118 | + statement2.execute("select j from test_txn.table3 where i = 1"); |
| 119 | + ResultSet rs = statement2.getResultSet(); |
| 120 | + assertTrue(rs.next()); |
| 121 | + Assert.assertEquals(rs.getInt(1), 222); |
| 122 | + assertFalse(rs.next()); |
| 123 | + } |
| 124 | + |
| 125 | + } |
96 | 126 | } |
0 commit comments