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

19,117

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

Share:
19,117
farbodg
Author by

farbodg

Software Engineer

Updated on June 05, 2022

Comments

  • farbodg
    farbodg almost 2 years

    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.

  • farbodg
    farbodg about 10 years
    Thanks for your help. I figured out what the issue was. After talking with my colleagues, I was told that this issue is common due to the way RAC works. I'm connecting to a Oracle RAC cluster, and there are three hosts under the service name I'm connecting to. When I'm initializing my connection, the load balancer returns the machine name (not the IP address) of the database server I will be using for my connection. So when my machine tries to resolve the machine name of the server, it fails. The fix is to place an entry in your hosts file. After doing this, my failures stopped.
  • codenheim
    codenheim about 10 years
    Sure. I'm familiar with RAC. Once your client machine is successfully configured, the RAC load balancing should be transparent to your JDBC client (which it sounds like you discovered). Did you place entries into your hosts file for all the RAC nodes (vips)?.
  • farbodg
    farbodg about 10 years
    Yeah I had to place all three machines in my hosts file.