title: java tcp server to many connections
body: <p>I wrote a simple TCP server to transfare some user Data to it and save it in an simple MySQL table. If i now run more than 2000 clients after each other it stops working. While running i get some <code>IO error java.io.EOFException</code> you may also see the misstake i made for that. But the most importand is that i get this</p>
  
  <pre><code> IO error java.net.SocketException: Connection reset
      Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
      at java.lang.Thread.start0(Native Method)
      at java.lang.Thread.start(Unknown Source)
      at Server.main(Server.java:49)
  </code></pre>
  
  <p>Enough Memory schould be there but the threads are still running and i dont see where i made the misstake that they dont get terminated. So i got up to 3900 threads running than.
  So here is the part of the Server:</p>
  
  <pre><code>try {
      // create new socket
      ServerSocket sock = new ServerSocket(port);
      textArea.setText(textArea.getText() + "Server started\n");
      while (true) {
          // accept the connection
              Socket newsock = sock.accept();
          // handle the action
          Thread t = new ThreadHandler(newsock);
              newsock.setSoTimeout(2000); // adding client timeout
          t.start();
          }
      } catch (Exception e) {
  </code></pre>
  
  <p>guess really simple. Here is how i handle the socket:</p>
  
  <pre><code>class ThreadHandler extends Thread {
      private Socket socket;
      private MySQLConnection sqlConnection;
  
      ThreadHandler(Socket s) {
          socket = s;
          sqlConnection = new MySQLConnection();
      }
  
      public void run() {
          try {
              DataOutputStream out = new DataOutputStream(
                      socket.getOutputStream());
              DataInputStream in = new DataInputStream(new BufferedInputStream(
                      socket.getInputStream()));
              Server.textArea.append((new Date()) + "\nClient connected IP: " + socket.getInetAddress().toString()+"\n");
  
              int firstLine = in.readInt(); // get first line for switch
  
              switch (firstLine) {
              case 0:
                  // getting the whole objekt for the database in own lines!
                  String name2 = in.readUTF();
                  int level2 = in.readInt();
                  int kp2 = in.readInt();
                  String skill = in.readUTF();
  
                  LeadboardElement element2 = new LeadboardElement();
                  element2.setName(name2);
                  element2.setLevel(level2);
                  element2.setKillPoints(kp2);
                  element2.setSkill(skill);
                  sqlConnection.saveChaToLeadboard(element2);
                  break;
                  //case 1 return the top10
  ###.... shorten here the rest of the cases
                  out.close();
              in.close();
              //close this socket
              socket.close();
                      Server.textArea.append("Client disconnected IP: " + socket.getInetAddress().toString()+ "\n" + (new Date())
                              + "\n----------------------------------------------------\n");
              // autoscrolldown
              Server.textArea.setCaretPosition(Server.textArea.getDocument()
                      .getLength());
           } catch (Exception e) {
              System.out.println("IO error " + e);
              try {
                  socket.close();
              } catch (IOException e1) {
                  e1.printStackTrace();
              }
          }finally{
          try {
              socket.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>the <code>saveChaToLeadboard</code> simply gets the name level kp and skill and uses a <code>preparedStatement</code> so save it to my MySQL Table.
  I hope you can help me i just dont see the misstake of it. I think i need to Join it somewhere but if i put a join at the end of it (after socket.close()) it still does the same.</p>
  
  <p>Here the save to database methode:</p>
  
  <pre><code>public void saveChaToLeadboard(LeadboardElement element) {
          try {
              // load driver
              Class.forName("com.mysql.jdbc.Driver");
              connection = DriverManager.getConnection(this.databaseURL
                      + DATABASE_NAME, this.user, this.password);
              // insert values into the prep statement
              preparedStatement = connection
                      .prepareStatement(PREP_INSERT_STATEMENT);
              preparedStatement.setString(1, element.getName());
              preparedStatement.setInt(2, element.getLevel());
              preparedStatement.setInt(3, element.getKillPoints());
              if(!element.getSkill().equalsIgnoreCase("")){
                  preparedStatement.setString(4, element.getSkill());
              }else{
                  preparedStatement.setString(4, null);
              }
              // execute
              preparedStatement.executeUpdate();
              connection.close();
  
          } catch (Exception e) {
              Server.textArea.append(e.getMessage() + "\n");
              Server.textArea.setCaretPosition(Server.textArea.getDocument()
                      .getLength());
              try {
                  connection.close();
              } catch (SQLException e1) {
                  e1.printStackTrace();
              }
          }
  </code></pre>
  
  <p>Thanks alot! 
  Regards</p>