Hibernate-Cache导致页面上的org.hibernate.service.UnknownServiceException刷新

时间:2015-10-21 16:28:17

标签: jsp tomcat8 hibernate-5.x

我正在使用Hibernate 5.0.2.Final,并希望在缓慢的数据库中使用缓存。第一次它通常工作,但当我刷新页面时,我得到错误。根据配置的不同,我也会遇到几个错误或根本没有错误,或者只是简单加载而根本没有任何结果。

配置是根据Github上的教程或Code使用Hibernate 5。我感觉问题可能是新的Hibernate版本,但也许我做了别的错误。

如果我获得了一个Stacktrace并且第一个加载过程有效,那么它就失败了:

  

2015年10月21日17:19:22.585严重[http-nio-8084-exec-78]   org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()   对于带有路径[/ Buran]的上下文中的servlet [jsp]抛出异常[An   处理JSP页面时发生异常   第26行/filter/Arbeitspreise/Arbeitspreisliste.jsp

     

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

     

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

我正在使用以下hibernate.cfg.xml(我必须为德语名称道歉)。正如您所看到的,我使用的是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>

有一件事困扰我关于ehcache.xml。像这样使用它只会导致我遇到的香草错误:

<?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>

但根据我能找到的信息,我应该将它与这些条目一起使用,对吗?

<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>

但这样做会使所有其他 Hibernate-Commands无法运行......

课程工作得很好,所以我只是添加第一个的标题,它们看起来几乎相同......

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 {

我的Java-Bean getFullArbeitspreisGruppenverwaltungloaded只加载所有记录,将它们转换为所需的字符串并关闭会话......

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");
        }
    }

有时它也会发生任何事情都没有起作用,而Hibernate只是加载和加载,直到连接暂停。 我的库的版本与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>

0 个答案:

没有答案