2

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.

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
  • "I suspect I have given the URL wrong", so you don't even know where you're connecting? It's not timing out because you haven't set a connection timeout, but setting a connection timeout is not required when you connect to the correct URL. – Kayaman Mar 04 '20 at 11:56
  • @Kayaman, my s/w helps out users to connect to multiple DB servers as opted, and this is an area I am trying to get a workaround, as my edited qs. Need to set a connection timeout. How? – Dr. Debasish Jana Mar 04 '20 at 12:00
  • See https://stackoverflow.com/questions/1683949/connection-timeout-for-drivermanager-getconnection – Ori Marko Mar 04 '20 at 12:02
  • Well, first you need to find out what your actual problem is. **If** it really warrants a connection timeout (which is highly *unlikely*), then you can consult the driver's documentation on how to set it. As for @user7294900, that question seems to be about how long a `Connection` is valid for, not the (network) connection timeout. – Kayaman Mar 04 '20 at 12:09
  • @Kayaman, my actual problem is trying to connect to a non-oracle server through Oracle's driver (client) and the non-oracle server doesnot return back nor a socket timeout. Perhaps, DriverManager.setLoginTimeout could have been done? or socket timeout? – Dr. Debasish Jana Mar 04 '20 at 12:32
  • So you have different databases at the same port `21050`, and you don't know whether they're Oracle or non-oracle? Then you connect to them with random drivers, hoping that it's the correct one? And you're trying to solve that with timeouts? Are you really sure about this? – Kayaman Mar 04 '20 at 12:40
  • @Kayaman, it's a bug in the software. While choosing the correct DB server, wrong DB server was chosen causing this. I was looking for a programming remedy with a timeout, thats all – Dr. Debasish Jana Mar 04 '20 at 12:49
  • Well, hopefully someone fixes the bug! But of course you're free to set a timeout. It can differ a bit between databases and drivers on how to do it, so search around. – Kayaman Mar 04 '20 at 13:01

0 Answers0