title: Problem with singleton database connection
body: <p>I'm using singleton design-pattern for my database connection.</p>
  
  <p>Here is my method.</p>
  
  <pre><code>     private static Connection con=null;
  public static Connection getConnection(){
      try {
          if(con==null){
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
          }else if(con.isClosed()){
           Class.forName("com.mysql.jdbc.Driver").newInstance();
          con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
          }
      } catch (Exception ex) {
  
      }
      return con;
  }
  </code></pre>
  
  <p>Whenever I restarted mysql service, it generates following error and have to restart the application also to run again. </p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
    Communications link failure</p>
  </blockquote>
  
  <p>Can anyone show what is the correct way of implementing this to avoid this error?</p>
  
  <p>thank you.  </p>