@Test
  public void testShowLocksFilterOptions() throws Exception {
    driver.run("drop table if exists db1.t14");
    driver.run("drop table if exists db2.t14"); // Note that db1 and db2 have a table with common name
    driver.run("drop table if exists db2.t15");
    driver.run("drop table if exists db2.t16");
    driver.run("drop database if exists db1");
    driver.run("drop database if exists db2");

    driver.run("create database if not exists db1");
    driver.run("create database if not exists db2");
    driver.run("create table if not exists db1.t14 (a int, b int) partitioned by (ds string) " +
        "clustered by (b) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true')");
    driver.run("create table if not exists db2.t14 (a int, b int) " +
        "clustered by (b) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true')");
    driver.run("create table if not exists db2.t15 (a int, b int) " +
        "clustered by (b) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true')");
    driver.run("create table if not exists db2.t16 (a int, b int) " +
        "clustered by (b) into 2 buckets stored as orc TBLPROPERTIES ('transactional'='true')");

    conf.setBoolVar(HiveConf.ConfVars.TXN_WRITE_X_LOCK, true);
    // Acquire different locks at different levels

    HiveTxnManager txnMgr1 = TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf);
    swapTxnManager(txnMgr1);
    driver.compileAndRespond("insert into table db1.t14 partition (ds='today') values (1, 2)", true);
    txnMgr1.acquireLocks(driver.getPlan(), ctx, "Tom");

    HiveTxnManager txnMgr2 = TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf);
    swapTxnManager(txnMgr2);
    driver.compileAndRespond("insert into table db1.t14 partition (ds='tomorrow') values (3, 4)", true);
    txnMgr2.acquireLocks(driver.getPlan(), ctx, "Jerry");

    HiveTxnManager txnMgr3 = TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf);
    swapTxnManager(txnMgr3);
    driver.compileAndRespond("select * from db2.t15", true);
    txnMgr3.acquireLocks(driver.getPlan(), ctx, "Donald");

    HiveTxnManager txnMgr4 = TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf);
    swapTxnManager(txnMgr4);
    driver.compileAndRespond("select * from db2.t16", true);
    txnMgr4.acquireLocks(driver.getPlan(), ctx, "Hillary");

    HiveTxnManager txnMgr5 = TxnManagerFactory.getTxnManagerFactory().getTxnManager(conf);
    swapTxnManager(txnMgr5);
    driver.compileAndRespond("select * from db2.t14", true);
    txnMgr5.acquireLocks(driver.getPlan(), ctx, "Obama");

    // Simulate SHOW LOCKS with different filter options

    // SHOW LOCKS (no filter)
    List<ShowLocksResponseElement> locks = getLocks();
    Assert.assertEquals("Unexpected lock count", 5, locks.size());
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db1", "t14", "ds=today", locks);
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db1", "t14", "ds=tomorrow", locks);
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db2", "t15", null, locks);
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db2", "t16", null, locks);
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db2", "t14", null, locks);

    // SHOW LOCKS db2
    locks = getLocksWithFilterOptions(txnMgr3, "db2", null, null);
    Assert.assertEquals("Unexpected lock count", 3, locks.size());
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db2", "t15", null, locks);
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db2", "t16", null, locks);
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db2", "t14", null, locks);

    // SHOW LOCKS t14
    swapTxnManager(txnMgr);
    driver.run("use db1");
    locks = getLocksWithFilterOptions(txnMgr, null, "t14", null);
    Assert.assertEquals("Unexpected lock count", 2, locks.size());
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db1", "t14", "ds=today", locks);
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db1", "t14", "ds=tomorrow", locks);
    // Note that it shouldn't show t14 from db2

    // SHOW LOCKS t14 PARTITION ds='today'
    Map<String, String> partSpec = Collections.singletonMap("ds", "today");
    locks = getLocksWithFilterOptions(txnMgr, null, "t14", partSpec);
    Assert.assertEquals("Unexpected lock count", 1, locks.size());
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db1", "t14", "ds=today", locks);

    // SHOW LOCKS t15
    driver.run("use db2");
    locks = getLocksWithFilterOptions(txnMgr3, null, "t15", null);
    Assert.assertEquals("Unexpected lock count", 1, locks.size());
    TestTxnDbUtil.checkLock(LockType.SHARED_READ, LockState.ACQUIRED, "db2", "t15", null, locks);
  }
SessionState is stored in ThreadLoacal; UnitTest runs in a single thread (otherwise Derby wedges)
{@link HiveTxnManager} instances are per SessionState.
So to be able to simulate concurrent locks/transactions s/o forking threads we just swap
the TxnManager instance in the session (hacky but nothing is actually threading so it allows us
to write good tests)
ql/src/test/org/apache/hadoop/hive/ql/lockmgr/TestDbTxnManager2.java