示例#1
0
  /*
   * Test em.getLockMode();
   */
  public void testGetLockMode() {
    EntityManager em = emf.createEntityManager();

    LockEmployee employee = em.find(LockEmployee.class, 1);
    try {
      em.getLockMode(employee);
      fail("Expecting TransactionRequiredException.");
    } catch (TransactionRequiredException tre) {
    } catch (Exception e) {
      fail("Expecting TransactionRequiredException.");
    }

    em.getTransaction().begin();
    try {
      assertEquals(
          "getLockMode only allows in transaction.", LockModeType.NONE, em.getLockMode(employee));
    } catch (Exception e) {
      fail("Do not expecting any exception.");
    }
    em.getTransaction().rollback();

    em.clear();
    em.getTransaction().begin();
    try {
      // getLockMode on a detached entity;
      em.getLockMode(employee);
      fail(
          "Expecting IllegalArgumentException for getLockMode on a detached entity in an active transaction.");
    } catch (IllegalArgumentException iae) {
    } catch (Exception e) {
      fail(
          "Expecting IllegalArgumentException for getLockMode on a detached entity in an active transaction.");
    }
    em.getTransaction().rollback();

    em.getTransaction().begin();
    try {
      employee = em.find(LockEmployee.class, 1, LockModeType.PESSIMISTIC_WRITE);
      assertEquals(
          "Test getLockMode on non-NONE lock mode type.",
          LockModeType.PESSIMISTIC_WRITE,
          em.getLockMode(employee));
    } catch (Exception e) {
      fail("Do not expecting any exception.");
    }
    em.getTransaction().rollback();

    em.close();
  }
  @Override
  public LockModeType getLockMode(Object entity) {
    EntityManager em = getCurrent();

    if (em != null) {
      return em.getLockMode(entity);
    }

    em = createEntityManager();

    try {
      return em.getLockMode(entity);
    } finally {
      freeEntityManager(em);
    }
  }
  public void testGetLockModeForObject() {
    EntityManager em = createEntityManager();
    Query query =
        em.createQuery(
            "select e from Employee e where e.firstName = 'Sarah' and e.lastName = 'Way'");
    query.setLockMode(LockModeType.OPTIMISTIC);
    beginTransaction(em);
    Employee emp = (Employee) query.getSingleResult();
    commitTransaction(em);
    try {
      em.getLockMode(emp);
      fail("TransactionRequiredException not thrown for getLockMode() with no transction open.");
    } catch (TransactionRequiredException e) {
    }
    clearCache();
    try {
      em.find(Employee.class, emp.getId(), LockModeType.OPTIMISTIC);
      fail(
          "TransactionRequiredException not thrown for find(Class, Object, LockModeType) with no transction open.");

    } catch (TransactionRequiredException e) {
    }
  }
 public LockModeType getLockMode(Object entity) {
   return delegate.getLockMode(getEntity(entity));
 }
 @Override
 public LockModeType getLockMode(Object arg0) {
   // TODO Auto-generated method stub
   return myEntityManager.getLockMode(arg0);
 }