title: Why does my executorservice.submit returns null?
body: <p>I want to call several external APIs concurrently with multithreading in java spring, then I use it like this:</p>
  <pre><code>@Service
  public class ServiceImpl implements Service {
      public static final Logger logger = LoggerFactory.getLogger(&quot;ServiceImpl&quot;);
      
      @Autowired
      SM sm;
  
      @Transactional
      @Override
      public Map getReturnsSeq(Map params) throws Exception {
          Map result1 = sm.callAPI1(params);
          Map result2 = sm.callAPI2(params);
          Map result3 = sm.callAPI3(params);
          Map result4 = sm.callAPI4(params);
          Map returnMap = new HashMap&lt;&gt;();
          List comb1and2 = new ArrayList();
          comb1and2.addAll((List) result1.get(&quot;data&quot;));
          comb1and2.addAll((List) result2.get(&quot;list_data&quot;));
          Comparator&lt;Map&lt;String, String&gt;&gt; mapComparator = (o1, o2) -&gt; o2.get(&quot;date&quot;).compareTo(o1.get(&quot;date&quot;));
          comb1and2.sort(mapComparator);
          returnMap.put(&quot;cmb12&quot;, comb1and2);
          returnMap.put(&quot;api_3&quot;, result3);
          returnMap.put(&quot;api_4&quot;, result4);
          return returnMap
      }
      
      @Transactional
      @Override
      public Map getReturnsCon(Map params) throws Exception {
          ExecutorService e1 = Executors.newFixedThreadPool(1);
          ExecutorService e2 = Executors.newFixedThreadPool(1);
          ExecutorService e3 = Executors.newFixedThreadPool(1);
          ExecutorService e4 = Executors.newFixedThreadPool(1);
          Map returnMap = new HashMap&lt;&gt;();
          try {
              Future&lt;Map&gt; result1 = e1.submit(new RecommendationAndHistory(params, 1));
              Future&lt;Map&gt; result2 = e2.submit(new RecommendationAndHistory(params, 2));
              Future&lt;Map&gt; result3 = e3.submit(new RecommendationAndHistory(params, 3));
              Future&lt;Map&gt; result4 = e4.submit(new RecommendationAndHistory(params, 4));
              
              List comb1and2 = new ArrayList();
              comb1and2.addAll(result1.get().get(&quot;data&quot;));
              comb1and2.addAll(result2.get().get(&quot;list_data&quot;));
              Comparator&lt;Map&lt;String, String&gt;&gt; mapComparator = (o1, o2) -&gt; o2.get(&quot;date&quot;).compareTo(o1.get(&quot;date&quot;));
              comb1and2.sort(mapComparator);
              returnMap.put(&quot;cmb12&quot;, comb1and2);
              returnMap.put(&quot;api_3&quot;, result3.get());
              returnMap.put(&quot;api_4&quot;, result4.get());
          } catch (Exception e){
              logger.debug(e.getMessage());
          } finally {
              e1.shutdown();
              e2.shutdown();
              e3.shutdown();
              e4.shutdown();
          }
          return returnMap;
      }
  
      class RecommendationAndHistory implements Callable&lt;Map&gt; {
          private Map params;
          private int code;
          public RecommendationAndHistory(Map param, int cd) {
              this.params = param;
              this.code = cd;
          }
          
          @Override
          public Map call() throws Exception {
              if(code == 1) {
                  return sm.callAPI1(params);
              } else if(code == 2) {
                  return sm.callAPI2(params);
              } else if(code == 3) {
                  return sm.callAPI3(params);
              } else if(code == 4) {
                  return sm.callAPI4(params);
              }
              return null;
          } 
      }
  }
  </code></pre>
  <p>Here is example of the <code>callAPI1</code>:</p>
  <pre><code>    @Override
      public function callAPI1(Map params) {
          ExternalAPI e = externalAPIDao.getExternalAPI(&quot;API_1&quot;);
          String url = e.getUrl() + &quot;/get_history&quot;;
          RestMiddlewareCommunicator rest = new RestMiddlewareCommunicator();
          rest.setInputStream(true);
          rest.setOutputStream(true);
  
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
          RestTemplate restTemplate = this.get_resttemplate();
          HttpEntity entity = new HttpEntity(headers);
          ResponseEntity&lt;Map&gt; response = restTemplate.exchange(url, HttpMethod.GET, entity, Map.class);
          return response.getBody();
      }
  </code></pre>
  <p>The <code>externalAPIDao.getExternalAPI(String name)</code> contains:</p>
  <pre><code>Session session = this.sessionFactory.getCurrentSession();
  ExternalAPI e = session.createQuery(&quot;from EAPI where name = :name&quot;).setParameter(&quot;name&quot;, name).uniqueResult();
  return e;
  </code></pre>
  <p>I can run the <code>getReturnsSeq</code> normally everytime, but takes very very long time. However, I got null everytime I run <code>getReturnsCon</code>. So, what is actually wrong from my code above and how to resolve it?</p>
  <h3>Edit</h3>
  <hr />
  <p>The results of the executors were null because of the <code>HibernateException</code> that can't get the <code>currentSession</code> for the currently running thread. Therefore I come back to the sequential process although it takes a long time, since currently I can't find the way out of this.</p>
  <p>I am still wondering about how can I overcome this long execution time with multithreading without getting such exception from Hibernate. Any help is greatly appreciated!</p>