Search in sources :

Example 71 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class QueryAndSQLTest method testClassQueries.

@Test
public void testClassQueries() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    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(14);
    s.persist(n);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    Query q = s.getNamedQuery("night.moreRecentThan");
    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 = sessionFactory().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();
}
Also used : Transaction(org.hibernate.Transaction) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Statistics(org.hibernate.stat.Statistics) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 72 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class AggressiveReleaseTest method testConnectionMaintanenceDuringFlush.

@Test
public void testConnectionMaintanenceDuringFlush() throws Throwable {
    final Statistics statistics = sessionFactory().getStatistics();
    prepare();
    Session s = getSessionUnderTest();
    List<Silly> entities = new ArrayList<Silly>();
    for (int i = 0; i < 10; i++) {
        Other other = new Other("other-" + i);
        Silly silly = new Silly("silly-" + i, other);
        entities.add(silly);
        s.save(silly);
    }
    s.flush();
    for (Silly silly : entities) {
        silly.setName("new-" + silly.getName());
        silly.getOther().setName("new-" + silly.getOther().getName());
    }
    long initialCount = statistics.getConnectCount();
    s.flush();
    assertEquals("connection not maintained through flush", initialCount + 1, statistics.getConnectCount());
    s.createQuery("delete from Silly").executeUpdate();
    s.createQuery("delete from Other").executeUpdate();
    s.getTransaction().commit();
    release(s);
    done();
}
Also used : ArrayList(java.util.ArrayList) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 73 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class HibernateCacheTest method testGeneralUsage.

@Test
public void testGeneralUsage() {
    EventManager mgr = new EventManager(sessionFactory());
    Statistics stats = sessionFactory().getStatistics();
    // create 3 persons Steve, Orion, Tim
    Person stevePerson = new Person();
    stevePerson.setFirstname("Steve");
    stevePerson.setLastname("Harris");
    Long steveId = mgr.createAndStorePerson(stevePerson);
    mgr.addEmailToPerson(steveId, "steve@tc.com");
    mgr.addEmailToPerson(steveId, "sharrif@tc.com");
    mgr.addTalismanToPerson(steveId, "rabbit foot");
    mgr.addTalismanToPerson(steveId, "john de conqueroo");
    PhoneNumber p1 = new PhoneNumber();
    p1.setNumberType("Office");
    p1.setPhone(111111);
    mgr.addPhoneNumberToPerson(steveId, p1);
    PhoneNumber p2 = new PhoneNumber();
    p2.setNumberType("Home");
    p2.setPhone(222222);
    mgr.addPhoneNumberToPerson(steveId, p2);
    Person orionPerson = new Person();
    orionPerson.setFirstname("Orion");
    orionPerson.setLastname("Letizi");
    Long orionId = mgr.createAndStorePerson(orionPerson);
    mgr.addEmailToPerson(orionId, "orion@tc.com");
    mgr.addTalismanToPerson(orionId, "voodoo doll");
    Long timId = mgr.createAndStorePerson("Tim", "Teck");
    mgr.addEmailToPerson(timId, "teck@tc.com");
    mgr.addTalismanToPerson(timId, "magic decoder ring");
    Long engMeetingId = mgr.createAndStoreEvent("Eng Meeting", stevePerson, new Date());
    mgr.addPersonToEvent(steveId, engMeetingId);
    mgr.addPersonToEvent(orionId, engMeetingId);
    mgr.addPersonToEvent(timId, engMeetingId);
    Long docMeetingId = mgr.createAndStoreEvent("Doc Meeting", orionPerson, new Date());
    mgr.addPersonToEvent(steveId, docMeetingId);
    mgr.addPersonToEvent(orionId, docMeetingId);
    for (Event event : (List<Event>) mgr.listEvents()) {
        mgr.listEmailsOfEvent(event.getId());
    }
    QueryStatistics queryStats = stats.getQueryStatistics("from Event");
    assertThat("Cache Miss Count", queryStats.getCacheMissCount(), equalTo(1L));
    assertThat("Cache Hit Count", queryStats.getCacheHitCount(), equalTo(0L));
    assertThat("Cache Put Count", queryStats.getCachePutCount(), equalTo(1L));
}
Also used : EventManager(org.hibernate.test.domain.EventManager) QueryStatistics(org.hibernate.stat.QueryStatistics) PhoneNumber(org.hibernate.test.domain.PhoneNumber) Event(org.hibernate.test.domain.Event) List(java.util.List) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) Person(org.hibernate.test.domain.Person) Date(java.util.Date) Test(org.junit.Test)

Example 74 with Statistics

use of org.hibernate.stat.Statistics in project karaf by apache.

the class StatisticsPublisher method publishStatistics.

private void publishStatistics(ServiceReference<EntityManagerFactory> reference, EntityManagerFactory emf) {
    String persitenceProvider = (String) reference.getProperty("osgi.unit.provider");
    if (!"org.hibernate.ejb.HibernatePersistence".equals(persitenceProvider)) {
        return;
    }
    if (reference.getProperty("org.apache.aries.jpa.proxy.factory") != null) {
        return;
    }
    try {
        EntityManager em = emf.createEntityManager();
        SessionFactory sessionFactory = em.unwrap(Session.class).getSessionFactory();
        final Statistics statistics = sessionFactory.getStatistics();
        statistics.setStatisticsEnabled(true);
        mbeanServer.registerMBean(getStatisticsMBean(statistics), getOName(reference));
    } catch (Exception e) {
        LOG.warn("Error publishing StatisticsMXBean" + e.getMessage(), e);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) EntityManager(javax.persistence.EntityManager) Statistics(org.hibernate.stat.Statistics) MalformedObjectNameException(javax.management.MalformedObjectNameException) Session(org.hibernate.Session)

Example 75 with Statistics

use of org.hibernate.stat.Statistics in project mamute by caelum.

the class HibernateStatisticsController method show.

@Get("/alsjkdalkjsjdhadskj")
@CustomBrutauthRules(ModeratorOnlyRule.class)
public void show() {
    Statistics statistics = sf.getStatistics();
    result.include("s", statistics);
}
Also used : Statistics(org.hibernate.stat.Statistics) CustomBrutauthRules(br.com.caelum.brutauth.auth.annotations.CustomBrutauthRules) Get(br.com.caelum.vraptor.Get)

Aggregations

Statistics (org.hibernate.stat.Statistics)97 Test (org.junit.Test)59 Session (org.hibernate.Session)27 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)26 EntityManager (javax.persistence.EntityManager)21 QueryStatistics (org.hibernate.stat.QueryStatistics)19 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)14 Transaction (org.hibernate.Transaction)12 ArrayList (java.util.ArrayList)11 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 List (java.util.List)10 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)10 SessionFactory (org.hibernate.SessionFactory)9 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)9 ByRef (org.infinispan.commons.util.ByRef)8 TestForIssue (org.hibernate.testing.TestForIssue)6 Date (java.util.Date)5 UserTransaction (javax.transaction.UserTransaction)5 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)5 Test (org.testng.annotations.Test)5