Package org.hibernate.stat

Examples of org.hibernate.stat.Statistics


    }
  }

  @Test
  public void testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics( Item.class.getName() + ".items" );

    Item item = null;
    Transaction txn = null;
    Session s = null;
View Full Code Here


    }
  }

  @Test
  public void testAddNewManyToManyPropertyRefNoInitFlushInitLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics( Item.class.getName() + ".items" );

    OtherItem otherItem = null;
    Transaction txn = null;
    Session s = null;
View Full Code Here

    }
  }

  @Test
  public void testStaleWritesLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();

    VersionedItem item = null;
    Transaction txn = null;
    Session s = null;

    beginTx();
    try {
      s = openSession();
      txn = s.beginTransaction();
      item = new VersionedItem();
      item.setName( "steve" );
      item.setDescription( "steve's item" );
      s.save( item );
      txn.commit();
      s.close();
    }
    catch (Exception e) {
      setRollbackOnlyTx( e );
    }
    finally {
      commitOrRollbackTx();
    }

    Long initialVersion = item.getVersion();

    // manually revert the version property
    item.setVersion( new Long( item.getVersion().longValue() - 1 ) );

    beginTx();
    try {
      s = openSession();
      txn = s.beginTransaction();
      s.update(item);
      txn.commit();
      fail("expected stale write to fail");
    }
    catch (Exception e) {
      setRollbackOnlyTxExpected(e);
    }
    finally {
      commitOrRollbackTx();
      if ( s != null && s.isOpen() ) {
        try {
          s.close();
        }
        catch (Throwable ignore) {
        }
      }
    }

    // check the version value in the cache...
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics( VersionedItem.class.getName() );

    Object entry = slcs.getEntries().get( item.getId() );
    Long cachedVersionValue;
    cachedVersionValue = (Long) ((CacheEntry) entry).getVersion();
    assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
View Full Code Here

  }

  @Test
  @TestForIssue( jiraKey = "HHH-5690")
  public void testPersistEntityFlushRollbackNotInEntityCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();

    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics( Item.class.getName() );

    Item item = null;
    Transaction txn = null;
    Session s = null;
View Full Code Here

  }

  @Test
  @TestForIssue( jiraKey = "HHH-5690")
  public void testPersistEntityFlushEvictGetRollbackNotInEntityCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics( Item.class.getName() );

    Item item = null;
    Transaction txn = null;
    Session s = null;
View Full Code Here

    }
  }

  @Test
  public void testQueryCacheInvalidation() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();

    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics( Item.class.getName() );
    sessionFactory().getCache().evictEntityRegion( Item.class.getName() );

    assertEquals(0, slcs.getPutCount());
    assertEquals( 0, slcs.getElementCountInMemory() );
    assertEquals( 0, slcs.getEntries().size() );
View Full Code Here

    }
  }

  @Test
  public void testQueryCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();

    Session s;
    Item item = new Item( "chris", "Chris's Item" );

    beginTx();
    try {
      s = openSession();
      s.getTransaction().begin();
      s.persist( item );
      s.getTransaction().commit();
      s.close();
    }
    catch (Exception e) {
      setRollbackOnlyTx( e );
    }
    finally {
      commitOrRollbackTx();
    }

    // Delay added to guarantee that query cache results won't be considered
    // as not up to date due to persist session and query results from first
    // query happening within same 100ms gap.
    Thread.sleep( 100 );

    beginTx();
    try {
      s = openSession();
      s.createQuery( "from Item" ).setCacheable( true ).list();
      s.close();
    }
    catch (Exception e) {
      setRollbackOnlyTx( e );
    }
    finally {
      commitOrRollbackTx();
    }

    beginTx();
    try {
      s = openSession();
      s.createQuery( "from Item" ).setCacheable( true ).list();
      assertEquals( 1, stats.getQueryCacheHitCount() );
      s.createQuery( "delete from Item" ).executeUpdate();
      s.close();
    }
    catch (Exception e) {
      setRollbackOnlyTx( e );
View Full Code Here

    }
  }

  @Test
  public void testQueryCacheHitInSameTransaction() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();

    Session s = null;
    Item item = new Item( "galder", "Galder's Item" );

    beginTx();
    try {
      s = openSession();
      s.getTransaction().begin();
      s.persist( item );
      s.getTransaction().commit();
      s.close();
    }
    catch (Exception e) {
      setRollbackOnlyTx( e );
    }
    finally {
      commitOrRollbackTx();
    }

    // Delay added to guarantee that query cache results won't be considered
    // as not up to date due to persist session and query results from first
    // query happening within same 100ms gap.
    Thread.sleep( 100 );

    beginTx();
    try {
      s = openSession();
      s.createQuery( "from Item" ).setCacheable( true ).list();
      s.createQuery( "from Item" ).setCacheable( true ).list();
      assertEquals( 1, stats.getQueryCacheHitCount() );
      s.close();
    }
    catch (Exception e) {
      setRollbackOnlyTx( e );
    }
View Full Code Here

            criteria.add( Restrictions.naturalId().set( "ssn", "1234" ).set( "state", france ) );
            criteria.setCacheable( true );

            BasicTransactionalTestCase.this.cleanupCache();

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

            // first query
            List results = criteria.list();
            assertEquals( 1, results.size() );
            assertEquals( "NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount() );
            assertEquals( "NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount() );
            assertEquals( "NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount() );
            assertEquals( "NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount() );

            // query a second time - result should be cached in session
            criteria.list();
            assertEquals( "NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount() );
            assertEquals( "NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount() );
            assertEquals( "NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount() );
            assertEquals( "NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount() );

            // cleanup
            tx.rollback();
            s.close();
            return null;
View Full Code Here

      });
   }

   @Test
   public void testNaturalIdLoaderCached() throws Exception {
      final Statistics stats = sessionFactory().getStatistics();
      stats.setStatisticsEnabled( true );
      stats.clear();

      assertEquals( "NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount() );
      assertEquals( "NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount() );
      assertEquals( "NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount() );
      assertEquals( "NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount() );

      saveSomeCitizens();

      assertEquals( "NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount() );
      assertEquals( "NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount() );
      assertEquals( "NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount() );
      assertEquals( "NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount() );

      //Try NaturalIdLoadAccess after insert
      final Citizen citizen = withTx(tm, new Callable<Citizen>() {
         @Override
         public Citizen call() throws Exception {
            Session s = openSession();
            Transaction tx = s.beginTransaction();
            State france = BasicTransactionalTestCase.this.getState(s, "Ile de France");
            NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
            naturalIdLoader.using("ssn", "1234").using("state", france);

            //Not clearing naturalId caches, should be warm from entity loading
            stats.clear();

            // first query
            Citizen citizen = (Citizen) naturalIdLoader.load();
            assertNotNull(citizen);
            assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
            assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
            assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
            assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());

            // cleanup
            tx.rollback();
            s.close();
            return citizen;
         }
      });

      // TODO: Clear caches manually via cache manager (it's faster!!)
      this.cleanupCache();
      Thread.sleep(PutFromLoadValidator.NAKED_PUT_INVALIDATION_PERIOD + TimeUnit.SECONDS.toMillis(1));
      stats.setStatisticsEnabled( true );
      stats.clear();

      //Try NaturalIdLoadAccess
      withTx(tm, new Callable<Void>() {
         @Override
         public Void call() throws Exception {
            Session s = openSession();
            Transaction tx = s.beginTransaction();

            // first query
            Citizen loadedCitizen = (Citizen) s.get( Citizen.class, citizen.getId() );
            assertNotNull( loadedCitizen );
            assertEquals( "NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount() );
            assertEquals( "NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount() );
            assertEquals( "NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount() );
            assertEquals( "NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount() );

            // cleanup
            tx.rollback();
            s.close();
            return null;
         }
      });

      // Try NaturalIdLoadAccess after load
      withTx(tm, new Callable<Void>() {
         @Override
         public Void call() throws Exception {
            Session s = openSession();
            Transaction tx = s.beginTransaction();
            State france = BasicTransactionalTestCase.this.getState(s, "Ile de France");
            NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
            naturalIdLoader.using( "ssn", "1234" ).using( "state", france );

            //Not clearing naturalId caches, should be warm from entity loading
            stats.setStatisticsEnabled( true );
            stats.clear();

            // first query
            Citizen loadedCitizen = (Citizen) naturalIdLoader.load();
            assertNotNull( loadedCitizen );
            assertEquals( "NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount() );
            assertEquals( "NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount() );
            assertEquals( "NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount() );
            assertEquals( "NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount() );

            // cleanup
            tx.rollback();
            s.close();
            return null;
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.