Package org.hibernate.stat

Examples of org.hibernate.stat.Statistics


    Item item = new Item( "Mouse", "Micro$oft mouse" );
    Distributor res = new Distributor();
    res.setName( "Bruce" );
    item.setDistributors( new HashSet<Distributor>() );
    item.getDistributors().add( res );
    Statistics stats = ( (HibernateEntityManagerFactory) factory ).getSessionFactory().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled( true );

    EntityManager em = factory.createEntityManager();
    em.getTransaction().begin();

    em.persist( res );
    em.persist( item );
    assertTrue( em.contains( item ) );

    em.getTransaction().commit();
    em.close();

    assertEquals( 1, stats.getSecondLevelCachePutCount() );
    assertEquals( 0, stats.getSecondLevelCacheHitCount() );

    em = factory.createEntityManager();
    em.getTransaction().begin();
    Item second = em.find( Item.class, item.getName() );
    assertEquals( 1, second.getDistributors().size() );
    assertEquals( 1, stats.getSecondLevelCacheHitCount() );
    em.getTransaction().commit();
    em.close();

    em = factory.createEntityManager();
    em.getTransaction().begin();
    second = em.find( Item.class, item.getName() );
    assertEquals( 1, second.getDistributors().size() );
    assertEquals( 3, stats.getSecondLevelCacheHitCount() );
    for ( Distributor distro : second.getDistributors() ) {
      em.remove( distro );
    }
    em.remove( second );
    em.getTransaction().commit();
    em.close();

    stats.clear();
    stats.setStatisticsEnabled( false );
  }
View Full Code Here


  public void testTransactionalOperationsWhenExtended() throws Exception {
    Book book = new Book();
    book.name = "Le petit prince";
    EntityManager em = factory.createEntityManager();
    Statistics stats = ( (HibernateEntityManagerFactory) factory ).getSessionFactory().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled( true );

    em.persist( book );
    assertEquals( 0, stats.getEntityInsertCount() );
    em.getTransaction().begin();
    em.flush();
    em.getTransaction().commit();
    assertEquals( 1, stats.getEntityInsertCount() );

    em.clear();
    book.name = "Le prince";
    book = em.merge( book );

    em.refresh( book );
    assertEquals( 0, stats.getEntityUpdateCount() );
    em.getTransaction().begin();
    em.flush();
    em.getTransaction().commit();
    assertEquals( 0, stats.getEntityUpdateCount() );

    book.name = "Le prince";
    em.getTransaction().begin();
    em.find( Book.class, book.id );
    em.getTransaction().commit();
    assertEquals( 1, stats.getEntityUpdateCount() );

    em.remove( book );
    assertEquals( 0, stats.getEntityDeleteCount() );
    em.getTransaction().begin();
    em.flush();
    em.getTransaction().commit();
    assertEquals( 1, stats.getEntityDeleteCount() );

    em.close();
    stats.setStatisticsEnabled( false );
  }
View Full Code Here

  public void testMergeWhenExtended() throws Exception {
    Book book = new Book();
    book.name = "Le petit prince";
    EntityManager em = factory.createEntityManager();
    Statistics stats = ( (HibernateEntityManagerFactory) factory ).getSessionFactory().getStatistics();

    em.getTransaction().begin();
    em.persist( book );
    assertEquals( 0, stats.getEntityInsertCount() );
    em.getTransaction().commit();

    em.clear(); //persist and clear
    stats.clear();
    stats.setStatisticsEnabled( true );

    Book bookReloaded = em.find( Book.class, book.id );

    book.name = "Le prince";
    assertEquals( "Merge should use the available entiies in the PC", em.merge( book ), bookReloaded );
    assertEquals( book.name, bookReloaded.name );

    assertEquals( 0, stats.getEntityDeleteCount() );
    assertEquals( 0, stats.getEntityInsertCount() );
    assertEquals( "Updates should have been queued", 0, stats.getEntityUpdateCount() );

    em.getTransaction().begin();
    Book bookReReloaded = em.find( Book.class, bookReloaded.id );
    assertEquals( "reload should return the object in PC", bookReReloaded, bookReloaded );
    assertEquals( bookReReloaded.name, bookReloaded.name );
    em.getTransaction().commit();

    assertEquals( 0, stats.getEntityDeleteCount() );
    assertEquals( 0, stats.getEntityInsertCount() );
    assertEquals( "Work on Tx should flush", 1, stats.getEntityUpdateCount() );

    em.getTransaction().begin();
    em.remove( bookReReloaded );
    em.getTransaction().commit();

    em.close();
    stats.setStatisticsEnabled( false );
  }
View Full Code Here

    Item item = new Item( "Mouse", "Micro$oft mouse" );
    Distributor res = new Distributor();
    res.setName( "Bruce" );
    item.setDistributors( new HashSet<Distributor>() );
    item.getDistributors().add( res );
    Statistics stats = ( (HibernateEntityManagerFactory) factory ).getSessionFactory().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled( true );

    EntityManager em = factory.createEntityManager();
    em.getTransaction().begin();

    em.persist( res );
    em.persist( item );
    assertTrue( em.contains( item ) );

    em.getTransaction().commit();
    em.close();

    assertEquals( 1, stats.getSecondLevelCachePutCount() );
    assertEquals( 0, stats.getSecondLevelCacheHitCount() );

    em = factory.createEntityManager();
    em.getTransaction().begin();
    Item second = em.find( Item.class, item.getName() );
    assertEquals( 1, second.getDistributors().size() );
    assertEquals( 1, stats.getSecondLevelCacheHitCount() );
    em.getTransaction().commit();
    em.close();

    em = factory.createEntityManager();
    em.getTransaction().begin();
    second = em.find( Item.class, item.getName() );
    assertEquals( 1, second.getDistributors().size() );
    assertEquals( 3, stats.getSecondLevelCacheHitCount() );
    em.remove( second );
    em.remove( second.getDistributors().iterator().next() );
    em.getTransaction().commit();
    em.close();

    stats.clear();
    stats.setStatisticsEnabled( false );
  }
View Full Code Here

        this.entityManagerFactory = entityManagerFactory;
    }

    public static ManagementLookup create(PersistenceUnitServiceRegistry registry, String persistenceUnitName) {

        Statistics stats = null;
        PersistenceUnitService persistenceUnitService = registry.getPersistenceUnitService(persistenceUnitName);
        if (persistenceUnitService != null) {
            final EntityManagerFactory entityManagerFactory = persistenceUnitService.getEntityManagerFactory();
            // TODO:  with JPA 2.1, if unwrap is added to EMF, change cast to "entityManagerFactory.unwrap(HibernateEntityManagerFactory.class)"
            HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
View Full Code Here

   * Check if disabling 2LC works as expected
   */
  public String disabled2LCCheck() {
   
    EntityManager em = emfNo2LC.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
   
    try {
      // check if entities are NOT cached in 2LC
      String names[] = stats.getSecondLevelCacheRegionNames();
      assertEquals("There aren't any 2LC regions.", 0, names.length);

      createEmployee(em, "Martin", "Prague 132", 1);
      assertEquals("There aren't any puts in the 2LC.", 0,stats.getSecondLevelCachePutCount());

      // check if queries are NOT cached in 2LC
      Employee emp = getEmployeeQuery(em, 1);
      assertNotNull("Employee returned", emp);
      assertEquals("There aren't any query puts in the 2LC.", 0,stats.getQueryCachePutCount());
     
      // cleanup
      em.remove(emp);
     
    }catch (AssertionError e) {
View Full Code Here

   *  Checking entity 2LC in one EntityManager session
   */
  public String sameSessionCheck(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{
      // add new entities and check if they are put in 2LC
      createEmployee(em, "Peter", "Ostrava", 2);
      createEmployee(em, "Tom", "Brno", 3);
View Full Code Here

   *  Checking entity 2LC in a different EntityManager session
   */
  public String secondSessionCheck(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{
      // add new entity
      createEmployee(em, "David", "Praha", 10);
      assertEquals("There is 1 put in the 2LC"+generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getPutCount());
View Full Code Here

   * Insert 2 entities and put them into the 2LC and then evicts entity cache.
   */
  public String addEntitiesAndEvictAll(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());
View Full Code Here

   * Checks if entity 2LC is empty.
   */
  public String evictedEntityCacheCheck(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
      assertEquals("Expected no entities stored in the cache"+emp2LCStats, 0, emp2LCStats.getElementCountInMemory());
     
      // loading entity stored in previous session, we are expecting miss in 2lc
View Full Code Here

TOP

Related Classes of org.hibernate.stat.Statistics

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.