public void testCreateAssassinWithGun() {
    EntityManager em = createEntityManager();

    try {
      beginTransaction(em);

      Assassin assassin = new Assassin();
      assassin.setName("Assassin1");

      Gun gun = new Gun();
      gun.setCaliber(new Integer(50));
      gun.setDescription("Sniper rifle");
      gun.setModel("9-112");

      assassin.setWeapon(gun);

      em.persist(assassin);
      assassinId = assassin.getId();
      gunSerialNumber = gun.getSerialNumber();

      commitTransaction(em);
    } catch (Exception exception) {
      exception.printStackTrace();
      fail("Error persisting new assassin: " + exception.getMessage());
    } finally {
      closeEntityManager(em);
    }
  }
  // Tested for an Assassin and a SpecialAssasin
  protected void optimisticLockTestOnAssassin(Integer id) {
    // Cannot create parallel entity managers in the server.
    if (!isOnServer()) {
      EntityManager em1 = createEntityManager();
      EntityManager em2 = createEntityManager();

      em1.getTransaction().begin();
      em2.getTransaction().begin();

      RuntimeException caughtException = null;

      try {
        Assassin assassin1 = em1.find(Assassin.class, id);
        Assassin assassin2 = em2.find(Assassin.class, id);

        assassin1.setName("Geezer");
        assassin2.setName("Guyzer");

        em1.getTransaction().commit();
        em2.getTransaction().commit();

        em1.close();
        em2.close();
      } catch (RuntimeException e) {
        if (em1.getTransaction().isActive()) {
          em1.getTransaction().rollback();
        }

        if (em2.getTransaction().isActive()) {
          em2.getTransaction().rollback();
        }

        if (e.getCause() instanceof javax.persistence.OptimisticLockException) {
          caughtException = e;
        } else {
          throw e;
        }
      } finally {
        em1.close();
        em2.close();
      }

      if (caughtException == null) {
        fail("Optimistic lock exception was not thrown.");
      }
    }
  }
  public void testAssassinOptimisticLockingUsingEntityManagerAPI() {
    // Cannot create parallel entity managers in the server.
    if (!isOnServer()) {
      EntityManager em = createEntityManager();
      beginTransaction(em);
      Assassin assassin = null;

      try {
        assassin = new Assassin();
        assassin.setName("OptLockAssassin");
        em.persist(assassin);
        commitTransaction(em);
      } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
          rollbackTransaction(em);
        }

        closeEntityManager(em);
        throw ex;
      }

      EntityManager em2 = createEntityManager();
      Exception optimisticLockException = null;
      beginTransaction(em);

      try {
        assassin = em.find(Assassin.class, assassin.getId());
        em.lock(assassin, LockModeType.WRITE);
        beginTransaction(em2);

        try {
          Assassin assassin2 = em2.find(Assassin.class, assassin.getId());
          assassin2.setName("OptLockAssassin2");
          commitTransaction(em2);
          em2.close();
        } catch (RuntimeException ex) {
          if (isTransactionActive(em2)) {
            rollbackTransaction(em2);
          }

          closeEntityManager(em2);
          throw ex;
        }

        try {
          em.flush();
        } catch (PersistenceException exception) {
          if (exception instanceof OptimisticLockException) {
            optimisticLockException = exception;
          } else {
            throw exception;
          }
        }

        rollbackTransaction(em);
      } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
          rollbackTransaction(em);
        }

        closeEntityManager(em);
        throw ex;
      }

      try {
        beginTransaction(em);
        assassin = em.find(Assassin.class, assassin.getId());
        em.remove(assassin);
        commitTransaction(em);
      } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
          rollbackTransaction(em);
        }

        closeEntityManager(em);
        throw ex;
      }

      assertFalse(
          "Proper exception not thrown when EntityManager.lock(object, OPTIMISTIC) is used.",
          optimisticLockException == null);
    }
  }