Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your test looks very strange, you create a new application context for each test, therefore Hibernate <code>SessionFactory</code> doesn't survive between tests, as well as its second-level cache.</p> <p>Correct test would look like this:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) public class CacheTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired private MutableDAO&lt;AppUser&gt; appUserDAO; @Autowired private SessionFactory sessionFactory; private TransactionTemplate tx; @Autowired public void setPtm(PlatformTransactionManagement ptm) { tx = new TransactionTemplate(ptm); } @Test public void doTestCache() { // Using programmatic transaction management since we need 2 transactions // inside the same method // 1st attempt tx.execute(new TransactionCallbackWithoutResult() { public void doInTransactionWithoutResult(TransactionStatus status) { testCache(); } }); // 2nd attempt tx.execute(new TransactionCallbackWithoutResult() { public void doInTransactionWithoutResult(TransactionStatus status) { testCache(); } }); } public void testCache() { long numberOfUsers = appUserDAO.countAll(); System.out.println("Number of rows :" + numberOfUsers); final String cacheRegion = AppUser.class.getCanonicalName(); SecondLevelCacheStatistics settingsStatistics = sessionFactory.getStatistics(). getSecondLevelCacheStatistics(cacheRegion); StopWatch stopWatch = new StopWatch(); stopWatch.start(); appUserDAO.findAll(); stopWatch.stop(); System.out.println("Query time : " + stopWatch.getTotalTimeSeconds()); System.out.println(settingsStatistics); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload