title: What will URLConnection object return when connection is broken
body: <p>I am using the URLConnection object in java, to post data to a remote database (via REST). I need to post alot of small datapackages, so I'd like to let my connection stay open until I'm sure there are no more new datapackages to send. However, it is possible that there are long periods of time, when there will be no datapackages to send. </p>
  
  <p>I'm wondering if the URLConnection object will be reset to null, when the connection is broken (because of timeout for example). If that is the case I could just check if the object is null and reconnect. However, if that's not the case, I would like some suggestion on how to solve this problem cheaply (will it be necessary to open and close a connection for each datapackage?).</p>
  
  <p>this is my current code (not completely finished! :) ):</p>
  
  <pre><code>public class PersistenceServiceImpl implements PersistenceService, Runnable{
  
  public void activate(){
      new Thread(this).start();
  }
  
  @Override
  public void run() {
      LinkedBlockingQueue&lt;JSONObject&gt; queue = JSONQueue.getUniqueInstance();
      JSONObject json;
      try {
          final URL url = new URL("");
          URLConnection urlConn = null;
          DataOutputStream printout;
          while(true){
              makeConnection(url, urlConn);
              try {
                  json = queue.take();
                  printout = new DataOutputStream (urlConn.getOutputStream());
                  String content = json.toString();
                  printout.writeBytes(content);
                  printout.flush();
                  printout.close();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }catch(Exception e){
  
      }
  }
  
  public void makeConnection(URL url, URLConnection urlConn) {
      if(urlConn == null){
          try {
              urlConn = url.openConnection();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }else{
          try {
              urlConn.connect();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
      urlConn.setDoInput(false);
      urlConn.setDoOutput(true);
      urlConn.setUseCaches(false);
      urlConn.setRequestProperty("Content-Type", "application/json");
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>my new and improved (or so I hope) code :p :</p>
  
  <pre><code>public class PersistenceServiceImpl implements PersistenceService, Runnable{
  
  public void activate(){
      new Thread(this).start();
  }
  
  LinkedBlockingQueue&lt;JSONObject&gt; queue = JSONQueue.getUniqueInstance();
  JSONObject json;
  final String urlString = new String("http://localhost/");
  @Override
  public void run() {
      while(true){
          try {
              URL url = new URL("http://localhost:8080/RESTful");
              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
              conn.setDoOutput(true);
              conn.setRequestMethod("POST");
              conn.setRequestProperty("Content-Type", "application/json");
              json = queue.take();
              DataOutputStream output = new DataOutputStream(conn.getOutputStream());
              output.write(json.toString().getBytes("utf-8"));
              output.flush();
              output.close();
          } catch (MalformedURLException e) {
              System.out.println("MALFORMEDURLEXCEPTION: fout url!!");
              e.printStackTrace();
          } catch (ProtocolException e){
              System.out.println("PROTCOL EXCEPTION: post fout");
          } catch (IOException e) {
              System.out.println("IOEXCEPTION: connection failure");
              e.printStackTrace();
          } catch (InterruptedException e) {
              System.out.println("INTERRUPTEDEXCEPTION: queue.take() deed iets fout");
              e.printStackTrace();
          } 
  </code></pre>