I'm trying to investigate caching behavior. I've built a small .jsp page to display statistics about the 2nd level cache. When using EhCache, the page displays correctly and the statistics are what I'd expect.
When I switch to TreeCache, I get a null pointer when trying to read the cache statistics. (stack trace below)
Hibernate version:
Hibernate 3.0.1
JBossCache 1.2.1 (from manifest of jboss-cache.jar)
Tomcat 5.5.9 under Java 1.5.0_02
Full stack trace of any exception that occurs:
Code:
org.hibernate.cache.CacheException: java.lang.NullPointerException
org.hibernate.cache.TreeCache.getElementCountInMemory(TreeCache.java:117)
org.hibernate.stat.SecondLevelCacheStatistics.getElementCountInMemory(SecondLevelCacheStatistics.java:36)
org.apache.jsp.internal.cacheinfo_jsp._jspService(org.apache.jsp.internal.cacheinfo_jsp:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
com.cpex.mpv.participants.RecordLoginFilter.doFilter(RecordLoginFilter.java:75)
com.cpex.mpv.marketplace.UserRunFilter.doFilter(UserRunFilter.java:81)
com.cpex.mpv.persistence.SessionFilter.doFilter(SessionFilter.java:58)
Mapping documents:In hibernate.cfg.xml:
Code:
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.TreeCacheProvider</property>
<!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
Treecache.xml:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<server>
<classpath codebase="./lib" archives="jboss-cache.jar, jgroups.jar" />
<!-- ==================================================================== -->
<!-- Defines TreeCache configuration -->
<!-- ==================================================================== -->
<mbean code="org.jboss.cache.TreeCache" name="jboss.cache:service=TreeCache">
<depends>jboss:service=Naming</depends>
<depends>jboss:service=TransactionManager</depends>
<!-- Configure the TransactionManager -->
<attribute name="TransactionManagerLookupClass">org.jboss.cache.DummyTransactionManagerLookup</attribute>
<!--
Node locking level : SERIALIZABLE
REPEATABLE_READ (default)
READ_COMMITTED
READ_UNCOMMITTED
NONE
-->
<attribute name="IsolationLevel">REPEATABLE_READ</attribute>
<!-- Valid modes are LOCAL
REPL_ASYNC
REPL_SYNC
-->
<attribute name="CacheMode">LOCAL</attribute>
<!-- Name of cluster. Needs to be the same for all clusters, in order
to find each other -->
<attribute name="ClusterName">MPV-TreeCache-Cluster</attribute>
<attribute name="ClusterConfig">
<config>
<!-- UDP: if you have a multihomed machine,
set the bind_addr attribute to the appropriate NIC IP address
-->
<!-- UDP: On Windows machines, because of the media sense feature
being broken with multicast (even after disabling media sense)
set the loopback attribute to true
-->
<UDP mcast_addr="228.1.2.3" mcast_port="45566" ip_ttl="64" ip_mcast="true"
mcast_send_buf_size="150000" mcast_recv_buf_size="80000" ucast_send_buf_size="150000"
ucast_recv_buf_size="80000" loopback="false" />
<PING timeout="2000" num_initial_members="3" up_thread="false" down_thread="false" />
<MERGE2 min_interval="10000" max_interval="20000" />
<FD shun="true" up_thread="true" down_thread="true" />
<VERIFY_SUSPECT timeout="1500" up_thread="false" down_thread="false" />
<pbcast.NAKACK gc_lag="50" max_xmit_size="8192" retransmit_timeout="600,1200,2400,4800" up_thread="false"
down_thread="false" />
<UNICAST timeout="600,1200,2400" window_size="100" min_threshold="10" down_thread="false" />
<pbcast.STABLE desired_avg_gossip="20000" up_thread="false" down_thread="false" />
<FRAG frag_size="8192" down_thread="false" up_thread="false" />
<pbcast.GMS join_timeout="5000" join_retry_timeout="2000" shun="true" print_local_addr="true" />
<pbcast.STATE_TRANSFER up_thread="false" down_thread="false" />
</config>
</attribute>
<!-- The max amount of time (in milliseconds) we wait until the
initial state (ie. the contents of the cache) are retrieved from
existing members in a clustered environment
-->
<attribute name="InitialStateRetrievalTimeout">5000</attribute>
<!-- Number of milliseconds to wait until all responses for a
synchronous call have been received.
-->
<attribute name="SyncReplTimeout">10000</attribute>
<!-- Max number of milliseconds to wait for a lock acquisition -->
<attribute name="LockAcquisitionTimeout">15000</attribute>
<!-- Name of the eviction policy class. -->
<attribute name="EvictionPolicyClass">org.jboss.cache.eviction.LRUPolicy</attribute>
<!-- Specific eviction policy configurations. This is LRU -->
<attribute name="EvictionPolicyConfig">
<config>
<attribute name="wakeUpIntervalSeconds">5</attribute>
<!-- Cache wide default -->
<!-- Zero is no limit on maxNodes and timeToLiveSeconds -->
<region name="/_default_">
<attribute name="maxNodes">100000</attribute>
<attribute name="timeToLiveSeconds">0</attribute>
</region>
<region name="/com/cpex/mpv/data">
<attribute name="maxNodes">500</attribute>
<attribute name="timeToLiveSeconds">0</attribute>
</region>
<region name="/com/cpex/mpv/marketplace">
<attribute name="maxNodes">200000</attribute>
<attribute name="timeToLiveSeconds">0</attribute>
</region>
<region name="/com/cpex/mpv/participants">
<attribute name="maxNodes">10000</attribute>
<attribute name="timeToLiveSeconds">0</attribute>
</region>
<region name="/com/cpex/mpv/rules">
<attribute name="maxNodes">100000</attribute>
<attribute name="timeToLiveSeconds">0</attribute>
</region>
</config>
</attribute>
<!-- DON'T USE A CACHELOADER -->
<!-- <attribute name="CacheLoaderClass">org.jboss.cache.loader.bdbje.BdbjeCacheLoader</attribute> -->
<!--
<attribute name="CacheLoaderClass">org.jboss.cache.loader.FileCacheLoader</attribute>
<attribute name="CacheLoaderConfig">
location=/tmp
</attribute>
<attribute name="CacheLoaderShared">true</attribute>
-->
<!-- <attribute name="CacheLoaderPreload">/a/b/c,/all/my/objects</attribute> -->
<!--
<attribute name="CacheLoaderFetchTransientState">false</attribute>
<attribute name="CacheLoaderFetchPersistentState">true</attribute>
-->
</mbean>
</server>
Code between sessionFactory.openSession() and session.close():Code:
<%@ page import="org.hibernate.stat.Statistics,
com.cpex.mpv.persistence.HibernateUtil,
org.hibernate.stat.SecondLevelCacheStatistics,
java.util.Arrays"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
Statistics statistics = HibernateUtil.getStatistics();
String reset = request.getParameter("reset");
if ("true".equalsIgnoreCase(reset))
{
statistics.clear();
}
String[] regions = statistics.getSecondLevelCacheRegionNames();
Arrays.sort(regions);
int totalSize = 0;
int totalObjects = 0;
%>
<html>
<head><title>Cache info</title></head>
<body>
<h1>Second Level Cache Statistics</h1>
<table>
<thead>
<th>Region Name</th>
<th>Elements In Memory</th>
<th>Size In Memory (KB)</th>
<th>Hit Count</th>
<th>Miss Count</th>
<th>Put Count</th>
</thead>
<tbody>
<%
for (int row = 0; row < regions.length; row++)
{
String region = regions[row];
SecondLevelCacheStatistics regionStats =
statistics.getSecondLevelCacheStatistics(region);
totalSize += regionStats.getSizeInMemory();
totalObjects += regionStats.getElementCountInMemory();
%>
<tr>
<td><%= region %></td>
<td style="text-align:right" ><%= regionStats.getElementCountInMemory() %></td>
<td style="text-align:right" ><%= regionStats.getSizeInMemory() / 1024 %></td>
<td style="text-align:right" ><%= regionStats.getHitCount() %></td>
<td style="text-align:right" ><%= regionStats.getMissCount() %></td>
<td style="text-align:right" ><%= regionStats.getPutCount() %></td>
</tr>
<%
} // endloop (regions)
%>
</tbody>
<tfoot>
<th>Totals</th>
<th style="text-align:right" ><%= totalObjects %></th>
<th style="text-align:right" ><%= totalSize /1024 %>K</th>
<th style="text-align:right" ><%= statistics.getSecondLevelCacheHitCount() %></th>
<th style="text-align:right" ><%= statistics.getSecondLevelCacheMissCount() %></th>
<th style="text-align:right" ><%= statistics.getSecondLevelCachePutCount() %></th>
</tfoot>
</table>
<br/>
<br/>
<a href="<c:url value="/internal/cacheinfo.jsp"/>">Refresh</a>
<a href="<c:url value="/internal/cacheinfo.jsp">
<c:param name="reset" value="true"/>
</c:url>">Reset Counters</a>
<br/>
<br/>
<a href="<c:url value="/internal/meminfo.jsp"/>">Memory Info</a>
</body>
</html>
HibernateUtil.getStatistics():
Code:
public static Statistics getStatistics()
{
return sessionFactory.getStatistics();
}