@Test
  public void testConcurrentConflictRetry() throws Exception {
    dropTable(new String[]{"target"});

    driver2 = Mockito.spy(driver2);
    driver2.getConf().setBoolVar(HiveConf.ConfVars.TXN_WRITE_X_LOCK, true);
    driver.run("create table target(i int) stored as orc tblproperties ('transactional'='true')");
    driver.run("insert into target values (1),(1)");

    DbTxnManager txnMgr2 = (DbTxnManager) TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf);
    swapTxnManager(txnMgr2);

    // This partial mock allows us to execute a transaction that conflicts with the driver2 query in a controlled
    // manner.
    AtomicInteger lockAndRespondCount = new AtomicInteger();
    Mockito.doAnswer((invocation) -> {
      lockAndRespondCount.getAndIncrement();
      // we want to make sure this transaction gets conflicted at least twice, to exercise the conflict retry loop
      if (lockAndRespondCount.get() <= 2) {
        swapTxnManager(txnMgr);
        try {
          // this should call a conflict with the current query being ran by driver2
          driver.run("update target set i = 1 where i = 1");
        } catch (Exception e) {
          // do nothing
        }
        swapTxnManager(txnMgr2);
      }
      invocation.callRealMethod();
      return null;
    }).when(driver2).lockAndRespond();

    driver2.run("update target set i = 1 where i = 1");

    // we expected lockAndRespond to be called 3 times.
    // 1 time after compilation, 2 more times due to the 2 conflicts
    Assert.assertEquals(3, lockAndRespondCount.get());
    swapTxnManager(txnMgr);
    // we expect two rows
    driver.run("select * from target");
    List<String> res = new ArrayList<>();
    driver.getFetchTask().fetch(res);
    Assert.assertEquals(2, res.size());
  }
run a merge statement using un-partitioned target table and a concurrent op on the target
Check that proper locks are acquired and Write conflict detection works and the state
of internal table.
@param causeConflict true to make 2 operations such that they update the same entity
ql/src/test/org/apache/hadoop/hive/ql/lockmgr/TestDbTxnManager2.java