'ExecutorService submit failing on Future get

I have a query that I am trying to run concurrently as many times as I want but for now, I am running with 5 times. I always get an error when I call Future.get() method to read the ResultSet. The same code works when I have only one query running.

ExecutorService service = Executors.newCachedThreadPool();


List<Task> tasks = new Arraylist<>();
 for(int i =0; i<5;i++){
    tasks.add(new Task("Select * from table", HiveConnectionObject));
 }
 
 
 List<Future<ResultSet>> futureList = new ArrayList<>();
 for(Task t: tasks){
    futureList.add(service.submit(t));
 }
 
 for(int i = 0; i<tasks.size();i++){
    ResultSet rs = null;
    try{
        rs = futureList.get(i).get();
    }catch(Exceptions e) {
    e.printStrackTrace();
    }
 }
 

My Task class is as follows

 public class Task implements Callable<ResultSet> {

    private String sql;
    private Connection conn;

    public Task(String sql, Connection conn) {
        this.sql = sql;
        this.conn = conn;
    }

    @Override
    public ResultSet call() throws Exception {
        Statement stmt = connection.createStatement();
        return stmt.executeQuery(this.sql);
    }
}

I am getting Exceptions for all 5 queries in this case as follows but submitted queries were successful on HIVE side.

java.sql.SQLException: Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.tez.TezTask at

Surprisingly I am able to get back the resultSet for a single Task instead of 5 with the same code. I feel I am doing some mistakes in running concurrent queries and Future.get().

Any help appreciated?



Solution 1:[1]

The solution was to create a new Connection for every task. Earlier i was using the same connection Object for all the tasks.

    @Override
    public ResultSet call() throws Exception {
        Connection conn = getNewConnection(drivername, url);
        Statement stmt = connection.createStatement();
        return stmt.executeQuery(this.sql);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rakeeee