2
votes

I have a really simple piece of JAVA code, where I try to connect from JAVA to my Oracle DB.

Everything works under Windows, but when I try to run it on Ubuntu, I have got an error.

I read a lot and I have tried lot of solutions. Here's my code:

package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class OracleJDBC {

    private static String driver = "oracle.jdbc.driver.OracleDriver";
    private static String password = "*****";
    private static String dbname = "XE";
    private static String username = "userir";

    public Connection getConnection() {

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        Connection connection = null;

        try {
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:" + dbname, username,
                    password);
        } catch (SQLException e) {
            System.out.println("Connection Failed");
            e.printStackTrace();
            return null;
        }
        return connection;

    }

    public void closeConnection(Connection connection) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

when I run it, I receive an error:

Connection Failed java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458) at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:546) at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:236) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521) at java.sql.DriverManager.getConnection(DriverManager.java:571) at java.sql.DriverManager.getConnection(DriverManager.java:215) at utils.OracleJDBC.getConnection(OracleJDBC.java:26) at utils.TestMain.main(TestMain.java:6) Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392) at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434) at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687) at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247) at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320) ... 8 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:150) at oracle.net.nt.ConnOption.connect(ConnOption.java:133) at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370) ... 13 more

I logged in: sqlplus sys as sysdba

I started db: startup

I logged into Oracle as userir. XE exists.

I have no idea, what I did wrong...

Thank you in advance for your hints!

3
Few things: Is your ubuntu box hosting the Oracle instance? If not, your connection string is pointing to localhost and needs to be updated. Two: You mentioned you tried many solutions, what have you tried?Robert H
Hi, thank you for this answer. I do not understand your solution. How should I update this string? I tried to change user, I checked if port is ok and all the credentials.user3006279
Is Oracle really running on the Linux box? Or only on your Windows computer?a_horse_with_no_name
I did everything on Windows. After, I installed oracle on Linux and started database. Everything I run locally. The problem is with this port. I wrote in comments below, which error I receiveduser3006279
Is the db listening on the same port?Bohemian♦

3 Answers

1
votes

Verify your connection string.

I've experienced similar issues with various databases because I forgot to update localhost with the proper hostname.

  connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:" + dbname, username,
                password);

Ensure that the connection string matches what you're specifying in sqlplus and you should be good.

Finding the connection string

Determining the connection string can be difficult, but being that you can connect via sqlplus it should be a bit easier:

My SqlPlus connection string looks like: sqlplus user/pass@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))

If I bring this into Java it now looks like: "jdbc:oracle:thin:user/pass@//hostname.network:1521/remote_SID";

Alternatively, if you connect via SQLDeveloper the string is made up on the connection properties:

enter image description here

now the hard part is if you truly are localhost and need to figure out the connection string from there.

First, try your IP address instead of localhost (In my screenshot, the 192.168.0.106) to make a string like:

"jdbc:oracle:thin:user/pass@//192.168.0.106:1521/remote_SID";

If that doesn't work, check your firewall, ensure that port 1521 is open (assuming you did not change the port setting, alter text accordingly if you did)

If still no resolution you will need to check out your listeners file. I'm going to link to some documentation if that's the case and kindly defer to some of the more senior Oracle/Linux guru's

0
votes

I'm guessing is that you've got some firewall or some network thingy blocking you from getting from your Linux box to your oracle server. Note the "connection refused" portion of the stacktrace..

...
java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at 
...

Oracle listens on port 1521, no? Maybe shell on the linux box and see if you can just telnet to the oracle host of 1521

telnet localhost 1521

Did you get connection refused there as well? I'm guessing that the Linux box doesn't have port 1521 open for whatever reason (firewall or the oracle listener isn't running).

Also, are you sure its localhost that you want to connect to when you deploy to Linux or is the expectation that the Oracle server is on a different machine? If the Oracle server is on a different machine, that machine's name needs to be in the connection string and not "localhost".

Welcome to the worst part of JDBC; figuring out the freaking connection string.

0
votes

The issue is with secure random gathering.

As per this SO Discussion, Oracle JDBC Driver tries to get a secure random from the OS that it uses as part of connection establishment with Oracle. The issue is that your OS (Ubuntu) doesn't have enough randoms to give back and blocks you till it secure your request (the request of Oracle JDBC Driver).

A solution would be to ask Oracle JDBC Driver to avoid blocking until getting the complete requested random and use whatever it found. To do so, you have to use -Djava.security.egd=file:///dev/urandom.

More details on the SO Discussion mentioned above.