1

I have two applications using JDBC and MySQL opened in Eclipse Neon.

One application has:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase","root","password");

The other application has:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase","root","password");

I have run the two applications one after another in eclipse several times. The first application can run to connect to and read and write the database. The second application always gives an exception at the call to DriverManager.getConnection() method

java.sql.SQLException: Io exception: The Network Adapter could not establish the connection

I googled the error, and https://stackoverflow.com/a/12574926/9119247 seems to say it is my network adapter problem. But if it is, why does it work for one application but not the other, however I run them in whichever order?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Perhaps you reached max connections available. – Vladyslav Nikolaiev Dec 21 '17 at 20:42
  • How many connections does mySQL allow? Does the app properly close the connections? – Andrew S Dec 21 '17 at 20:43
  • @AndrewS Thanks. I am debugging the application over and over again, and most of time I don't reach the close method and then restart debugging. Can that create more and more connections to mysql server? Or everytime I exit debugging, the connection is closed by itself? How can I check how many connections my mysql server has? –  Dec 21 '17 at 22:13
  • @Vlad Thanks. Suppose the reason is because I reach the max connection limit. I repeatedly run the two applications one after another i.e. in interleaving order, it is always the first one which can run, while the second gives exception. Does it mean that multiple runs of the same application will reuse the same connection? –  Dec 21 '17 at 22:50
  • How can you get an Oracle exception when connecting to a MySQL database? Please post the full exception stacktrace and include a [MCVE] – Mark Rotteveel Dec 22 '17 at 11:27

2 Answers2

2

I think your second application is connecting to an Oracle database as well as a MySQL database, and it is the connection to Oracle, not the connection to MySQL, that is failing.

Take the following (admittely rather stupid) code:

import java.sql.*;

public class ConnectionsTest {
    public static void main(String[] args) throws Exception {
        System.out.println("Connecting to MySQL...");
        try {
            Connection mySQLConnection = DriverManager.getConnection(
                    "jdbc:mysql://8.8.8.8/3306/mysql", "X", "X");
            System.out.println("Odd, got MySQL connection!"); // Shouldn't get here
        }
        catch (SQLException e) {
            System.out.println("MySQL connection failed: " + e.getMessage());
        }

        System.out.println();
        System.out.println("Connecting to Oracle...");
        try {
            Connection oracleConnection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@8.8.8.8:1521:ORCL", "X", "X");
            System.out.println("Odd, got Oracle connection!"); // Shouldn't get here
        }
        catch (SQLException e) {
            System.out.println("Oracle connection failed: " + e.getMessage());
        }
    }
}

This code attempts to make two database connections, one to Oracle and one to MySQL, both of which should fail. (The server 8.8.8.8 won't be running either database.) When I run this code, I get the following output:

Connecting to MySQL...
MySQL connection failed: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

Connecting to Oracle...
Oracle connection failed: IO Error: The Network Adapter could not establish the connection

Note that the MySQL connection-failed error is different to the one you're getting, whereas the Oracle one matches.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • I think this is a great guess as to the cause. Of course, it's easy to verify (by OP), by simply looking at the stacktrace and seeing where the error occurs. – Andreas Dec 21 '17 at 23:21
  • Hi Luke, I am running Ubuntu, and Oracle Database doesn't support Ubuntu. The database connection parts of the two applications are shown in my post, and both are for MySQL. –  Dec 22 '17 at 00:09
  • @Mary: what's running Ubuntu, your development machine or the database server? If you're running Ubuntu, there's every chance that the database is on another machine. Alternatively, there might not even be an Oracle database anywhere, but you've still got code in your app that attempts to use the Oracle JDBC driver to connect to a (non-existent) Oracle database. The message "The Network Adapter could not establish the connection" is **very** characteristic of Oracle tools, and that is why I have written this answer. – Luke Woodward Dec 22 '17 at 22:18
  • Hi Luke, the problem went away after I restarted Eclipse, and reopened the application in it. Note that the application was originally written for Oracle DB, but I had changed it to use MySQL and saved the files, and then it was strange to me after you mentioned the exception was specific to Oracle DB. –  Dec 23 '17 at 00:26
-1

It is all about closing things. JDBC is best used with try-with-resources.

try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase",
            "root", "password")) {
    try (PreparedStatement stmt = conn.prepareStatement(...)) {
         ...
         try (ResultSet rs = stmt.executeQuery()) {
             ... return / throw ... no problem
         }
    }
}

For connections a connection pool is an option, that speeds up the application, as establishing a connection takes its toll. (Class.forName is not needed since a couple of years.)

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thanks, Joop! I noticed that the second application doesn't have close operations on connections an prepared statements. while the first one does. I was wondering if that can cause the second application to have "java.sql.SQLException: Io exception: The Network Adapter could not establish the connection", and after some googling, I haven't found other similar report. –  Dec 22 '17 at 12:41
  • It is a critical resource leakage on operating system components. Not encountered for years myself, so cannot tell more. Fortunately there is no question on what is to be done first. – Joop Eggen Dec 22 '17 at 12:50
  • Downvoted because this does not answer why the same code apparently works in one case and does not in the other. – Little Santi Dec 22 '17 at 23:07
  • @LittleSanti your downvote is justified as indeed I should have mentioned that not closing things will soon excerpt the free connections and so on. Embarassingly enough I cannot tell much about the mess of the miscellaneous timeouts and settings (like session timeout) influencing the system behaviour. – Joop Eggen Dec 24 '17 at 14:13