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 = getOrCreateEntityManager();
    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 = getOrCreateEntityManager();
    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 = getOrCreateEntityManager();
    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


    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 ) emf ).getSessionFactory().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled( true );

    EntityManager em = emf.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 = emf.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 = emf.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 );
    emf.close();
  }
View Full Code Here

      if ( anotherB2.getId() == 1 ) {
        anotherB2.getC2List().add( c2 );
        c2.setB2( anotherB2 );
      }
    }
    Statistics statistics = em.unwrap(Session.class).getSessionFactory().getStatistics();
    statistics.setStatisticsEnabled( true );
    statistics.clear();
    em.persist( c2 );
    long loaded = statistics.getEntityLoadCount();
    assertEquals( 0, loaded );
    em.flush();
    em.getTransaction().rollback();
    em.close();
  }
View Full Code Here

            france
        )
    );
    criteria.setCacheable( true );

    Statistics stats = getSessions().getStatistics();
    stats.setStatisticsEnabled( true );
    stats.clear();
    assertEquals(
        "Cache hits should be empty", 0, stats
            .getQueryCacheHitCount()
    );

    // first query
    List results = criteria.list();
    assertEquals( 1, results.size() );
    assertEquals(
        "Cache hits should be empty", 0, stats
            .getQueryCacheHitCount()
    );
    assertEquals(
        "First query should be a miss", 1, stats
            .getQueryCacheMissCount()
    );
    assertEquals(
        "Query result should be added to cache", 1, stats
            .getQueryCachePutCount()
    );

    // query a second time - result should be cached
    criteria.list();
    assertEquals(
        "Cache hits should be empty", 1, stats
            .getQueryCacheHitCount()
    );

    // cleanup
    tx.rollback();
View Full Code Here

            france
        )
    );
    criteria.setCacheable( false );

    Statistics stats = getSessions().getStatistics();
    stats.setStatisticsEnabled( true );
    stats.clear();
    assertEquals(
        "Cache hits should be empty", 0, stats
            .getQueryCacheHitCount()
    );

    // first query
    List results = criteria.list();
    assertEquals( 1, results.size() );
    assertEquals(
        "Cache hits should be empty", 0, stats
            .getQueryCacheHitCount()
    );
    assertEquals(
        "Query result should be added to cache", 0, stats
            .getQueryCachePutCount()
    );

    // query a second time
    criteria.list();
    assertEquals(
        "Cache hits should be empty", 0, stats
            .getQueryCacheHitCount()
    );

    // cleanup
    tx.rollback();
View Full Code Here

    tx = s.beginTransaction();
    Criteria criteria = s.createCriteria( NaturalIdOnManyToOne.class );
    criteria.add( Restrictions.naturalId().set( "citizen", c1 ) );
    criteria.setCacheable( true );

    Statistics stats = getSessions().getStatistics();
    stats.setStatisticsEnabled( true );
    stats.clear();
    assertEquals(
        "Cache hits should be empty", 0, stats
            .getQueryCacheHitCount()
    );

    // first query
    List results = criteria.list();
    assertEquals( 1, results.size() );
    assertEquals(
        "Cache hits should be empty", 0, stats
            .getQueryCacheHitCount()
    );
    assertEquals(
        "First query should be a miss", 1, stats
            .getQueryCacheMissCount()
    );
    assertEquals(
        "Query result should be added to cache", 1, stats
            .getQueryCachePutCount()
    );

    // query a second time - result should be cached
    criteria.list();
    assertEquals(
        "Cache hits should be empty", 1, stats
            .getQueryCacheHitCount()
    );

    // cleanup
    tx.rollback();
View Full Code Here

    q.setDate( "date", aMonthAgo );
    assertEquals( 1, q.list().size() );
    q = s.getNamedQuery( "night.moreRecentThan" );
    q.setDate( "date", inAMonth );
    assertEquals( 0, q.list().size() );
    Statistics stats = getSessions().getStatistics();
    stats.setStatisticsEnabled( true );
    stats.clear();
    q = s.getNamedQuery( "night.duration" );
    q.setParameter( "duration", 14l );
    assertEquals( 1, q.list().size() );
    assertEquals( 1, stats.getQueryCachePutCount() );
    q = s.getNamedQuery( "night.duration" );
    q.setParameter( "duration", 14l );
    s.delete( q.list().get( 0 ) );
    assertEquals( 1, stats.getQueryCacheHitCount() );
    tx.commit();
    s.close();
  }
View Full Code Here

    }
    s.close();
  }

  public void testSQLQueryWithManyToOne() {
    Statistics stats = getSessions().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled( true );
    Night n = new Night();
    Calendar c = new GregorianCalendar();
    c.set( 2000, 2, 2 );
    Date now = c.getTime();
    c.add( Calendar.MONTH, -1 );
    Date aMonthAgo = c.getTime();
    c.add( Calendar.MONTH, 2 );
    Date inAMonth = c.getTime();
    n.setDate( now );
    n.setDuration( 9999 );
    Area a = new Area();
    a.setName( "Paris" );
    n.setArea( a );
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist( a );
    s.persist( n );
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    Query q = s.getNamedQuery( "night&areaCached" );
    List result = q.setCacheable( true ).list();
    assertEquals( 1, result.size() );
    assertEquals( 1, stats.getQueryCachePutCount() );
    q.list();
    assertEquals( 1, stats.getQueryCacheHitCount() );
    Night n2 = ( Night ) ( ( Object[] ) result.get( 0 ) )[0];
    assertEquals( n2.getDuration(), n.getDuration() );
    tx.commit();
    s.close();
  }
View Full Code Here

  public static Test suite() {
    return new FunctionalTestClassTestSuite( StatsTest.class );
  }

  public void testCollectionFetchVsLoad() throws Exception {
    Statistics stats = getSessions().getStatistics();
    stats.clear();

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Continent europe = fillDb(s);
    tx.commit();
    s.clear();

    tx = s.beginTransaction();
    assertEquals(0, stats.getCollectionLoadCount() );
    assertEquals(0,  stats.getCollectionFetchCount() );
    Continent europe2 = (Continent) s.get( Continent.class, europe.getId() );
    assertEquals("Lazy true: no collection should be loaded", 0, stats.getCollectionLoadCount() );
    assertEquals( 0, stats.getCollectionFetchCount() );
    europe2.getCountries().size();
    assertEquals( 1, stats.getCollectionLoadCount() );
    assertEquals("Explicit fetch of the collection state", 1, stats.getCollectionFetchCount() );
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    stats.clear();
    europe = fillDb(s);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    assertEquals( 0, stats.getCollectionLoadCount() );
    assertEquals( 0, stats.getCollectionFetchCount() );
    europe2 = (Continent) s.createQuery(
        "from " + Continent.class.getName() + " a join fetch a.countries where a.id = " + europe.getId()
      ).uniqueResult();
    assertEquals( 1, stats.getCollectionLoadCount() );
    assertEquals( "collection should be loaded in the same query as its parent", 0, stats.getCollectionFetchCount() );
    tx.commit();
    s.close();

    Collection coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
    coll.setFetchMode(FetchMode.JOIN);
    coll.setLazy(false);
    SessionFactory sf = getCfg().buildSessionFactory();
    stats = sf.getStatistics();
    stats.clear();
    stats.setStatisticsEnabled(true);
    s = sf.openSession();
    tx = s.beginTransaction();
    europe = fillDb(s);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    assertEquals( 0, stats.getCollectionLoadCount() );
    assertEquals( 0, stats.getCollectionFetchCount() );
    europe2 = (Continent) s.get( Continent.class, europe.getId() );
    assertEquals( 1, stats.getCollectionLoadCount() );
    assertEquals( "Should do direct load, not indirect second load when lazy false and JOIN", 0, stats.getCollectionFetchCount() );
    tx.commit();
    s.close();
    sf.close();

    coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
    coll.setFetchMode(FetchMode.SELECT);
    coll.setLazy(false);
    sf = getCfg().buildSessionFactory();
    stats = sf.getStatistics();
    stats.clear();
    stats.setStatisticsEnabled(true);
    s = sf.openSession();
    tx = s.beginTransaction();
    europe = fillDb(s);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    assertEquals( 0, stats.getCollectionLoadCount() );
    assertEquals( 0, stats.getCollectionFetchCount() );
    europe2 = (Continent) s.get( Continent.class, europe.getId() );
    assertEquals( 1, stats.getCollectionLoadCount() );
    assertEquals( "Should do explicit collection load, not part of the first one", 1, stats.getCollectionFetchCount() );
    Iterator countries = europe2.getCountries().iterator();
    while ( countries.hasNext() ) {
      s.delete( countries.next() );
    }
    cleanDb( s );
View Full Code Here

        return false;
    }

    public void testEmptySecondLevelCacheEntry() throws Exception {
        getSessions().evictEntity(Item.class.getName());
        Statistics stats = getSessions().getStatistics();
        stats.clear();
        SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics(getPrefixedRegionName(Item.class.getName()));
        Map cacheEntries = statistics.getEntries();
        assertEquals(0, cacheEntries.size());
    }
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.