Example #1
0
  private void shutdown() {
    if (entityManager != null) {
      try {
        if (entityManager.getTransaction().isActive()) {
          entityManager.getTransaction().rollback();
        }
      } finally {
        entityManager.close();
        entityManager = null;
      }
    }

    if (entityManagerFactory != null) {
      entityManagerFactory.getCache().evictAll();
      entityManagerFactory.close();
      entityManagerFactory = null;
    }

    // clean shutdown of derby
    if (isDerby) {
      try {
        DriverManager.getConnection("jdbc:derby:;shutdown=true");
      } catch (SQLException e) {
        if (!e.getMessage().equals("Derby system shutdown.")) {
          throw new RuntimeException(e);
        }
      }
    }
  }
Example #2
0
  /** Check if eviction of entity cache is working */
  public String evict2LCCheck(String CACHE_REGION_NAME) {

    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics emp2LCStats =
        stats.getSecondLevelCacheStatistics(CACHE_REGION_NAME + "Employee");

    try {
      createEmployee(em, "Jan", "Ostrava", 20);
      createEmployee(em, "Martin", "Brno", 30);
      assertEquals(
          "There are 2 puts in the 2LC" + generateEntityCacheStats(emp2LCStats),
          2,
          emp2LCStats.getPutCount());

      assertTrue(
          "Expected entities stored in the cache" + generateEntityCacheStats(emp2LCStats),
          emp2LCStats.getElementCountInMemory() > 0);

      // evict cache and check if is empty
      emf.getCache().evictAll();
      assertEquals(
          "Expected no entities stored in the cache" + generateEntityCacheStats(emp2LCStats),
          0,
          emp2LCStats.getElementCountInMemory());

    } catch (AssertionError e) {
      return e.getMessage();
    } finally {
      em.close();
    }
    return "OK";
  }