/** * Query statistics from query string (HQL or SQL) * * @param queryString query string * * @return QueryStatistics */ @Override public QueryStatistics getQueryStatistics(String queryString) { ConcurrentQueryStatisticsImpl qs = (ConcurrentQueryStatisticsImpl) queryStatistics.get( queryString ); if ( qs == null ) { qs = new ConcurrentQueryStatisticsImpl( queryString ); ConcurrentQueryStatisticsImpl previous; if ( ( previous = (ConcurrentQueryStatisticsImpl) queryStatistics.putIfAbsent( queryString, qs ) ) != null ) { qs = previous; } } return qs; }
protected void verifyQueryCount(final Statistics stats) { final long totalQueryCount = Math.max(stats.getQueryExecutionCount(), stats.getPrepareStatementCount()); if (assertions.maxQueries() >= 0 && totalQueryCount > assertions.maxQueries()) { final StringBuilder msgBuf = new StringBuilder(); msgBuf.append("Statements prepared: ").append(stats.getPrepareStatementCount()); // Create list of queries for debugging purposes final List<String> queryLines = Stream.of(stats.getQueries()) .map(query -> { final QueryStatistics qStats = stats.getQueryStatistics(query); return Tuple.of(qStats.getExecutionCount(), query); }) .sorted(reverseOrder()) .map(pair -> String.format("%s: %s", StringUtils.leftPad(pair._1.toString(), 3, ' '), pair._2)) .collect(toList()); if (!queryLines.isEmpty()) { msgBuf.append("\n Queries (ordered by execution count): ") .append(stats.getQueryExecutionCount()) .append("\n ") .append(Joiner.on("\n ").join(queryLines)); } throw new MaximumQueryCountExceededException(String.format("%s\n %s\n", MaximumQueryCountExceededException.getErrorMessage(assertions.maxQueries(), totalQueryCount), msgBuf.toString())); } if (assertions.queryCount() >= 0 && totalQueryCount != assertions.queryCount()) { throw new QueryCountAssertionException(assertions.queryCount(), totalQueryCount); } }
public QueryStatistics getQueryStatistics(String queryString) { return null; }
/** * @see StatisticsServiceMBean#getQueryStatistics(java.lang.String) */ public QueryStatistics getQueryStatistics(String hql) { return stats.getQueryStatistics(hql); }
public void testQueryCacheInvalidation() throws Exception { getSessions().evictQueries(); getSessions().getStatistics().clear(); final String queryString = "from Item i where i.name='widget'"; Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( queryString ).setCacheable(true).list(); Item i = new Item(); i.setName("widget"); i.setDescription("A really top-quality, full-featured widget."); s.save(i); t.commit(); s.close(); QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics( queryString ); EntityStatistics es = s.getSessionFactory().getStatistics().getEntityStatistics( Item.class.getName() ); Thread.sleep(200); s = openSession(); t = s.beginTransaction(); List result = s.createQuery( queryString ).setCacheable(true).list(); assertEquals( result.size(), 1 ); t.commit(); s.close(); assertEquals( qs.getCacheHitCount(), 0 ); s = openSession(); t = s.beginTransaction(); result = s.createQuery( queryString ).setCacheable(true).list(); assertEquals( result.size(), 1 ); t.commit(); s.close(); assertEquals( qs.getCacheHitCount(), 1 ); assertEquals( s.getSessionFactory().getStatistics().getEntityFetchCount(), 0 ); s = openSession(); t = s.beginTransaction(); result = s.createQuery( queryString ).setCacheable(true).list(); assertEquals( result.size(), 1 ); assertTrue( Hibernate.isInitialized( result.get(0) ) ); i = (Item) result.get(0); i.setName("Widget"); t.commit(); s.close(); assertEquals( qs.getCacheHitCount(), 2 ); assertEquals( qs.getCacheMissCount(), 2 ); assertEquals( s.getSessionFactory().getStatistics().getEntityFetchCount(), 0 ); Thread.sleep(200); s = openSession(); t = s.beginTransaction(); result = s.createQuery( queryString ).setCacheable(true).list(); if ( dialectIsCaseSensitive("i.name='widget' should not match on case sensitive database.") ) { assertEquals( result.size(), 0 ); } i = (Item) s.get( Item.class, new Long(i.getId()) ); assertEquals( i.getName(), "Widget" ); s.delete(i); t.commit(); s.close(); assertEquals( qs.getCacheHitCount(), 2 ); assertEquals( qs.getCacheMissCount(), 3 ); assertEquals( qs.getCachePutCount(), 3 ); assertEquals( qs.getExecutionCount(), 3 ); assertEquals( es.getFetchCount(), 0 ); //check that it was being cached }
public void testQueryCacheFetch() throws Exception { getSessions().evictQueries(); getSessions().getStatistics().clear(); Session s = openSession(); Transaction t = s.beginTransaction(); Item i = new Item(); i.setName("widget"); i.setDescription("A really top-quality, full-featured widget."); Item i2 = new Item(); i2.setName("other widget"); i2.setDescription("Another decent widget."); s.persist(i); s.persist(i2); t.commit(); s.close(); final String queryString = "from Item i where i.name like '%widget'"; QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics( queryString ); Thread.sleep(200); s = openSession(); t = s.beginTransaction(); List result = s.createQuery( queryString ).setCacheable(true).list(); assertEquals( result.size(), 2 ); t.commit(); s.close(); assertEquals( qs.getCacheHitCount(), 0 ); assertEquals( s.getSessionFactory().getStatistics().getEntityFetchCount(), 0 ); getSessions().evict(Item.class); s = openSession(); t = s.beginTransaction(); result = s.createQuery( queryString ).setCacheable(true).list(); assertEquals( result.size(), 2 ); assertTrue( Hibernate.isInitialized( result.get(0) ) ); assertTrue( Hibernate.isInitialized( result.get(1) ) ); t.commit(); s.close(); assertEquals( qs.getCacheHitCount(), 1 ); assertEquals( s.getSessionFactory().getStatistics().getEntityFetchCount(), 1 ); s = openSession(); t = s.beginTransaction(); s.createQuery("delete Item").executeUpdate(); t.commit(); s.close(); }
public void testQueryStatGathering() { Statistics stats = getSessions().getStatistics(); stats.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); fillDb(s); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); final String continents = "from Continent"; int results = s.createQuery( continents ).list().size(); QueryStatistics continentStats = stats.getQueryStatistics( continents ); assertNotNull( "stats were null", continentStats ); assertEquals( "unexpected execution count", 1, continentStats.getExecutionCount() ); assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() ); long maxTime = continentStats.getExecutionMaxTime(); assertEquals( maxTime, stats.getQueryExecutionMaxTime() ); // assertEquals( continents, stats.getQueryExecutionMaxTimeQueryString() ); Iterator itr = s.createQuery( continents ).iterate(); // iterate() should increment the execution count assertEquals( "unexpected execution count", 2, continentStats.getExecutionCount() ); // but should not effect the cumulative row count assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() ); Hibernate.close( itr ); ScrollableResults scrollableResults = s.createQuery( continents ).scroll(); // same deal with scroll()... assertEquals( "unexpected execution count", 3, continentStats.getExecutionCount() ); assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() ); scrollableResults.close(); tx.commit(); s.close(); // explicitly check that statistics for "split queries" get collected // under the original query stats.clear(); s = openSession(); tx = s.beginTransaction(); final String localities = "from Locality"; results = s.createQuery( localities ).list().size(); QueryStatistics localityStats = stats.getQueryStatistics( localities ); assertNotNull( "stats were null", localityStats ); // ...one for each split query assertEquals( "unexpected execution count", 2, localityStats.getExecutionCount() ); assertEquals( "unexpected row count", results, localityStats.getExecutionRowCount() ); maxTime = localityStats.getExecutionMaxTime(); assertEquals( maxTime, stats.getQueryExecutionMaxTime() ); // assertEquals( localities, stats.getQueryExecutionMaxTimeQueryString() ); tx.commit(); s.close(); assertFalse( s.isOpen() ); // native sql queries stats.clear(); s = openSession(); tx = s.beginTransaction(); final String sql = "select id, name from Country"; results = s.createSQLQuery( sql ).addEntity( Country.class ).list().size(); QueryStatistics sqlStats = stats.getQueryStatistics( sql ); assertNotNull( "sql stats were null", sqlStats ); assertEquals( "unexpected execution count", 1, sqlStats.getExecutionCount() ); assertEquals( "unexpected row count", results, sqlStats.getExecutionRowCount() ); maxTime = sqlStats.getExecutionMaxTime(); assertEquals( maxTime, stats.getQueryExecutionMaxTime() ); // assertEquals( sql, stats.getQueryExecutionMaxTimeQueryString() ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); cleanDb( s ); tx.commit(); s.close(); }
public QueryStatistics getQueryStatistics(String arg0) { return delegate.getQueryStatistics( arg0 ); }