title: Java and Database significant slowness
body: <p>I have a java program that creates thread for each field being read from the DB. I have a lot of fields (+10,000). I had problems in significant slowness. In addition to that, the program enters an infinite loop at the end. i.e, there is one or more threads that never ends, which make the loop infinite. I tried to reduce the number of fields that the program reads from inside the Java code, by specifying the number (PK) of the item to read like in the following statement:</p>
  
  <pre><code>"select hName from schema.table where 
  ID between 1 AND 200";
  </code></pre>
  
  <p>The program works fine after this change. I need to know what should I do to solve such issue. Does statements that contains conditions like, (where) affects the java program performance? If I didn't specify an order or a condition, but I have primary key in my table, does making the statement without any condition a better option ? </p>
  
  <p>There are some other insert statements in other classes. I do synchronize the statements there to avoid having two threads with the same record number.</p>
  
  <pre><code>synchronized (this) {
              Query = " insert into schema.table values (default,?,?,?)";
              Stmt = DBConnection.con.Statement(Query);
          } 
  </code></pre>
  
  <p>The concerned piece of code in the main function is :</p>
  
  <pre><code>try {
      ExecutorService threadExecutor = Executors.newFixedThreadPool(10);
  
      while (resultSet.next()) {
          name = resultSet.getString("hName");
          MyRunnable worker = new Myrunnable(name);
          threadExecutor.execute(worker);
          Counter++;
      }
  
      //This never appears
      System.out.println("End while with counter" + Counter);
  
      threadExecutor.shutdown();
      System.out.println("thread shutdown"); //this never appears
  
      // Wait until all threads are finish
      while (!threadExecutor.isTerminated()) {
          threadExecutor.awaitTermination(1, TimeUnit.SECONDS);
          System.out.println("inside the thread termination loop."); //I have infinite loop
  
      }
  
      System.out.println("Finished all threads"); //never appears
  
  } catch (Exception e) {
      e.printStackTrace();
  }
  
  System.out.println("END MAIN");
  
  DBConnection.con.close();
  </code></pre>
  
  <p>The class that implements runnable is here:</p>
  
  <pre><code>//The constructor
  String threadName=null;
  MyRunnable (String name)  {
      threadName=name;
  }
  
  public void run() {
      myclass Obj=new myclass();
      try {
          Obj.myFunction(name);
      } catch (Exception e) {
          System.out.println("Got an Exception: "+e.getMessage());
      }
      System.out.println(" thread exiting" + threadName);
  }
  </code></pre>