Results 1 to 8 of 8
  1. #1
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default Mutiple Connections problem

    Hi I am having a problem with multiple connections opened though I have closed them using cn.close() method.
    Actually, I have a frame contains multiple instances of panels containing the records on JLabel. I have setText() on labels from MySQL.
    The query goes like this:

    Java Code:
    public void fillDetails(int ID) {
            Connection cn = null;
            Statement stmt = null;
            ResultSet rss = null;
            try {
                cn = connectMySql.conn();
                String sqltempCDR = "select amount from table1 where id=" + ID + "";
                stmt = cn.createStatement();
                rss = stmt.executeQuery(sqltempCDR);
                int li_row = 0;
                if (rss != null) {
                    while (rss.next()) {
                        amount = amount + rss.getDouble(1);
                        li_row++;
                    }
                }
                labelTotalAmountValue.setText(Double.toString(amount));
                rss.close();
                stmt.close();
                cn.close();
            } catch (Exception e) {
                e.PrintStackTrace();
            } 
    }
    There are 15 panels in a frame.
    So when I load the application this method is invoked 15 times.
    and though I have closed the connection, when i try to view the connection state in ms-dos command using netstat -a
    it dispaly around 15-16 connections
    with
    proto local address foreign address State
    tcp ip-address TIME_WAIT

    and I think this is also the reason that my application consumes Mem usage alot too and becomes slow.

    Any solution plz....

    Thanks in advance
    Last edited by Eranga; 12-19-2011 at 10:37 AM. Reason: code tags added

  2. #2
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default Re: Mutiple Connections problem

    I tried using the finally too
    finally
    {
    if(cn!=null)
    {
    cn.close();
    }
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Mutiple Connections problem

    In a single execution of cycle, you have to open the connection then process data and then close the connection. You have to maintain the sequence in each cycle. Seems you don't have work on that.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Mutiple Connections problem

    And also please use the code tags when you are posting again. Unformatted codes are really hard to read.

  5. #5
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default Re: Mutiple Connections problem

    But I think this is the process you open connection, process, then close the connection. If you have better way plz suggest me.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Mutiple Connections problem

    Only the other possibility you have is that open a connection and keep for the complete of all the processing you have. Connection open/close in periodically required lots of resources.

  7. #7
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Mutiple Connections problem

    TIME_WAIT is a TCP thing. It can hang around for a while after a connection is closed. Try again and wait a good few minutes to see if the TIME_WAITs disappear.

  8. #8
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default Re: Mutiple Connections problem

    Thank you so much your reply. let me try that too.

Similar Threads

  1. Problem with pool connections
    By juamd in forum Advanced Java
    Replies: 9
    Last Post: 06-02-2011, 06:32 PM
  2. mutiple object
    By ahmed80 in forum New To Java
    Replies: 1
    Last Post: 01-07-2011, 12:57 AM
  3. Storing Mutiple Classes/Data Types in a Linked List
    By Chronoattica in forum New To Java
    Replies: 7
    Last Post: 12-28-2010, 10:03 PM
  4. issue of mutiple panels
    By de.megha in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2010, 09:27 AM
  5. Replies: 0
    Last Post: 03-24-2010, 10:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •