Discussions

Performance and scalability: Is java.net.HttpURLConnection thread safe??

  1. Is java.net.HttpURLConnection thread safe?? (4 messages)

    Hi,
     
    I am using the java.net.HttpURLConnection to get the connection to the database. Depend on the input parameter I am getting the result.
     
    Query : if more than one person get the connection with using java.net.HttpURLConnection and sends their inputs at the same time, will they get their respective results. ie, to say is the connection is thread safe??
     
    Please suggest.
     
    Thanks,
      Madhu

    Threaded Messages (4)

  2. please elaborate[ Go to top ]

    How are you using the HTTPUrlConnection to get a database connection? Please paste code.
  3. Here is code....[ Go to top ]

    Thanks for the reply, Osullivan.
    Here is the code. Please let me know if you need any more information.

    ......
           ......
            String myResp="";
            BufferedReader br=null;
            String myquery= "";

            java.net.URL url;
            java.net.HttpURLConnection con=null;
           
           try {

             url = form the url
    //( ie. url = new URL("http://"+Server_name":"+other information);

        con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.connect();
     
     if (con.getResponseCode()==HttpURLConnection.HTTP_OK)
        {
         br= new BufferedReader(new InputStreamReader con.getInputStream()));
         String tmp = br.readLine();
         while(tmp!=null)
          {
            myResp=myResp+tmp;
    tmp=br.readLine();
          }
         }
        ......
        ...... // populate the response to the data beans.
      }catch (Exception e) {
                    
      }finally {
         br.close();
         con.disconnect();
        }

             ......
             .......
          }
  4. Hmm[ Go to top ]

    Hi Madhu,

    Ok I understand what you are doing now.

    I guess the question is really whether URLConnection.connect() is threadsafe, as that is the one that issues the HTTP request. Clearly each of your clients is running as a discreet process, so they don't share any memory segments. So there is not need to worry about thread safety among clients.

    So the only other worry would be whether client requests and handled with thread safety on the server. This depends on the database implementation you are using to handle the incoming HTTP requests.

    If it is a vendor product, such as Mod-PL/SQL, it can be assumed to be thread-safe in this respect, otherwise it would not be fit for purpose!

    If you have developed your own HTTP broker for the database, this may well thread unsafe, for example if you are using Servlets with member variables.
  5. regarding HtpURLConnection[ Go to top ]

    Thanks for your detailed information, Osullivan.

    Thanks,
     Madhu