title: Query cannot get result when firing multiple threads to get the data
body: <p>I have a spring boot application where i am firing 5 DB queries on a postgres table using callable of java concurrent package.</p>
  
  <p>The Table 'User' that i am using is very small and has only 6 entries but still the application is taking more than 40 seconds to display the data and gives an exception after 40 seconds.</p>
  
  <p><strong>DataServiceImpl.java</strong></p>
  
  <pre><code>@Service
  public class DataServiceImpl {
      /*@Autowired
      private EntityManagerFactory emf;*/
  
      @Autowired
      private EntityManager entityManager;
  
      public List&lt;Object&gt; showUser(int firstParam, int secondParam) {
          //EntityManager entityManager = emf.createEntityManager();
          String sql = "select * from sunny.user where id between ?1 and ?2";
          Query query = entityManager.createNativeQuery(sql);
          query.setParameter(1, firstParam);
          query.setParameter(2, secondParam);
          return query.getResultList();
      }
  }
  </code></pre>
  
  <p><strong>UserService.java</strong></p>
  
  <pre><code>@Service
  public class UserService {
  
      @Autowired
      private DataServiceImpl dataServiceImpl;
  
      public List&lt;Object&gt; callUser() throws InterruptedException, ExecutionException {
          int startParam = 1;
          int endParam = 6;
          List&lt;Future&lt;List&lt;Object&gt;&gt;&gt; futures = new ArrayList&lt;&gt;();
          ExecutorService executorService = Executors.newCachedThreadPool();
          List&lt;Object&gt; b = null;
          for (int i = startParam; i &lt; 6; i++) {
              if (i == 1)
                  endParam = startParam + 1;
              else {
                  startParam += 1;
                  endParam += 1;
              }
              Callable&lt;List&lt;Object&gt;&gt; callable = new ProcessingData(startParam, endParam);
              futures.add(executorService.submit(callable));
          }
          executorService.awaitTermination(40, TimeUnit.SECONDS);
          int i = 0;
          for (Future&lt;List&lt;Object&gt;&gt; f : futures) {
              if (i == 0) {
                  b = new ArrayList&lt;&gt;(f.get());
              } else {
                  b.add(f.get());
              }
              i++;
          }
          return b;
      }
  
      private class ProcessingData implements Callable&lt;List&lt;Object&gt;&gt; {
          private int firstParam;
          private int secondParam;
          /*
           * @Autowired private DataServiceImpl dataServiceImpl;
           */
  
          public ProcessingData(int firstParam, int secondParam) {
              super();
              this.firstParam = firstParam;
              this.secondParam = secondParam;
          }
  
          @Override
          public List&lt;Object&gt; call() throws Exception {
              return dataServiceImpl.showUser(firstParam, secondParam);
         }
      }
  }
  </code></pre>
  
  <p>You can find the entire code here - <a href="https://github.com/iftekharkhan09/TemporaryRepo" rel="nofollow noreferrer">Git Code Here</a></p>
  
  <p>Can anyone please tell me where am i doing wrong?
  Exception After 40 seconds:-</p>
  
  <pre><code>1  Iftekhar
  2  Sunny
  2018-05-27 13:29:19.096 ERROR 9836 --- [nio-8085-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Ljava.lang.Object;] with root cause
  
  java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Ljava.lang.Object;
      at com.example.controller.UserController.showUser(UserController.java:24) ~[classes/:na]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
      at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.6.RELEASE.jar:5.0.6.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  </code></pre>
  
  <p>As you can see that i am only getting data of first 2 rows and exception after that as the other queries are not able to complete within 40 seconds they throw an exception as the value returned by them is null.</p>
  
  <p><strong>Queries Fired :-</strong></p>
  
  <pre><code>Hibernate: select * from sunny.user where id between ? and ?
  Hibernate: select * from sunny.user where id between ? and ?
  Hibernate: select * from sunny.user where id between ? and ?
  Hibernate: select * from sunny.user where id between ? and ?
  Hibernate: select * from sunny.user where id between ? and ?
  </code></pre>