• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

ODBC Connection goes slow....

I'm writing a server application that connects some clients to a database, for use in a till system, and I'm setting up the queries as shown in the code below, it's the first time I've used databases in Java before, and I'm quite certain the technique I'm using is wrong.

N.B. these methods get called A LOT!

The class's interface is the

processMessage(MessagePacket packet)

method, where MessagePacket is just a wrapper for 3 pieces of information, the message-ID, the command, and the data. When a new MessagePacket is constructed, it is passed an ID, and the composite command and data, i.e. (3,"getDrinkList:none"), and sets messageID=3, message="getDrinkList", data="none".

The problem is, after about 100 calls, the results suddenly start getting returned much more slowly on my Win NT system, it doesnt seem to happen on my Win 98 machine at home.

If anyone can, can you supply an example of the answer?

Program Code ------ | D. Cartwright
Program Title       | MessageHandler.Java
Program Description | The MessageHandler acts as a link between the server
                    | and the database. It can be sent messages, or direct
                    | SQL statements.
Version ----------- | 1
Creation Date ----- | 5/01

*/

//*****************************************************************************
//State the name of the Package this class is a part

package Server;

//*****************************************************************************
//Import things

import java.io.*;
import java.net.*;
import java.awt.*;
import com.borland.dx.sql.dataset.*;
import java.sql.*;
import java.util.*;

//*****************************************************************************
//Declare the MessageHandler class

public class MessageHandler{

//*****************************************************************************
//

  private MessagePacket MPS=new MessagePacket();

  private Database database = new Database();
  private QueryDataSet queryDataSet = new QueryDataSet();
  private Connection con;
  private ResultHandler results=new ResultHandler();
  private TextArea display;

//*****************************************************************************
//

  private void checkForWarnings(SQLWarning warn) throws Exception
    {
    if(warn!=null)
      {
      display.append("***** A WARNING OCCURED *****");
      }
    }

//*****************************************************************************
//

  public MessageHandler(TextArea display) throws Exception
    {
    this.display=display;
    display.append("Opening ODBC connection for database access...");
    String URL="jdbc:odbc:GPD.mdb";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//    DriverManager.setLogStream();
    con=DriverManager.getConnection(URL,"","");
    checkForWarnings(con.getWarnings());
    DatabaseMetaData dma=con.getMetaData();
    display.append("DONE\n");
    display.append("Connected to: "+dma.getURL()+"\n");
    display.append("Driver: "+dma.getDriverName()+"\n");
    display.append("Version: "+dma.getDriverVersion()+"\n\n");
    }

//*****************************************************************************
//

  public MessagePacket processMessage(MessagePacket packet) throws Exception
    {
    if(packet==null) return null;
    if(packet.message.equals("getMenuItem"))          return(MPS.newMessagePacket(packet.messageID,getMenuItem(packet.data)));
    if(packet.message.equals("getMenuList"))          return(MPS.newMessagePacket(packet.messageID,getMenuList()));
    if(packet.message.equals("getCurrentOrderList"))  return(MPS.newMessagePacket(packet.messageID,getCurrentOrderList()));
    if(packet.message.equals("getOrderDetails"))      return(MPS.newMessagePacket(packet.messageID,getOrderDetails(packet.data)));
    if(packet.message.equals("getDrinkList"))         return(MPS.newMessagePacket(packet.messageID,getDrinkList()));
    if(packet.message.equals("getMainMealList"))      return(MPS.newMessagePacket(packet.messageID,getMainMealList()));
    if(packet.message.equals("getSideOrderList"))     return(MPS.newMessagePacket(packet.messageID,getSideOrderList()));
    if(packet.message.equals("getDessertList"))       return(MPS.newMessagePacket(packet.messageID,getDessertList()));
    if(packet.message.equals("orderCompleted"))       {orderCompleted(packet.data); return(MPS.newMessagePacket(packet.messageID,"Order "+packet.data+" Completed"));}

    if(packet.message.equals("orderReady"))           {orderReady(packet.data); return(MPS.newMessagePacket(packet.messageID,"Order "+packet.data+" Ready"));}
    if(packet.message.equals("orderNotReady"))        {orderNotReady(packet.data); return(MPS.newMessagePacket(packet.messageID,"Order "+packet.data+" Not Ready"));}
    if(packet.message.equals("getOrderReady"))        return(MPS.newMessagePacket(packet.messageID,getOrderReady(packet.data)));

    if(packet.message.equals("DirectSQL-Update"))     {directSQLUpdate(packet.data); return(MPS.newMessagePacket(packet.messageID,"Internal Command OK"));}

    if(packet.message.equals("newOrder"))             {newOrder(packet.data); return(MPS.newMessagePacket(packet.messageID,"New Order Placed!"));}

    display.append("UNKNOWN COMMAND!!!\n....ABOVE COMMAND HAS NOT BEEN ");
    return(MPS.newMessagePacket(packet.messageID,"Unknown command!"));
    }

//*****************************************************************************
//

  public void newOrder(String rawOrderData) throws Exception
    {
    String waitressID,tableID;
    int thisOrderID=0;
    StringTokenizer data=new StringTokenizer(rawOrderData,",");
    tableID=data.nextToken();
    waitressID=data.nextToken();
    directSQLUpdate("INSERT INTO tblCurrentOrder ([Table ID],[Waitress ID]) VALUES ("+tableID+","+waitressID+")");
    ResultSet currentOrders=con.createStatement().executeQuery("SELECT [Order ID] FROM [tblCurrentOrder] WHERE NOT Completed=Yes");
    while(currentOrders.next())
      thisOrderID=Integer.parseInt(currentOrders.getString("Order ID"));
            while(data.hasMoreTokens())
      directSQLUpdate("INSERT INTO tblOrderDetails ([Order ID],[Item ID]) VALUES ("+thisOrderID+","+data.nextToken()+")");
    }

//*****************************************************************************
//

  public void directSQLUpdate(String SQLStatement) throws Exception
    {
    con.createStatement().executeUpdate
      (
      SQLStatement
      );
    }

//*****************************************************************************
//

  public String getOrderReady(String order) throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT Ready FROM [tblCurrentOrder] WHERE \"Order ID\" = "+order
          )
        )
      );
    }

//*****************************************************************************
//

  public void orderReady(String orderID) throws Exception
    {
    con.createStatement().executeUpdate
      (
      "UPDATE tblCurrentOrder SET Ready=Yes WHERE [Order ID]="+orderID
      );
    }

//*****************************************************************************
//

  public void orderNotReady(String orderID) throws Exception
    {
    con.createStatement().executeUpdate
      (
      "UPDATE tblCurrentOrder SET Ready=No WHERE [Order ID]="+orderID
      );
    }

//*****************************************************************************
//

  public void orderCompleted(String orderID) throws Exception
    {
    con.createStatement().executeUpdate
      (
      "UPDATE tblCurrentOrder SET Completed=Yes WHERE [Order ID]="+orderID
      );
    }

//*****************************************************************************
//

  public String getMenuItem(String item) throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblMenuItems] WHERE \"Item ID\" = "+item
          )
        )
      );
    }

//*****************************************************************************
//

  public String getMenuList() throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblMenuItems]"
          )
        )
      );
    }

//*****************************************************************************
//

  public String getSideOrderList() throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblMenuItems] WHERE [Item Type]='Extra' ORDER BY Name"
          )
        )
      );
    }

//*****************************************************************************
//

  public String getDessertList() throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblMenuItems] WHERE [Item Type]='Dessert' ORDER BY Name"
          )
        )
      );
    }

//*****************************************************************************
//

  public String getDrinkList() throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblMenuItems] WHERE [Item Type]='Drink' ORDER BY Name"
          )
        )
      );
    }

//*****************************************************************************
//

  public String getMainMealList() throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblMenuItems] WHERE [Item Type]='Main' ORDER BY Name"
          )
        )
      );
    }

//*****************************************************************************
//

  public String getCurrentOrderList() throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblCurrentOrder] WHERE \"Completed\"=False"
          )
        )
      );
    }

//*****************************************************************************
//

  public String getOrderDetails(String OrderID) throws Exception
    {
    return
      (
      results.newResultSet
        (
        con.createStatement().executeQuery
          (
          "SELECT * FROM [tblOrderDetails] WHERE \"Order ID\"="+OrderID
          )
        )
      );
    }
  }

CLASS ENDS!!!!!!!! :o)
0
Lab_Rat
Asked:
Lab_Rat
1 Solution
 
My name is MudCommented:
Is this the Q???
0
 
Lab_RatAuthor Commented:
YES!

The question is on line 10, posted here again, just in case your fingers getting tired of all the scrolling:

The problem is, after about 100 calls, the results suddenly start getting returned much more slowly
on my Win NT system, it doesnt seem to happen on my Win 98 machine at home.
0
 
dorothy2Commented:
If your program is slowing down after it has executed for a while, it could be a problem with garbage collection. You should explicitly close the statements and the result sets after you are finished with them.

In addition, if you are looking for performance, don't use the JDBC-ODBC bridge. All the database vendors have written drivers for their products, and you should use one created specifically for your database. They can be easily downloaded. Here is a reference to an article about the JDBC-ODBC bridge and there is a hot-link to the list of drivers available.


http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/bridge.doc.html

Finally, it's tedious, but you could benchmark which of your methods is slowing down by creating a log file and outputting the timestamps when the procedures begin and end. This would identify database problems. I notice you're doing some updates. You might have row lock problems, where several requests are trying to access the same row in the database, and the database's concurrency software only allows access to 1 row at a time.

Good luck,

Dorothy
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimYatesCommented:
I agree with dorothy...  You need "try...finally" blocks to make sure you close the resultSets and statements...
0
 
Lab_RatAuthor Commented:
Thanks for your valued info, I've included the ResultHandler class below, where the ResultSet object is passed, to show you how I'm closing the ResultSet off. As you can see in the newResultSet(ResultSet prs) method, I use the command:

    prs.close();//Ensure the query is closed after use!

to close the ResultSet.

Is this all I need to do?

I know these type of problems can be hard to pin down, and that sometimes answers are never found. So I'll accept a comment as an answer soon.

I'd like to add that I'm using the code:

System.out.println("Free Mem:"+Runtime.getRuntime().freeMemory());

To display the amount of free memory the system has, after each query of the database. Apparently it never drops below a few hundred thousand.

Anyway the 'results' object in the code above is:

/*

Program Code ------ | D. Cartwright
Program Title       | ResultHandler.Java
Program Description | This program takes a new database ResultSet and converts
                    | it into a predefined String
Version ----------- | 1
Creation Date ----- | 5/01

*/

//*****************************************************************************
//State the name of the Package this class is a part

package Server;

//*****************************************************************************
//Import things

import java.sql.*;

//*****************************************************************************
//Declare the ResultHandler class

public class ResultHandler{

//*****************************************************************************
//Declare the global data

  private ResultSetMetaData rsmd; // Somewhere to store the result set meta data
  private ResultSet rs; //  Somewhere to store the ResultSet
  private int numOfColumns,numOfRows; //  The ResultSet table dimensions
  private String result;  //  The ResultSet re-formatted String

//*****************************************************************************
//The main ResultHandler constructor

  public ResultHandler() {}

//*****************************************************************************
//Returns the number of results for the ResultSet query

  public int getNumberOfResults()
    {
    return numOfRows;
    }

//*****************************************************************************
//Returns the number of fields for the result query

  public int getNumberOfColumns()
    {
    return numOfColumns;
    }

//*****************************************************************************
//Returns the ResultSet column name, given its index

  public String getColumnName(int index)
    {
    try
      {
      return(rsmd.getColumnLabel(index));
      }
    catch(Exception e)
      {
      e.printStackTrace();
      return("");
      }
    }

//*****************************************************************************
//Returns the re-formatted ResultSet as a String

  public String getResultSet()
    {
    return(result);
    }

//*****************************************************************************
//Process a new ResultSet to accuire a standard String formatted to our own spec
//
//The String is formatted thus:
// "
// [Number of rows]    "\n"
// [Number of columns] "\n"
// [Field 1 title],[Field 2 title],[Field 3 title] etc... "\n"
// [Data 1]       ,[Data 2]       ,[Data 3] etc...        "\n"
// etc... "\n"
// "

  public String newResultSet(ResultSet prs) throws Exception
    {
    result="";
    rs=prs;
    rsmd=rs.getMetaData();
    numOfColumns=rsmd.getColumnCount();
    numOfRows=0;
//Store the field names, end with a "\n"
    for(int i=1;i<=numOfColumns;i++) result+=(i>1?",":"")+rsmd.getColumnLabel(i);
    result+="\n";
//Scan through all the results, storing in the String as we go
    boolean more=rs.next();
    while(more)
      {
      numOfRows++;
      for(int i=1;i<=numOfColumns;i++) result+=(i>1?",":"")+rs.getString(i);
      result+="\n";
      more=rs.next();
      }
    prs.close();//Ensure the query is closed after use!
//Return the finished String
    return(numOfRows+"\n"+numOfColumns+"\n"+result);
    }

  }

0
 
Lab_RatAuthor Commented:
I realise; if there is an Exception thrown the ResultSet will not be closed, but (can I get away with saying?) I am very sure the statements are never throwing Exceptions, therefore, the prs.close(); should be executed successfully.
0
 
TimYatesCommented:
you can put:

try
{
   result="";
   rs=prs;
   rsmd=rs.getMetaData();
   numOfColumns=rsmd.getColumnCount();
   numOfRows=0;
//Store the field names, end with a "\n"
   for(int i=1;i<=numOfColumns;i++) result+=(i>1?",":"")+rsmd.getColumnLabel(i);
   result+="\n";
//Scan through all the results, storing in the String as we go
   boolean more=rs.next();
   while(more)
     {
     numOfRows++;
     for(int i=1;i<=numOfColumns;i++) result+=(i>1?",":"")+rs.getString(i);
     result+="\n";
     more=rs.next();
     }
}
finally
{
   prs.close();//Ensure the query is closed after use!
}

which should close it even if an exception is thrown...

Tim
0
 
Lab_RatAuthor Commented:
Thanks, done and dusted!

I've also done something drastic and added a counter to the query engine, when it reaches a predetermined value, I use:

con.commit();
con.close();

and reconnect again.

It works!

But now, the Java.Exe program crashes intermitently, triggering Dr Watson...

At least I know MY part of the syatems working!

Points!
dorothy2 is especially deserving for the points, as you made the biggest contribution!

In fact, I'll change your comment to my accepted answer.

If anyone else feels left out, just let me know.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now