Solved

ODBC Connection goes slow....

Posted on 2001-06-12
8
200 Views
Last Modified: 2010-05-18
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
Comment
Question by:Lab_Rat
8 Comments
 
LVL 6

Expert Comment

by:My name is Mud
ID: 6180660
Is this the Q???
0
 
LVL 1

Author Comment

by:Lab_Rat
ID: 6181172
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
 
LVL 6

Accepted Solution

by:
dorothy2 earned 100 total points
ID: 6181248
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
 
LVL 35

Expert Comment

by:TimYates
ID: 6181441
I agree with dorothy...  You need "try...finally" blocks to make sure you close the resultSets and statements...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:Lab_Rat
ID: 6182179
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
 
LVL 1

Author Comment

by:Lab_Rat
ID: 6182218
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
 
LVL 35

Expert Comment

by:TimYates
ID: 6182269
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
 
LVL 1

Author Comment

by:Lab_Rat
ID: 6185765
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now