0

I am trying to execute a query with a jdbcTemplate using an executor object but for some reason the program doesn't go inside the jdbcTemplate.

        ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_CONCURRENT_THREADS);
        executor.execute(new Runnable() {
        @Override
        public void run() {
            inboundJdbcTemplate.query(selectQuery, new RowCallbackHandler() {
                @Override
                public void processRow(ResultSet rs) throws SQLException {//<-instruction pointer never goes to this line
                    try {
                        //buffer.put(buildDataPoint(rs, testPermutationId));
                        System.out.println(rs.getString(0));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Thread.currentThread().interrupt();
                    }
                }
            });
            try {
                buffer.put(STOPPING_TOKEN);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

Can anyone help me with this stupid bug?

locorecto
  • 1,178
  • 3
  • 13
  • 40
  • 1
    I suspect that your query does not return results matching SQL criteria – hoaz May 10 '13 at 20:19
  • It does, if I remove the executor part (only the jdbctemplate part) it work like a charm. I don't think that is the problem. – locorecto May 13 '13 at 01:31
  • Then I suspect your program ends before executor runs your code in new Thread – hoaz May 13 '13 at 02:18
  • Looking at the debugger actually the program goes into the executor until it runs the inboundJdbcTemplate and then the next line it executes is outside the executor code. – locorecto May 13 '13 at 02:40

1 Answers1

0

I found a solution to the problem.

I needed a CompletionService in order to make sure that I know when the execution of the JdbcTemplate finishes.

{...
 ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_CONCURRENT_THREADS);
 CompletionService<String> completionService = new ExecutorCompletionService (executor);

 completionService.submit(new Runnable() {
    @Override
    public void run() {
        inboundJdbcTemplate.query(selectQuery, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                try {
                    buffer.put(buildDP(rs, Id));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
 }, "Success");

 try{
      Future<String> take1 = completionService.take();
      String s = take1.get();
      if(!"Success".equals(s)) throw new RuntimeException("Error Occured");
 catch (InterruptedException | ExecutionException e) {
        LOG.error(" Could not execute DataExtraction",e);}
 executor.shutdown();
...}
locorecto
  • 1,178
  • 3
  • 13
  • 40