title: Mysql Exception in JAVA project
body: <p>In a simple java application with jdbc and mysql only I am getting error :</p>
  
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
  </code></pre>
  
  <p>Threading is used to open and close connection to Mysql</p>
  
  <p>I have tried increasing the max_connection variable of MySQL but it only delays the exception as it will again arise if the number of threads are increased</p>
  
  <p>Please provide root cause and resolution.</p>
  
  <pre><code>import java.sql.DriverManager;
  import java.sql.SQLException;
  public class Connection1 extends Thread{
  public java.sql.Connection con = null;
  
  public Connection1() throws ClassNotFoundException, SQLException {
      public static void main(String[] args) throws ClassNotFoundException, SQLException {
          for(int i=0;i&lt;500;i++) {
              new Thread(new Connection1().new  StartTester(i)).start();
          }
      }
  
      public void looper() throws ClassNotFoundException, SQLException {
          for(int i=0;i&lt;2;i++) {
              new Thread(this.new  StartTester(i)).start();
          }
      }
  
      public void connect() throws ClassNotFoundException, SQLException {
  
          Class.forName("com.mysql.jdbc.Driver");
          con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","esignal");
          System.out.println("con success "+con);
          con.close();
          System.out.println("con Closed "+con);
      }
  
      class StartTester implements Runnable {
  
          public StartTester(int count) {
              System.out.println("Thread count "+count);
          }
  
          @Override
          public void run() {
              try {
                  connect();
              } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>