|
77 | 77 | import static com.facebook.presto.testing.TestingSession.TESTING_CATALOG; |
78 | 78 | import static com.facebook.presto.testing.assertions.Assert.assertEquals; |
79 | 79 | import static com.facebook.presto.tests.QueryAssertions.assertContains; |
80 | | -import static com.facebook.presto.transaction.TransactionBuilder.transaction; |
81 | 80 | import static com.google.common.collect.ImmutableList.toImmutableList; |
82 | 81 | import static com.google.common.collect.Iterables.getOnlyElement; |
83 | 82 | import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly; |
@@ -250,73 +249,71 @@ public void testCreateTable() |
250 | 249 | } |
251 | 250 |
|
252 | 251 | @Test |
253 | | - public void testClearTransactionId() |
| 252 | + public void testNonAutoCommitTransactionWithRollback() |
254 | 253 | { |
255 | | - assertUpdate("create table test_clear_transaction_id_table(a int, b varchar)"); |
256 | | - assertUpdate("insert into test_clear_transaction_id_table values(1, '1001'), (2, '1002')", 2); |
257 | | - Session session = getQueryRunner().getDefaultSession(); |
258 | | - String defaultCatalog = session.getCatalog().get(); |
259 | | - transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()) |
260 | | - .execute(Session.builder(session) |
261 | | - .setIdentity(new Identity("admin", |
262 | | - Optional.empty(), |
263 | | - ImmutableMap.of(defaultCatalog, new SelectedRole(ROLE, Optional.of("admin"))), |
264 | | - ImmutableMap.of(), |
265 | | - ImmutableMap.of(), |
266 | | - Optional.empty(), |
267 | | - Optional.empty())) |
268 | | - .build(), |
269 | | - txnSession -> { |
270 | | - MaterializedResult result = computeActual(txnSession, "select * from test_clear_transaction_id_table"); |
271 | | - assertEquals(result.getRowCount(), 2); |
272 | | - assertFalse(result.isClearTransactionId()); |
273 | | - |
274 | | - result = computeActual(txnSession, "insert into test_clear_transaction_id_table values(1, '1001'), (2, '1002')"); |
275 | | - assertEquals(result.getOnlyValue(), 2L); |
276 | | - assertFalse(result.isClearTransactionId()); |
277 | | - |
278 | | - // `Rollback` executes successfully, and the client gets a flag `clearTransactionId = true` |
279 | | - result = computeActual(txnSession, "rollback"); |
280 | | - assertTrue(result.isClearTransactionId()); |
281 | | - }); |
282 | | - |
283 | | - assertQuery("select * from test_clear_transaction_id_table", "values(1, '1001'), (2, '1002')"); |
284 | | - assertUpdate("drop table if exists test_clear_transaction_id_table"); |
| 254 | + assertUpdate("create table multi_statements_transaction_rollback(a int, b varchar)"); |
| 255 | + assertUpdate("insert into multi_statements_transaction_rollback values(1, '1001'), (2, '1002')", 2); |
| 256 | + |
| 257 | + Session session = assertStartTransaction(getSession(), "start transaction"); |
| 258 | + |
| 259 | + assertQuery(session, "select * from multi_statements_transaction_rollback", "values(1, '1001'), (2, '1002')"); |
| 260 | + assertUpdate(session, "insert into multi_statements_transaction_rollback values(3, '1003'), (4, '1004')", 2); |
| 261 | + |
| 262 | + // `Rollback` executes successfully, and the client gets a flag `clearTransactionId = true` |
| 263 | + session = assertEndTransaction(session, "rollback"); |
| 264 | + |
| 265 | + assertQuery(session, "select * from multi_statements_transaction_rollback", "values(1, '1001'), (2, '1002')"); |
| 266 | + assertUpdate(session, "drop table if exists multi_statements_transaction_rollback"); |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + public void testNonAutoCommitTransactionWithCommit() |
| 271 | + { |
| 272 | + assertUpdate("create table multi_statements_transaction_commit(a int, b varchar)"); |
| 273 | + assertUpdate("insert into multi_statements_transaction_commit values(1, '1001'), (2, '1002')", 2); |
| 274 | + Session session = assertStartTransaction(getSession(), "start transaction"); |
| 275 | + |
| 276 | + assertQuery(session, "select * from multi_statements_transaction_commit", "values(1, '1001'), (2, '1002')"); |
| 277 | + assertUpdate(session, "insert into multi_statements_transaction_commit values(3, '1003'), (4, '1004')", 2); |
| 278 | + |
| 279 | + // `Commit` executes successfully, and the client gets a flag `clearTransactionId = true` |
| 280 | + session = assertEndTransaction(session, "commit"); |
| 281 | + |
| 282 | + assertQuery(session, "select * from multi_statements_transaction_commit", |
| 283 | + "values(1, '1001'), (2, '1002'), (3, '1003'), (4, '1004')"); |
| 284 | + assertUpdate(session, "drop table if exists multi_statements_transaction_commit"); |
285 | 285 | } |
286 | 286 |
|
287 | 287 | @Test |
288 | 288 | public void testNonAutoCommitTransactionWithFailAndRollback() |
289 | 289 | { |
290 | 290 | assertUpdate("create table test_non_autocommit_table(a int, b varchar)"); |
291 | | - Session session = getQueryRunner().getDefaultSession(); |
292 | | - String defaultCatalog = session.getCatalog().get(); |
293 | | - transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()) |
294 | | - .execute(Session.builder(session) |
295 | | - .setIdentity(new Identity("admin", |
296 | | - Optional.empty(), |
297 | | - ImmutableMap.of(defaultCatalog, new SelectedRole(ROLE, Optional.of("admin"))), |
298 | | - ImmutableMap.of(), |
299 | | - ImmutableMap.of(), |
300 | | - Optional.empty(), |
301 | | - Optional.empty())) |
302 | | - .build(), |
303 | | - txnSession -> { |
304 | | - // simulate failure of SQL statement execution |
305 | | - assertQueryFails(txnSession, "SELECT fail('forced failure')", "forced failure"); |
306 | | - |
307 | | - // cannot execute any SQLs except `rollback` in current session |
308 | | - assertQueryFails(txnSession, "select count(*) from test_non_autocommit_table", "Current transaction is aborted, commands ignored until end of transaction block"); |
309 | | - assertQueryFails(txnSession, "show tables", "Current transaction is aborted, commands ignored until end of transaction block"); |
310 | | - assertQueryFails(txnSession, "insert into test_non_autocommit_table values(1, '1001')", "Current transaction is aborted, commands ignored until end of transaction block"); |
311 | | - assertQueryFails(txnSession, "create table test_table(a int, b varchar)", "Current transaction is aborted, commands ignored until end of transaction block"); |
312 | | - |
313 | | - // execute `rollback` successfully |
314 | | - MaterializedResult result = computeActual(txnSession, "rollback"); |
315 | | - assertTrue(result.isClearTransactionId()); |
316 | | - }); |
317 | | - |
318 | | - assertQuery("select count(*) from test_non_autocommit_table", "values(0)"); |
319 | | - assertUpdate("drop table if exists test_non_autocommit_table"); |
| 291 | + Session session = Session.builder(getSession()) |
| 292 | + .setIdentity(new Identity("admin", |
| 293 | + Optional.empty(), |
| 294 | + ImmutableMap.of(getSession().getCatalog().get(), new SelectedRole(ROLE, Optional.of("admin"))), |
| 295 | + ImmutableMap.of(), |
| 296 | + ImmutableMap.of(), |
| 297 | + Optional.empty(), |
| 298 | + Optional.empty())) |
| 299 | + .build(); |
| 300 | + |
| 301 | + session = assertStartTransaction(session, "start transaction"); |
| 302 | + |
| 303 | + // simulate failure of SQL statement execution |
| 304 | + assertQueryFails(session, "SELECT fail('forced failure')", "forced failure"); |
| 305 | + |
| 306 | + // cannot execute any SQLs except `rollback` in current session |
| 307 | + assertQueryFails(session, "select count(*) from test_non_autocommit_table", "Current transaction is aborted, commands ignored until end of transaction block"); |
| 308 | + assertQueryFails(session, "show tables", "Current transaction is aborted, commands ignored until end of transaction block"); |
| 309 | + assertQueryFails(session, "insert into test_non_autocommit_table values(1, '1001')", "Current transaction is aborted, commands ignored until end of transaction block"); |
| 310 | + assertQueryFails(session, "create table test_table(a int, b varchar)", "Current transaction is aborted, commands ignored until end of transaction block"); |
| 311 | + |
| 312 | + // execute `rollback` successfully |
| 313 | + session = assertEndTransaction(session, "rollback"); |
| 314 | + |
| 315 | + assertQuery(session, "select count(*) from test_non_autocommit_table", "values(0)"); |
| 316 | + assertUpdate(session, "drop table if exists test_non_autocommit_table"); |
320 | 317 | } |
321 | 318 |
|
322 | 319 | @Test |
|
0 commit comments