title: synchronize database queries java
body: <p>Hi I have a java program with multiple threads querying an HSQL database. I'm getting some very weird results from the queries and my guess is that the database is not set up to handle many queries at the same time. (Is this even possible in a database?).</p>
  
  <p>What I would like to do then is queue any database queries.</p>
  
  <p>As far as I can tell this is done using the synchronize keyword when making functions in my database object?</p>
  
  <p>My database object looks something like this:</p>
  
  <pre><code>public class Database(){
      public Database(){
          connect();
      }
  
      private void connect(){
          //connect to HSQL database
      }
  
      public void executeOneTypeOfQuery(){
          ...
          ResultSet rs1 = someStatement.executeQuery();
          //do something with a query
      }
  
      public void executeAnotherTypeOfQuery(){
          ...
          ResultSet rs2 = anotherStatement.executeQuery();
          //do something with a query
      }
  
  }
  </code></pre>
  
  <p>At the moment I may be calling any combination of executeOneTypeOfQuery() and executeAnotherTypeOfQuery() at the same time in different threads.</p>
  
  <p>Should those two functions have the synchronized keyword? Or will that only stop them calling JUST THAT method twice at the same time?</p>
  
  <p>Am I right in thinking another solution could be making use of callbacks (I don't really understand the syntax here)?
  So anywhere in my project I call one of those database functions, I call an intermediate synchronized function, stating which database function I really want to use.</p>
  
  <p>So threads call something like:</p>
  
  <pre><code>public synchronized void executeAnyQuery(Function theFunctionIWantToCall, Object[] args){
      //do theFunctionIWantToCall
  }
  </code></pre>
  
  <p>Is this the right way to do it? If so what is the syntax with callbacks?</p>