1

I'm using Hibernate 5.0.2.Final and want to use caching for a table in a slow Database. The first time it works usually but as I refresh the page I'm getting errors. Depending on the configuration I also encounter either several or no error at all or just plain loading without any result at all.

The configuration was according to tutorials or Code on Github which used Hibernate 5 as well. I'm getting the feeling the problem might be the new Hibernate version, but maybe I did something else wrong.

If I'm getting a Stacktrace and the first process of loading worked, it fails with this here:

21-Oct-2015 17:19:22.585 SEVERE [http-nio-8084-exec-78] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/Buran] threw exception [An exception occurred processing JSP page /filter/Arbeitspreise/Arbeitspreisliste.jsp at line 26

23: 24: 25: 26: 27:
28: 29:
$(document).ready(function() {

Stacktrace:] with root cause org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.cache.spi.RegionFactory] at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:184) at org.hibernate.cfg.Settings.getRegionFactory(Settings.java:300) at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1322) at org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:677) at arbeitspreisliste.Arbeitspreislistenhibernate.getFullArbeitspreisGruppenverwaltungloaded(Arbeitspreislistenhibernate.java:68) at org.apache.jsp.filter.Arbeitspreise.Arbeitspreisliste_jsp._jspService(Arbeitspreisliste_jsp.java:109) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at filter.SessionCheckFilter.doFilter(SessionCheckFilter.java:114) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

I'm using the following hibernate.cfg.xml (I have to apologize for the German names). As you can see I'm using the Data-Source provided by the Tomcat 8-Server:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.datasource">java:comp/env/jdbc/sqlserv</property>        
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <property name="hbm2ddl.auto">auto</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</property>
    <property name="hibernate.generate_statistics">true</property>  

    <!--Query Cache: unused>
     <property name="hibernate.cache.use_query_cache">true</property-->
    <!-- Cache -->
    <property name="hibernate.cache.use_second_level_cache"> true</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>

    <!--property name="hibernate.cache.use_query_cache">false</property-->

    <!-- General Hibernate settings. -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="use_sql_comments">true</property>
    <mapping class="arbeitspreisliste.Arbeitspreise"/>
    <mapping class="arbeitspreisliste.Arbeitspreisliste"/>
    <mapping class="arbeitspreisliste.Arbeitspreisgruppe"/>
  </session-factory>
</hibernate-configuration>

There is one thing that bothers me about the ehcache.xml. Using it like this just causes the vanilla error I'm encountering:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir/ehcache" />

<defaultCache maxEntriesLocalHeap="1000" eternal="false" timeToIdleSeconds="60" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="1000"
    diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true">
    <persistence strategy="localTempSwap" />
</defaultCache>

<cache name="org.hibernate.cache.internal.StandardQueryCache" maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">
    <persistence strategy="localTempSwap" />
</cache>

<cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxEntriesLocalHeap="1000" eternal="true">
    <persistence strategy="localTempSwap" />
</cache>

</ehcache>

But according to the information I could find I should use it with those entries, right?

<cache name="arbeitspreisliste.Arbeitspreise" maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="10">
    <persistence strategy="localTempSwap" />
</cache>

<cache name="arbeitspreisliste.Arbeitspreisgruppe" maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="10">
    <persistence strategy="localTempSwap" />
</cache>

<cache name="arbeitspreisliste.Arbeitspreisliste" maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="10">
    <persistence strategy="localTempSwap" />
</cache>

But doing so makes all other Hibernate-Commands impossible to run...

The Classes work just fine, so I'm just adding the headers of the first one, they look practically the same...

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name = "[Buran].[dbo].[Arbeitspreisliste]")
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Arbeitspreisliste implements Serializable {

My Java-Bean getFullArbeitspreisGruppenverwaltungloaded just loads all records, converts them into the desired String and closes the session...

private final String hql = "SELECT G FROM arbeitspreisliste.Arbeitspreisgruppe G order by G.agrupID";

public String getFullArbeitspreisGruppenverwaltungloaded() {
    boolean hasError = false;

    Session session = null;
    SessionFactory factory = null;

    try {
        factory = HibernateUtil.getSessionFactory();
        session = factory.openSession();
        session.setCacheMode(CacheMode.NORMAL);
        session.beginTransaction();
        Query query = session.createQuery(hql);
        //query.setCacheable(true); <-- This doesn't really do much..

        List<Arbeitspreisgruppe> apgruppenmaterial = query.list();

        //Do something useful

    } catch (NullPointerException | HibernateException npe) {
            hasError = true;
            Logger.getLogger(Arbeitspreislistenhibernate.class.getName()).log(Level.INFO, null, hql);
            Logger.getLogger(Arbeitspreislistenhibernate.class.getName()).log(Level.SEVERE, null, npe);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (HibernateException hibendex) {
                Logger.getLogger(Arbeitspreislistenhibernate.class.getName()).log(Level.INFO, null, hibendex);
                Logger.getLogger(Arbeitspreislistenhibernate.class.getName()).log(Level.FINEST, null, "Couldn't close session...");
            }
        } else {
            Logger.getLogger(Arbeitspreislistenhibernate.class.getName()).log(Level.FINE, null, "Session=null");
        }
    }

Sometimes it's also happening that nothing at all is working and Hibernate just loads and loads until the connection is suspended. The versions of my libraries are as they're in the pom.xml:

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <netbeans.hint.deploy.server>Tomcat</netbeans.hint.deploy.server>
    <!-- Hibernate EHCache API -->
    <hibernate-ehcache.version>5.0.2.Final</hibernate-ehcache.version>

    <!-- Ehcache -->
    <ehcache-core.version>2.6.11</ehcache-core.version>

    <hibernate-validator.version>5.2.2.Final</hibernate-validator.version>

    <hibernate-core.version>5.0.2.Final</hibernate-core.version>

    <hibernate-entitymanager.version>5.0.2.Final</hibernate-entitymanager.version>

</properties>
    <!-- HIBERNATE -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>${hibernate-ehcache.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>${ehcache-core.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-core.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate-entitymanager.version}</version>
        <scope>compile</scope>
    </dependency>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Qohelet
  • 1,459
  • 4
  • 24
  • 41
  • I had a similar problem, because I closed the `SessionFactory` and then I tried to do another query, but it doesn't seem to be the same situation. Can you add `HibernateUtil` content to your post? You can also use `HibernateUtil.getSessionFactory().openSession()` directly ... – ROMANIA_engineer Dec 28 '15 at 14:57
  • This was quite a while ago, meanwhile a few things are updated: http://stackoverflow.com/a/33230317/2516892 – Qohelet Dec 28 '15 at 18:22

0 Answers0