title: Class Not Found: Could not load com.microsoft.sqlserver.jdbc.Nanos
body: <p>I'e got what seems to be a very strange issue. My application runs fairly well and I am in the process of optimizing it (I inherited it from other developers). </p>
  
  <p>The project uses Hibernate 4 with MS SQL Server and runs on Tomcat 8 using Primefaces. I've recently gone back and began use lazy loading (the previous developers set all lazy loading to false) with the LazyDateModel from Primefaces and have had some issues getting back to a working state. Most of them I can fix on my own but this one has me stumped.</p>
  
  <pre><code>    &lt;query name="assayByLineAndDateRange"&gt;
              &lt;![CDATA[from Assay a where a.line.number = :line and a.time &gt;= :startDate 
                            and a.time &lt; :endDate order by a.time DESC]]&gt;
          &lt;/query&gt;
  </code></pre>
  
  <p>The above HQL query will cause an eventual <code>ClassNotFoundException</code> for <code>com.microsoft.sqlserver.jdbc.Nanos</code>, which I have already verified to be present in my project classpath.</p>
  
  <p>Now for the strange part: when I remove the <code>and</code> conditions of the query, the application runs fine, like so:     </p>
  
  <pre><code>&lt;query name="assayByLineAndDateRange"&gt;
              &lt;![CDATA[from Assay a where a.line.number = :line order by a.time DESC]]&gt;
          &lt;/query&gt;
  </code></pre>
  
  <p>The exact issue occurs when I call query.iterate() in my util class and I do not understand why. Here is the snippet of the code in question:</p>
  
  <pre><code>protected List&lt;T&gt; getList(Query query) {
      List&lt;T&gt; list = new ArrayList&lt;T&gt;();
      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;
      }
  </code></pre>
  
  <p>Note that this code works perfectly fine for literally every other query, including queries that use the <code>and</code> operator.</p>
  
  <p>Here is the relevant portions of the mapping file from Assay:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="com.app.gen.model.data.Assay" table="ASSAY" schema="GEN"&gt;
          &lt;id name="id" type="java.lang.Long" unsaved-value="0"&gt;
              &lt;column name="ID" precision="10" scale="0"/&gt;
              &lt;generator class="identity"/&gt;
          &lt;/id&gt;
          &lt;property name="time" type="timestamp"&gt;
              &lt;column name="TIME" not-null="true" unique="true"/&gt;
          &lt;/property&gt;
          &lt;many-to-one name="line" class="com.app.gen.model.data.Line" cascade="none"&gt;
              &lt;column name="LINE_ID" not-null="true"/&gt;
          &lt;/many-to-one&gt;
       &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Additionally, I have tested the parameters to ensure that they are being passed correctly and they are. On top of that, this query worked fine before I implemented lazy loading. Here is the overloaded <code>load</code> method for lazy loading into a Primefaces DataTable:</p>
  
  <pre><code>@Override
  public List&lt;Assay&gt; load(int first, int pageSize, String sortField, SortOrder sortOrder, Map&lt;String,String&gt; filters) {
      try { 
          if(null==lineAssays)
              lineAssays = new HashMap&lt;Integer,List&lt;Assay&gt;&gt;();
          if(null == lineAssays.get(getLineNumber())) {
              if(log.isDebugEnabled()) { log.debug("Fetching Line Assays..."); }
              List&lt;Assay&gt; 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 &gt; 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;
  }
  </code></pre>
  
  <p>Here is the stack trace from the exception:</p>
  
  <pre><code>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 &gt;= :startDate and a.time &lt; :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
  </code></pre>
  
  <p>Sorry for the wall of text but any help is greatly appreciated!</p>