Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
How do I test that my second level cache is working properly?
I have tried enabling the second level cache (ehcache) and wrote a test case to read a table with over 40,000 rows. I have a loop that will spawn one thread after another to read the same data from table over and over, so none of these threads concurrently access the database. I have set the cache to read-only in the mapping file, and I flush, clear, and close the session in between test runs.
When I start my test, it looks like my cache is setup properly because i see these lines at startup:
Code:
Jun-28-2005 11:52:25 INFO (SettingsFactory.java:209) - Second-level cache: enabled
Jun-28-2005 11:52:25 INFO (SettingsFactory.java:213) - Query cache: disabled
Jun-28-2005 11:52:25 INFO (SettingsFactory.java:321) - Cache provider: org.hibernate.cache.EhCacheProvider
Jun-28-2005 11:52:25 INFO (SettingsFactory.java:228) - Optimize cache for minimal puts: disabled
Jun-28-2005 11:52:25 INFO (SettingsFactory.java:233) - Cache region prefix: hibernate.test
Jun-28-2005 11:52:25 INFO (SettingsFactory.java:237) - Structured second-level cache entries: disabled
Jun-28-2005 11:52:26 WARN (Configurator.java:126) - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/hibernate-3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
Jun-28-2005 11:52:26 WARN (CacheFactory.java:43) - read-only cache configured for mutable class: hibernate.test.com.itp.gt.reference.pojos.RefBic
Jun-28-2005 11:52:26 WARN (EhCacheProvider.java:97) - Could not find configuration [hibernate.test.com.itp.gt.reference.pojos.RefBic]; using defaults.
Jun-28-2005 11:52:26 INFO (SessionFactoryObjectFactory.java:82) - Not binding factory to JNDI, no JNDI name configured
When I enable second level cache, the performance does not change (the timings in my test runs are the same when second level cache is disabled). Also, I noticed that there is still network activity after the first run. I'm assuming that after the first run, the data should be in the cache and that threads should not access the database through the network if the data is already in the cache.
Is this correct?
How do I check if my cache is working and how do I view what's in my cache?
Hibernate version: 3.0.5
Mapping documents:RefBic.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="com.itp.gt.reference.pojos.RefBic" table="REF_BIC" schema="S">
<id name="bic" type="string">
<column name="BIC" length="15" />
<generator class="assigned" />
</id>
<property name="institution" type="string">
<column name="INSTITUTION" length="120" />
</property>
<property name="branch" type="string">
<column name="BRANCH" length="80" />
</property>
<property name="city" type="string">
<column name="CITY" length="50" />
</property>
<property name="country" type="string">
<column name="COUNTRY" length="50" />
</property>
<property name="srcInfo" type="string">
<column name="SRC_INFO" length="2" />
</property>
<property name="updateDate" type="timestamp">
<column name="UPDATE_DATE" length="7" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close(): Code:
logger.info(logger.getName() + " loadAllRefBic");
Transaction myTransaction = null;
long startTime = System.currentTimeMillis();
try {
myTransaction = hibernateSession.beginTransaction();
Criteria query = hibernateSession.createCriteria(RefBic.class);
java.util.Iterator results = query.list().iterator();
RefBic myRefBic;
java.util.LinkedList retrievedArtifacts = new java.util.LinkedList();
while (results.hasNext()) {
myRefBic = (RefBic) results.next();
// logger.info(myRefBic.getInstitution());
if (!retrievedArtifacts.contains(myRefBic))
retrievedArtifacts.add(myRefBic);
}
myTransaction.commit();
hibernateSession.clear();
} catch (Exception e) {
e.printStackTrace();
try {
myTransaction.rollback();
} catch (Exception e2) {
// Silent failure of transaction rollback
}
} finally {
long duration = System.currentTimeMillis() - startTime;
logger.info("Timing # : " + duration);
totalTime += duration;
logger.info("Total time: " + totalTime);
try {
if (hibernateSession != null) {
hibernateSession.flush();
hibernateSession.close();
}
} catch (Exception e) {
e.printStackTrace();
// Silent failure of session close
}
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
oracle 9.2
The generated SQL (show_sql=true):
Jun-28-2005 11:53:36 INFO (?:?) - com.itp.gt.reference.cachetest.CacheTest loadAllRefBic
Hibernate: select this_.BIC as BIC0_, this_.INSTITUTION as INSTITUT2_6_0_, this_.BRANCH as BRANCH6_0_, this_.CITY as CITY6_0_, this_.COUNTRY as COUNTRY6_0_, this_.SRC_INFO as SRC6_6_0_, this_.UPDATE_DATE as UPDATE7_6_0_ from STEVEN.REF_BIC this_
Debug level Hibernate log excerpt: