aspose file tools*
The moose likes Servlets and the fly likes Tomcat swallowing SQLException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Tomcat swallowing SQLException" Watch "Tomcat swallowing SQLException" New topic
Author

Tomcat swallowing SQLException

David Hunter
Greenhorn

Joined: Dec 15, 2011
Posts: 7
I am developing an app in Tomcat 7 on windows with Oracle 11g (thin driver) and Java 6. I am working on a page that lets users validate SQL statements that are stored in an Oracle database. My plan is to use the EXPLAIN PLAN FOR feature in Oracle to do this.

I have a JSP that executes the SQL statemnts in a try-catch-finally block that looks like this:



String error = "";

try{
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/myDatabase");
con = ds.getConnection();
st = con.prepareStatement(sql);
st.execute();
}
catch(SQLException e){
error = "SQL error: " + e.getMessage();
}
finally{

if(rs != null) rs.close();
if(st != null) st.close();
if(con != null) con.close();
}

// respond with error....


When a known valid SQL statement is passed, the JSP responds accordingly. However, when an error is introduced to the SQL statement ("SELECT fubar FROM mytable"), the SQLException is not caught by the JSP and the st.execute() statement does not return, like it's stuck in a loop.

The Tomcat log shows the SQL error. I have tried using different settings in the logging.properties file, and I have also tried using Log4J. I have also tried the the same code in Tomcat 6, and I have tried moving the code in the JSP to a DAO bean. None of this helps.

It sure appears to be the Tomcat logger that is swallowing the SQLExceptions, how can I change this behavior? I recently upgraded to Tomcat 7 from Tomcat 5, I don't recall this being a problem before, but I can't find anything on the internet about this. Anybody have any ideas?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

You are using JSP technology inappropriately. Modern JSPs should have no Java code within them. You should be doing this in a servlet where you have more control over what is going on, and use JSP only to create the resulting view.

The exception is in no way being ignored. It's being caught and logged just as your code specifies.

And, as this is nothing that is Tomcat-specific, it's been moved to the servlets forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

P.S. Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Maybe it was just the hard-to-read code, or maybe I was careless in reading it, but I don't see any logging going on in the code you posted. I do see your code (not Tomcat) catching an SQLException, but all it does is to store it in a variable. Personally I would call this "swallowing" the exception, but then your code finishes with a hint that the variable might be used elsewhere in some way.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

I could have sworn I saw a logging statement in there, but you're right. The code itself, in addition to being inappropriately placed within JSP, is what is swallowing the exception.
David Hunter
Greenhorn

Joined: Dec 15, 2011
Posts: 7
Bear Bibeault wrote:You are using JSP technology inappropriately. Modern JSPs should have no Java code within them. You should be doing this in a servlet where you have more control over what is going on, and use JSP only to create the resulting view.

The exception is in no way being ignored. It's being caught and logged just as your code specifies.

And, as this is nothing that is Tomcat-specific, it's been moved to the servlets forum.



My code does not log the error, some other process is doing that, presumably tomcat, which is why it appears to me that Tomcat is "swallowing" the exception. If you read the post, I tried moving the code to a Java class, so the problem does not lie in the use a JSP. I want my code to catch the error, save it to a variable, and use that in an HTTP response. Pretty simple, I think....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

David Hunter wrote:My code does not log the error, some other process is doing that, presumably tomcat, which is why it appears to me that Tomcat is "swallowing" the exception. If you read the post, I tried moving the code to a Java class, so the problem does not lie in the use a JSP. I want my code to catch the error, save it to a variable, and use that in an HTTP response. Pretty simple, I think....


Yes, that's what your code does. It catches AN error and saves it to a variable. So if you see some other process catching AN error, perhaps it's catching a different error. A more detailed enquiry would be necessary to find out if that were the case. Looking at stack traces always helps.
David Hunter
Greenhorn

Joined: Dec 15, 2011
Posts: 7
Paul Clapham wrote:Maybe it was just the hard-to-read code, or maybe I was careless in reading it, but I don't see any logging going on in the code you posted. I do see your code (not Tomcat) catching an SQLException, but all it does is to store it in a variable. Personally I would call this "swallowing" the exception, but then your code finishes with a hint that the variable might be used elsewhere in some way.



I posted a simplified block of code, just to convey what I'm trying to do. Sorry about the lack of formatting.

I am in fact trying to catch the exception and saving the error message in a string variable, which i will use in an HTTP response. To me, this seems very simple and basic, and i don;t understand what is swallowing and logging this error. Seems to me taht it's Tomcat, and I'm trying to find a configuration or logging setting that might be causing this.

I've tried the code in a Java class rather than a JSP, tow different versions of Tomcat, using a Statement instead of a PreparedStatement, executeQuery() instead of execute(), plus a few other things. The problem seems to be the fact that the query, when it has an error and does not return a resultset, cause the execute() to not return, and JSP does not complete and send a response.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Well, I have to say that Tomcat is a pretty poor choice of test environment. Sure, you didn't think you were making a choice, but by continuing to work with Tomcat you did. A better choice would be a plain old Java application which executes similar code. Then you would be able to debug the goings-on.
David Hunter
Greenhorn

Joined: Dec 15, 2011
Posts: 7
Paul Clapham wrote:
David Hunter wrote:My code does not log the error, some other process is doing that, presumably tomcat, which is why it appears to me that Tomcat is "swallowing" the exception. If you read the post, I tried moving the code to a Java class, so the problem does not lie in the use a JSP. I want my code to catch the error, save it to a variable, and use that in an HTTP response. Pretty simple, I think....


Yes, that's what your code does. It catches AN error and saves it to a variable. So if you see some other process catching AN error, perhaps it's catching a different error. A more detailed enquiry would be necessary to find out if that were the case. Looking at stack traces always helps.


the error in the log is clearly from the query. For instance, if I try to execute "select fubar from mytable", the error in the log is

java.sql.SQLException: ORA-00904: "fubar": invalid identifier
David Hunter
Greenhorn

Joined: Dec 15, 2011
Posts: 7
Paul Clapham wrote:Well, I have to say that Tomcat is a pretty poor choice of test environment. Sure, you didn't think you were making a choice, but by continuing to work with Tomcat you did. A better choice would be a plain old Java application which executes similar code. Then you would be able to debug the goings-on.


I don't have any choice in that matter
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

David Hunter wrote:
Paul Clapham wrote:Well, I have to say that Tomcat is a pretty poor choice of test environment. Sure, you didn't think you were making a choice, but by continuing to work with Tomcat you did. A better choice would be a plain old Java application which executes similar code. Then you would be able to debug the goings-on.


I don't have any choice in that matter


Does that mean you are not allowed to create a small application with 20 lines of code in it? Or where does that lack of choice come from?
David Hunter
Greenhorn

Joined: Dec 15, 2011
Posts: 7
Paul Clapham wrote:
David Hunter wrote:
Paul Clapham wrote:Well, I have to say that Tomcat is a pretty poor choice of test environment. Sure, you didn't think you were making a choice, but by continuing to work with Tomcat you did. A better choice would be a plain old Java application which executes similar code. Then you would be able to debug the goings-on.


I don't have any choice in that matter


Does that mean you are not allowed to create a small application with 20 lines of code in it? Or where does that lack of choice come from?


it means that this code has to work within a Tomcat web app
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

David Hunter wrote:
Paul Clapham wrote:
David Hunter wrote:
Paul Clapham wrote:Well, I have to say that Tomcat is a pretty poor choice of test environment. Sure, you didn't think you were making a choice, but by continuing to work with Tomcat you did. A better choice would be a plain old Java application which executes similar code. Then you would be able to debug the goings-on.


I don't have any choice in that matter


Does that mean you are not allowed to create a small application with 20 lines of code in it? Or where does that lack of choice come from?


it means that this code has to work within a Tomcat web app


Right. So test it in a controlled environment and then put it into Tomcat when you understand what's going on.
David Hunter
Greenhorn

Joined: Dec 15, 2011
Posts: 7
Found it! The problem was a linefeed character at the end of e.getMessage(), which apparently messes up JSON (sorry, I didn't think the use of JSON was germane when i posted my code fragment).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat swallowing SQLException