Getting "IO Error: The Network Adapter could not establish the connection" randomly [duplicate]

StackOverflow https://stackoverflow.com/questions/22213580

  •  10-06-2023
  •  | 
  •  

Question

I'm randomly getting "IO Error: The Network Adapter could not establish the connection" when trying to connect to an Oracle database through Java. Sometimes I have to run my application a couple times before it stops throwing the error.

// initializes database connection
private static Connection initializeDatabaseConnection(Properties prop) {

    System.setProperty("oracle.net.tns_admin", prop.getProperty("tnsLocation"));

    try {
        Class.forName("oracle.jdbc.OracleDriver");
    }
    catch (ClassNotFoundException ex)
    {
        System.out.println(ex.getMessage());
    }

    String dbURL = "jdbc:oracle:thin:@" + prop.getProperty("serviceName");
    String username = prop.getProperty("username");
    String password = prop.getProperty("password");

    Connection conn = null;

    try {
        conn = DriverManager.getConnection(dbURL, username, password);
    }
    catch (SQLException ex)
    {
        System.out.println("Error initializing database connection. " + ex.getMessage());
        System.exit(1);
    }

    return conn;
}

Any ideas as to why it throws that error randomly? I'm using JDK 1.7 with the ojdbc6.jar driver.

Was it helpful?

Solution

Download PingPlotter and turn it on (leave it running for 24 hours or so). Watch the historical graph, you'll likely find the IP address dropping or response times going through the roof at the same time your app can't connect to Oracle. You don't have a logic error, either the server is drastically overloaded, or you've got major congestion between you and it. Are you connecting through a VPN?

http://www.pingplotter.com/

Or just use ping from the command line in continuous mode for an hour or so:

  ping -t <IP address or hostname>

Ping plotter doesn't ping as often and it creates nice graphs.

If you are working remotely, then it could be your ISP. Otherwise, talk to the network administrator or the DBA.

Obviously you can't go into production with a poor DB connection, so hopefully you just see this issue from your development environment. Try deploying to the machine where you plan to run the application eventually and see if it improves.

If there is nothing you can do to make it better, I recommend increasing your initial login timeout for your JDBC driver.

Google JDBC setLoginTimeout

Here is an example, although there are several ways to go about it, depending on your driver.

Connection timeout for DriverManager getConnection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top