@Test
  public void testAccountUpdatesFailureInTransaction() throws Exception {

    mySqlConfigProps.setProperty(DB_EXECUTE_BATCH_SIZE, "5");
    mySqlAccountService = getAccountService();

    // Add account "a1"with few containers
    List<Container> containers = new ArrayList<>();
    for (int i = 1; i <= 3; i++) {
      containers.add(
          new ContainerBuilder((short) i, "c" + i, Container.ContainerStatus.ACTIVE, "c" + i, (short) 1).build());
    }
    Account a1 = new AccountBuilder((short) 1, "a1", Account.AccountStatus.ACTIVE).containers(containers).build();
    mySqlAccountService.updateAccounts(Collections.singletonList(a1));

    // Add few more containers with no conflicts.
    containers.clear();
    for (int i = 4; i <= 6; i++) {
      containers.add(
          new ContainerBuilder((short) i, "c" + i, Container.ContainerStatus.ACTIVE, "c" + i, (short) 1).build());
    }
    a1 = new AccountBuilder((short) 1, "a1", Account.AccountStatus.ACTIVE).containers(containers).build();
    mySqlAccountService.getCachedAccountService().updateAccountsWithMySqlStore(Collections.singletonList(a1));

    // Add few more containers with one of them (container ID: 6) conflicting.
    containers.clear();
    for (int i = 6; i <= 9; i++) {
      containers.add(
          new ContainerBuilder((short) i, "c" + i, Container.ContainerStatus.ACTIVE, "c" + i, (short) 1).build());
    }
    Account a1Updated =
        new AccountBuilder((short) 1, "a1", Account.AccountStatus.ACTIVE).containers(containers).build();

    // Verify updateAccountsWithMySqlStore() fails with Integrity constraint exception
    TestUtils.assertException(SQLException.class, () -> mySqlAccountService.getCachedAccountService()
        .updateAccountsWithMySqlStore(Collections.singletonList(a1Updated)), null);

    // Verify account update failed atomically with none of the containers (7 to 9) added.
    mySqlAccountService.fetchAndUpdateCache();
    assertEquals("Mismatch in number of containers of a1", 6,
        mySqlAccountService.getAccountById(a1.getId()).getAllContainers().size());
  }
Tests update account with container conflicts fails atomically
ambry-account/src/integration-test/java/com/github/ambry/account/MySqlAccountServiceIntegrationTest.java