Showing posts with label Java Exceptions. Show all posts
Showing posts with label Java Exceptions. Show all posts

Java Exhausted Result Set / Oracle & Websphere 5.1|java.sql.SQLException: Exhausted Resultset

I am experiencing some problems with a simple query on a thin oracle driver. I am using a connection from the application server connection pool on a cluster machine. Basically this is my code:

try {
conn = DAOFactory.getConnection();
stmt = conn.prepareStatement(SELECT_STATEMENT);
stmt.setInt(1, var);
stmt.setFetchSize(max);
rs = stmt.executeQuery();

while( rs.next() ) {
long id = rs.getLong(1);
}

All this I incorporate into a try/catch block like this:

} catch(SQLException e) {
// some exception handling
} finally {
try {
if(stmt!=null)
stmt.close();
if(rs!=null)
rs.close();
if(conn!=null)
conn.close();
} catch (SQLException e1) {
// some exception handling
}
}

After some runs I get sporadically the “Exhausted Resultset” Exception at line rs.getLong(1). This indicates, that my result set is closed. This will imply that rs.next() returns true even if there are no results! This kind of strange behaviour I have already noticed by some other users.

My question: How can I ensure that rs.next() will return false if the result set is empty? Does anybody else encounter this problem before and have a smooth solution?

java.net.ConnectException: Connection refused: connect; No available router to destination

Question :
I am trying to read from a URL and display the contents in an Applet. I am using URL,HttpURLConnection classes. I am consistently getting Connection time out Exception. I am unable to connect. Could any one help me out with this. I have tried some TimeOut handler classes available on the net but still I am getting the same Exception. Please provide your comments if you have come across similar problems before.

Here is the code Iam using

url = new URL((URL)null, str, new HttpTimeoutHandler(15000)); // timeout value in milliseconds
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.connect();
nputStream is = httpConnection.getInputStream();

Solution;
connection timeouts occur when:

the IP address for the requested server is successfully found connection establishment packets are dispatched to the IP address the destination address deliberately ignores or does not receive them
similar to connection timeout is Connection Refused, but in this case the destination system is actually sending packets back saying "go away, there is no service running on the port you are trying to connect to"
your packets are just being lost.. either the system or some intermediate system is firewalled. follow CMA's advice.. if you cant connect using telnet, a browser or some other app, its a network problem, not a java one

java.net.connectexception connection timed out connect Exception In java

I'm getting a ConnectException: Connection timed out with some frequency from my code. The URL I am trying to hit is up. The same code works for some users, but not others. It seems like once one user starts to get this exception they continue to get the exception.
 
java.net.ConnectException: Connection timed outCaused by: java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:516)
    at java.net.Socket.connect(Socket.java:466)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
    at sun.net.www.http.HttpClient.New(HttpClient.java:287)
    at sun.net.www.http.HttpClient.New(HttpClient.java:299)
 
Solution: 
Connection Timeouts (assuming a local network and several client machines) typically result from
a) some kind of firewall on the way that simply eats the packets without telling the sender things like "No Route to host"
b) packet loss due to wrong network configuration or line overload
c) too many requests overloading the server
d) a small number of simultaneously available threads/processes on the server which leads to all of them being taken. This happens especially with requests that take a long time to run and may combine with c).

How to get original exception object from inside a nested or wrapped Exception (for example an EJBException or RemoteException)

  • When you have an javax.ejb.EJBException, you can use the getCausedByException() that returns a java.lang.Exception.
  • A java.rmi.RemoteException there is a public field called detail of type java.lang.Throwable
  • With a java.sql.SQLException you need to use the method getNextException() to get the chained java.sql.SQLException.
  • When you have an java.lang.reflect.InvocationtargetException, you can get the thrown target java.lang.Throwable using the getTargetException() method.
As usual, the best way to check how to get that piece of information is to read the documentation of the specific Exception.

java.lang.OutOfMemoryError: Java heap space Exception in Java

Question java.lang.OutOfMemoryError: Java heap space
What does this error mean and how can I correct it?  

It means that the JVM has run out of all the memory that has been allocated to it. You can change the amount of memory allocated for use by your JVM using the -Xms and -Xmx command line parameters.

For example, the following says run MyApp in a JVM and allocate a minimum of 5 Megabytes and a maximum of 15 Megabytes off of the heap in order to do so.

   java -Xms5m -Xmx15m MyApp  

In tomcat the heap size can be passed to the java here.
"$_RUNJAVA" -Xmx512M $JAVA_OPTS $CATALINA_OPTS