others-How to solve connection reset exception when connecting with Oracle 11g with JDBC

1. The purpose of this post

Sometimes, when we execute sql via JDBC with Oracle 11g , we get this exception or error like this:

> java -jar myapp.jar
> IO exception: connection reset

2. Solution

According to this post,there are two possible reasons you get this error:

java.security.SecureRandom is a standard API provided by sun. Among various methods offered by this class void nextBytes(byte[]) is one. This method is used for generating random bytes. Oracle 11g JDBC drivers use this API to generate random number during login. Users using Linux have been encountering SQLException(“Io exception: Connection reset”).

  1. The JVM tries to list all the files in the /tmp (or alternate tmp directory set by -Djava.io.tmpdir) when SecureRandom.nextBytes(byte[]) is invoked. If the number of files is large the method takes a long time to respond and hence cause the server to timeout
  1. The method void nextBytes(byte[]) uses /dev/random on Linux and on some machines which lack the random number generating hardware the operation slows down to the extent of bringing the whole login process to a halt. Ultimately the the user encounters SQLException(“Io exception:Connection reset”)

You can resolve the issue by adding this option to your command or system property:

2.1 Solution 1

-Djava.security.egd=file:/dev/../dev/urandom

2.2 Solution 2

System.setProperty("java.security.egd", "file:///dev/urandom");  // the 3 '/' are important to make it an URL

2.3 My solution

I choose the first solution:

> java -jar myapp.jar -Djava.security.egd=file:/dev/../dev/urandom

Run the command again , everything works fine now.