Saat ini saya mencoba menghubungkan database Oracle 11g dari server Tomcat dan saya mendapatkan kesalahan berikut:

Caused by: 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.<init>(PhysicalConnection.java:546)
    at oracle.jdbc.driver.T4CConnection.<init>(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:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:154)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:145)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169)
    at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:50)
    ... 34 more
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)
    ... 45 more
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    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)
    ... 50 more

Saya pikir itu akan menjadi semacam masalah firewall atau masalah database down. Jadi saya mencoba terhubung melalui SQL Developer dan itu berfungsi dengan baik.

Saya mencoba terhubung dengan menjalankan program Java berikut:

public class RunDB {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@9.2.2.2:1521:ORCL", "user", "pwd");  
    }
}

Masih terjadi kesalahan yang sama:

Exception in thread "main" java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:257)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:389)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.mkyong.test.RunDB.main(RunDB.java:13)

Saya menggunakan ide Java 8 dan Spring Tool Suite.

JVM saya menggunakan protokol IPv6.

Jika tidak, apakah ada cara untuk memperbaiki masalah ini?

0
Joby Wilson Mathews 10 Januari 2018, 13:29

1 menjawab

Jawaban Terbaik

Akhirnya saya memperbaiki masalah ini, kesalahan ini disebabkan oleh JVM yang menggunakan protokol IPv6 yang tidak didukung oleh jaringan/server Oracle.

Karena saya menggunakan server Tomcat untuk aplikasi saya, saya menambahkan argumen berikut ke Tomcat

-Djava.net.preferIPv4Stack=true

Atau Anda dapat menambahkan argumen ini sebagai variabel lingkungan Sistem sebagai:

Nama variabel : _JAVA_OPTIONS

Nilai variabel: -Djava.net.preferIPv4Stack=true

1
Joby Wilson Mathews 22 Januari 2018, 05:13