I have a Java application with multiple concurrent threads. In a particular situation, I find that the call java.sql.DriverManager.getConnection
hangs and waits indefinitely causing the whole system freeze.
I suspect I have given the URL wrong, where there is no ORACLE server listening to this call, but there is another DB listener which is not returning back to the socket call. That could be the reason of this hang. When I changed the URL to a wrong value, e.g. jdbc:oracle:thin:@xx:21050:DB, where no DB listener (oracle or non-oracle) is running, getConnection
call comes out with java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
.
Is there any other API through which I may check at runtime if the URL is having an ORACLE server listener running out there? My problem for this hang is because I have some other DB (non-ORACLE) listener running there and my getConnection
hangs. Is there any remedy or workaround?
The thread dump shows me the following:
"AWT-EventQueue-0@3511" prio=6 tid=0x15 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.SocketDispatcher.read0(SocketDispatcher.java:-1)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:197)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
- locked <0x459c> (a java.lang.Object)
at oracle.net.nt.TimeoutSocketChannel.read(TimeoutSocketChannel.java:144)
at oracle.net.ns.NIOHeader.readHeaderBuffer(NIOHeader.java:82)
at oracle.net.ns.NIOPacket.readNIOPacket(NIOPacket.java:252)
at oracle.net.ns.NSProtocolNIO.negotiateConnection(NSProtocolNIO.java:118)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:317)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1438)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:518)
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:688)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:39)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:691)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
I found a similar problem discussed as: Oracle JDBC DriverManager.getConnection() hangs But my question is even if I supply some wrong parameters (URL, userid and password), why the call never comes back with a timeout? Can I add a timeout here or exception that I may handle in this call? Please help me out.