4 Replies Latest reply: Feb 20, 2013 1:52 PM by jschellSomeoneStoleMyAlias RSS

    as long as i close them, i can open as many connections as i want?

    860579

      After retreiving 5000 records from a ResultSet, I get this exception:

      com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
      ** BEGIN NESTED EXCEPTION **
      java.net.SocketException
      MESSAGE: java.net.BindException: Address already in use: connect

      I have closed every ResultSet, PreparedSatement, and DB Connection object. But, am I still running out of some jdbc resource?

      Here is my code:
      public class DataTable {
      public static void load() throws Exception {
      Connection dbconn = IOTools.getDbConn("foo_db");
      PreparedStatement ps = dbconn.prepareStatement("SELECT word, df FROM dictionary;");
      ResultSet rs = ps.executeQuery();

      while(rs.next()) {
      String word = rs.getString(1);
      int id = WordRegistration.getId("foo_db", word); // <--- 3rd to last line in stack trace
      }

      ps.close(); rs.close(); dbconn.close();
      }
      }


      public class IOTools {

      public static Connection getDbConn(String dbname) throws Exception {
      Class.forName("com.mysql.jdbc.Driver");
      String connStrg = "jdbc:mysql://localhost/" + dbname + "?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8";
      return DriverManager.getConnection(connStrg, "user", "password"); // <--- last line in stacktrace

      }
      }

      public class WordRegistration {

      public static int getId(String dbname, String word) throws Exception {
      int id = -1;
      Connection dbconn = IOTools.getDbConn(dbname); // <--- 2nd to last line in stacktrace
      PreparedStatement ps = dbconn.prepareStatement("SELECT id FROM word_registration WHERE word=?");
      ps.setString(1, word);
      ResultSet rs = ps.executeQuery();
      if(rs.next()) { id = rs.getInt(1); }
      rs.close(); ps.close(); dbconn.close();
      return id;
      }

      }



      __stack trace__

      com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
      ** BEGIN NESTED EXCEPTION **
      java.net.SocketException
      MESSAGE: java.net.BindException: Address already in use: connect
      STACKTRACE:
      java.net.SocketException: java.net.BindException: Address already in use: connect
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
      at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
      at com.mysql.jdbc.Connection.createNewIO(Connection.java:2555)
      at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at dw.util.IOTools.getDbConn(IOTools.java:104)
      at dw.util.IOTools.getDbConn(IOTools.java:99)
      at gaku.datamodel.WordReg.getId(WordReg.java:15)
      at gaku.datamodel.JpEng.load(JpEng.java:69)
      at gaku.datamodel.JpEng.load(JpEng.java:46)
      at dw.util.Main.jpeng(Main.java:83)
      at dw.util.Main.main(Main.java:20)
      ** END NESTED EXCEPTION **

      Last packet sent to the server was 15 ms ago.
      at com.mysql.jdbc.Connection.createNewIO(Connection.java:2621)
      at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at dw.util.IOTools.getDbConn(IOTools.java:104)
      at dw.util.IOTools.getDbConn(IOTools.java:99)
      at gaku.datamodel.WordReg.getId(WordReg.java:15)
      at gaku.datamodel.JpEng.load(JpEng.java:69)
      at gaku.datamodel.JpEng.load(JpEng.java:46)
      at dw.util.Main.jpeng(Main.java:83)
      at dw.util.Main.main(Main.java:20)