Class 未找到:无法加载 com.microsoft.sqlserver.jdbc.Nanos

Class Not Found: Could not load com.microsoft.sqlserver.jdbc.Nanos

我遇到了一个非常奇怪的问题。我的应用程序运行得相当好,我正在优化它(我从其他开发人员那里继承了它)。

该项目使用带 MS SQL 服务器的 Hibernate 4,并使用 Primefaces 在 Tomcat 8 上运行。我最近回去开始使用延迟加载(以前的开发人员将所有延迟加载设置为 false)和 Primefaces 的 LazyDateModel 并且在恢复工作状态时遇到了一些问题。其中大部分我都可以自己修复,但这个让我很困惑。

    <query name="assayByLineAndDateRange">
            <![CDATA[from Assay a where a.line.number = :line and a.time >= :startDate 
                          and a.time < :endDate order by a.time DESC]]>
        </query>

上面的 HQL 查询将导致 com.microsoft.sqlserver.jdbc.Nanos 的最终 ClassNotFoundException,我已经验证它存在于我的项目 classpath.

现在奇怪的部分是:当我删除查询的 and 条件时,应用程序运行正常,如下所示:

<query name="assayByLineAndDateRange">
            <![CDATA[from Assay a where a.line.number = :line order by a.time DESC]]>
        </query>

当我在我的 util class 中调用 query.iterate() 时出现了确切的问题,我不明白为什么。这是相关代码的片段:

protected List<T> getList(Query query) {
    List<T> list = new ArrayList<T>();
    System.out.println("Query name: " + query.getQueryString());
    try {
        @SuppressWarnings("rawtypes")
        Iterator i = query.iterate();
        System.out.println("This line does not print");
        while(i.hasNext()) {
            list.add(getNext(i));
        }
        return list;
    } catch(Exception e) {
        System.out.println("CAUSE: " + e.getCause().toString());
        e.printStackTrace();
        return null;
    }

请注意,此代码几乎适用于所有其他查询,包括使用 and 运算符的查询。

这是来自 Assay 的映射文件的相关部分:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.app.gen.model.data.Assay" table="ASSAY" schema="GEN">
        <id name="id" type="java.lang.Long" unsaved-value="0">
            <column name="ID" precision="10" scale="0"/>
            <generator class="identity"/>
        </id>
        <property name="time" type="timestamp">
            <column name="TIME" not-null="true" unique="true"/>
        </property>
        <many-to-one name="line" class="com.app.gen.model.data.Line" cascade="none">
            <column name="LINE_ID" not-null="true"/>
        </many-to-one>
     </class>
</hibernate-mapping>

此外,我已经测试了参数以确保它们被正确传递并且它们是正确的。最重要的是,在我实现延迟加载之前,这个查询运行良好。这是用于延迟加载到 Primefaces 数据表中的重载 load 方法:

@Override
public List<Assay> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
    try { 
        if(null==lineAssays)
            lineAssays = new HashMap<Integer,List<Assay>>();
        if(null == lineAssays.get(getLineNumber())) {
            if(log.isDebugEnabled()) { log.debug("Fetching Line Assays..."); }
            List<Assay> assays = GenServiceFactory.create().fetchAssaysByLineAndDateRange(getLineNumber(), getStartDate(), getEndDate());
            lineAssays.put(getLineNumber(),assays);
        }
    } catch(Exception e) {
        System.out.println("AN ERROR OCCURED!");
        e.printStackTrace();
    }
    try {
        int numAssays = lineAssays.get(getLineNumber()).size();
        this.setRowCount(numAssays); 

        if(numAssays > pageSize) {
            try {
                return lineAssays.get(line).subList(first, first + pageSize);
            }
            catch(IndexOutOfBoundsException e) {
                return lineAssays.get(line).subList(first, first + (numAssays % pageSize));
            }
        }
        else {
            return lineAssays.get(line);
        }
    } catch(NullPointerException e1) {
        System.out.println("lineAssays is null");
    }
    return null;
}

这是异常的堆栈跟踪:

DEBUG (2015-02-03 14:57:50) [http-apr-8080-exec-34] (LazyAssayLoader.java:72) - Fetching Line Assays...
Query name: 
        from Assay a where a.line.number = :line and a.time >= :startDate and a.time < :endDate order by a.time DESC

CAUSE: java.lang.ClassNotFoundException
java.lang.IllegalStateException: Can't overwrite cause with java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already.  Could not load com.microsoft.sqlserver.jdbc.Nanos.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
    at java.lang.Throwable.initCause(Throwable.java:457)
    at org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1331)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1212)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1173)
    at com.microsoft.sqlserver.jdbc.TDSWriter.writeScaledTemporal(IOBuffer.java:4278)
    at com.microsoft.sqlserver.jdbc.TDSWriter.writeRPCDateTime2(IOBuffer.java:4184)
    at com.microsoft.sqlserver.jdbc.DTV$SendByRPCOp.sendTemporal(dtv.java:584)
    at com.microsoft.sqlserver.jdbc.DTV$SendByRPCOp.execute(dtv.java:356)
    at com.microsoft.sqlserver.jdbc.DTV.executeOp(dtv.java:1025)
    at com.microsoft.sqlserver.jdbc.DTV.sendByRPC(dtv.java:1116)
    at com.microsoft.sqlserver.jdbc.Parameter.sendByRPC(Parameter.java:660)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.sendParamsByRPC(SQLServerPreparedStatement.java:473)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doPrepExec(SQLServerPreparedStatement.java:628)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:400)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1716)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:285)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2040)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1837)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1816)
    at org.hibernate.loader.hql.QueryLoader.iterate(QueryLoader.java:518)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.iterate(QueryTranslatorImpl.java:399)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performIterate(HQLQueryPlan.java:245)
    at org.hibernate.internal.SessionImpl.iterate(SessionImpl.java:1331)
    at org.hibernate.internal.QueryImpl.iterate(QueryImpl.java:68)
    at com.core.service.HibernateService$AbstractHQLQuery.getList(HibernateService.java:120)
    at com.core.service.HibernateService$AbstractHQLQuery.getList(HibernateService.java:138)
    at com.urenco.iphex.service.IphexService.fetchAssaysByLineAndDateRange(IphexService.java:77)
    at com.urenco.iphex.view.lazy.LazyAssayLoader.load(LazyAssayLoader.java:73)
    at org.primefaces.component.datatable.DataTable.loadLazyData(DataTable.java:731)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:79)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:63)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47)
    at org.primefaces.component.panel.PanelRenderer.encodeContent(PanelRenderer.java:202)
    at org.primefaces.component.panel.PanelRenderer.encodeMarkup(PanelRenderer.java:119)
    at org.primefaces.component.panel.PanelRenderer.encodeEnd(PanelRenderer.java:56)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at org.primefaces.component.tabview.TabViewRenderer.encodeTabContent(TabViewRenderer.java:274)
    at org.primefaces.component.tabview.TabViewRenderer.encodeContents(TabViewRenderer.java:253)
    at org.primefaces.component.tabview.TabViewRenderer.encodeMarkup(TabViewRenderer.java:127)
    at org.primefaces.component.tabview.TabViewRenderer.encodeEnd(TabViewRenderer.java:71)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at com.sun.faces.renderkit.html_basic.CompositeRenderer.encodeChildren(CompositeRenderer.java:78)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:518)
    at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1612)
    at javax.faces.component.UIForm.visitTree(UIForm.java:371)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1623)
    at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:297)
    at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
    at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:981)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:409)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    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.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:616)
lineAssays is null
    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:534)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
    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)
Caused by: java.lang.ClassNotFoundException
    at org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading(WebappClassLoader.java:1330)
    ... 103 more

对不起,文字墙太长了,但非常感谢您的帮助!

我认为 ClassNotFoundException 不是真正的问题。如果您查看 here(第 1328 行)

的源代码
org.apache.catalina.loader.WebappClassLoader.checkStateForClassLoading

它在实际尝试加载 class 时并没有失败,它在加载 class 之前验证 Web 应用程序的当前状态时失败了,所以这个异常有点误导。

真正的问题似乎是您的应用程序在加载数据时处于无效状态。查找有关该问题的更多信息是指 Tomcat 缓存旧版本应用程序代码的问题。不确定是否属于这种情况,或者您是否尝试过从干净状态重新启动服务器。至少您可以确定 class 加载并不是这里的真正问题。