Thread: Servlet problems
Hi all. I'm a bit confused here. I'm using Tomcat JSP/Servlet engine. I have a database in PostgreSQL, which holds a table describingan n-tree structure (hierarchy). I would like my JSP page to able to display (if parameter "print" is present)a complete sub-tree of a given node. This is a simple recursion. What I'm trying to do is make our coroporate E-Mailaddress book, so I have organizational units and users. Without recursion everything works fine, all the direct subnodes are displayed. When I use recusrion to include all subnodes,I get strange errors. First, it was killing the response in the middile of a table, in the middle of a row. Now it dies with "NullPointerException:next()". In other words, in one of the "ResultSet.next()" methods, it runs into a non existant ResultSet. I have tried putting a "breakpoint" (throwing a ServletException) on all lines of my JSP page. When there is a "breakpoint"I do not run into a NullPointerException. When I remove it, I have exception. My conclusion was that I get the error AFTER my JSP page starts emerging from the recursion (if it was inside recursion,it would have been stopped by the "breakpoint"). So, after a page calls itself once, the "insider" does it's thingand then exits and releases control to it's caller, the the exception occurs. I must admit that JSP page closes it's JDBC objects, I thought it would be OK, since the page creates them. This is the flow of events: OUDisplay.jsp [instance 1]: ---------------------------- DriverManager.getConnection() Connection.createStatement() Statement.executeQuery() while( ResultSet.next() ) { ... <jsp.include page="OUDisplay.jsp" /> OUDisplay.jsp [instance 2]: ---------------------------- DriverManager.getConnection() Connection.createStatement() Statement.executeQuery() while( ResultSet.next() ) { ... } ResultSet.close() Statement.close() Connection.close() --------------[instance 2]- } ResultSet.close() Statement.close() Connection.close() --------------[instance 1]- The problem occurs inside the outer "next() {...}", which leads me to believe that Some objects in the "instance 1" get screwedup, but I don't know how. Any insight? Nix.
Nikola, I suspect that you are correct, you would have to look at the compiled code to see what is going on. It is possible that it is using the same result set? Either way you would be better reading the categories in a class. In my servlet code I read the categories into a static tree. The assumptions are 1) the category tree doesn't change a lot. 2) it is very costly to recurse through the tree Dave -----Original Message----- From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Nikola Milutinovic Sent: Monday, December 17, 2001 9:32 AM To: PostgreSQL JDBC Subject: [JDBC] Servlet problems Hi all. I'm a bit confused here. I'm using Tomcat JSP/Servlet engine. I have a database in PostgreSQL, which holds a table describing an n-tree structure (hierarchy). I would like my JSP page to able to display (if parameter "print" is present) a complete sub-tree of a given node. This is a simple recursion. What I'm trying to do is make our coroporate E-Mail address book, so I have organizational units and users. Without recursion everything works fine, all the direct subnodes are displayed. When I use recusrion to include all subnodes, I get strange errors. First, it was killing the response in the middile of a table, in the middle of a row. Now it dies with "NullPointerException: next()". In other words, in one of the "ResultSet.next()" methods, it runs into a non existant ResultSet. I have tried putting a "breakpoint" (throwing a ServletException) on all lines of my JSP page. When there is a "breakpoint" I do not run into a NullPointerException. When I remove it, I have exception. My conclusion was that I get the error AFTER my JSP page starts emerging from the recursion (if it was inside recursion, it would have been stopped by the "breakpoint"). So, after a page calls itself once, the "insider" does it's thing and then exits and releases control to it's caller, the the exception occurs. I must admit that JSP page closes it's JDBC objects, I thought it would be OK, since the page creates them. This is the flow of events: OUDisplay.jsp [instance 1]: ---------------------------- DriverManager.getConnection() Connection.createStatement() Statement.executeQuery() while( ResultSet.next() ) { ... <jsp.include page="OUDisplay.jsp" /> OUDisplay.jsp [instance 2]: ---------------------------- DriverManager.getConnection() Connection.createStatement() Statement.executeQuery() while( ResultSet.next() ) { ... } ResultSet.close() Statement.close() Connection.close() --------------[instance 2]- } ResultSet.close() Statement.close() Connection.close() --------------[instance 1]- The problem occurs inside the outer "next() {...}", which leads me to believe that Some objects in the "instance 1" get screwed up, but I don't know how. Any insight? Nix. ---------------------------(end of broadcast)--------------------------- TIP 3: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to majordomo@postgresql.org so that your message can get through to the mailing list cleanly
Hi! I'm afraid I don't speak JSP ;-) but I may siggest something. Can you try to flush every output you send? For every println(), if you use them, just place a flush() immediately after it. That WILL NOT solve your problem, and WILL degrade performance, but MAY help you find the source for your trouble. Once your problem is solved (I let you do the hard work), you can safely take them away. Good luck! Antonio Nikola Milutinovic wrote: >Hi all. > >I'm a bit confused here. I'm using Tomcat JSP/Servlet engine. I have a database in PostgreSQL, which holds a table describingan n-tree structure (hierarchy). I would like my JSP page to able to display (if parameter "print" is present)a complete sub-tree of a given node. This is a simple recursion. What I'm trying to do is make our coroporate E-Mailaddress book, so I have organizational units and users. > >Without recursion everything works fine, all the direct subnodes are displayed. When I use recusrion to include all subnodes,I get strange errors. > >First, it was killing the response in the middile of a table, in the middle of a row. Now it dies with "NullPointerException:next()". In other words, in one of the "ResultSet.next()" methods, it runs into a non existant ResultSet. > >I have tried putting a "breakpoint" (throwing a ServletException) on all lines of my JSP page. When there is a "breakpoint"I do not run into a NullPointerException. When I remove it, I have exception. > >My conclusion was that I get the error AFTER my JSP page starts emerging from the recursion (if it was inside recursion,it would have been stopped by the "breakpoint"). So, after a page calls itself once, the "insider" does it's thingand then exits and releases control to it's caller, the the exception occurs. > >I must admit that JSP page closes it's JDBC objects, I thought it would be OK, since the page creates them. > >This is the flow of events: > >OUDisplay.jsp [instance 1]: >---------------------------- > DriverManager.getConnection() > Connection.createStatement() > Statement.executeQuery() > while( ResultSet.next() ) { > ... > <jsp.include page="OUDisplay.jsp" /> > > OUDisplay.jsp [instance 2]: > ---------------------------- > DriverManager.getConnection() > Connection.createStatement() > Statement.executeQuery() > while( ResultSet.next() ) { > ... > } > ResultSet.close() > Statement.close() > Connection.close() > --------------[instance 2]- > > } > ResultSet.close() > Statement.close() > Connection.close() >--------------[instance 1]- > >The problem occurs inside the outer "next() {...}", which leads me to believe that Some objects in the "instance 1" getscrewed up, but I don't know how. > >Any insight? > >Nix. > >---------------------------(end of broadcast)--------------------------- >TIP 3: if posting/reading through Usenet, please send an appropriate >subscribe-nomail command to majordomo@postgresql.org so that your >message can get through to the mailing list cleanly > >. >
Nikola- I have a thought which doesn't match your description precisely, but might still be worth a try if you haven't already thought of it. I'm reminded of it because I had a similar mystery once where my jsp would die mysteriously unless I interrupted it before completion with a debug statement. In my case the problem was that totmcat starts you off with a fairly small chunk of memory to write each page to. I don't remember the exact number, but I'm thinking it was 8K or 16K. The problem was that I overran this buffer, resulting in an error that was misleading every time the page grew to a certain size. A sizable email directory might be big enough to do this. The fix was to insert this line at the top: <%@ page buffer="64kb" %> If this doesn't help, I'd suggest looking in Tomcat's work directory for the java code that gets produced & posting a copy of it to the list. Although many folks on the list don't speak jsp, they'll all be able to grok the java version. -Nick > -----Original Message----- > From: pgsql-jdbc-owner@postgresql.org > [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Nikola Milutinovic > Sent: Monday, December 17, 2001 9:32 AM > To: PostgreSQL JDBC > Subject: [JDBC] Servlet problems > > > Hi all. > > I'm a bit confused here. I'm using Tomcat JSP/Servlet engine. I > have a database in PostgreSQL, which holds a table describing an > n-tree structure (hierarchy). I would like my JSP page to able to > display (if parameter "print" is present) a complete sub-tree of > a given node. This is a simple recursion. What I'm trying to do > is make our coroporate E-Mail address book, so I have > organizational units and users. > > Without recursion everything works fine, all the direct subnodes > are displayed. When I use recusrion to include all subnodes, I > get strange errors. > > First, it was killing the response in the middile of a table, in > the middle of a row. Now it dies with "NullPointerException: > next()". In other words, in one of the "ResultSet.next()" > methods, it runs into a non existant ResultSet. > > I have tried putting a "breakpoint" (throwing a ServletException) > on all lines of my JSP page. When there is a "breakpoint" I do > not run into a NullPointerException. When I remove it, I have exception. > > My conclusion was that I get the error AFTER my JSP page starts > emerging from the recursion (if it was inside recursion, it would > have been stopped by the "breakpoint"). So, after a page calls > itself once, the "insider" does it's thing and then exits and > releases control to it's caller, the the exception occurs. > > I must admit that JSP page closes it's JDBC objects, I thought it > would be OK, since the page creates them. > > This is the flow of events: > > OUDisplay.jsp [instance 1]: > ---------------------------- > DriverManager.getConnection() > Connection.createStatement() > Statement.executeQuery() > while( ResultSet.next() ) { > ... > <jsp.include page="OUDisplay.jsp" /> > > OUDisplay.jsp [instance 2]: > ---------------------------- > DriverManager.getConnection() > Connection.createStatement() > Statement.executeQuery() > while( ResultSet.next() ) { > ... > } > ResultSet.close() > Statement.close() > Connection.close() > --------------[instance 2]- > > } > ResultSet.close() > Statement.close() > Connection.close() > --------------[instance 1]- > > The problem occurs inside the outer "next() {...}", which leads > me to believe that Some objects in the "instance 1" get screwed > up, but I don't know how. > > Any insight? > > Nix. > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly >
Use try/catch/finally blocks. Make sure that you close your connection in finally block. try { all ur code goes here } catch (SQLException e) { print exception } catch (Exception e) { print exception } finally { try { close resultset close statement close connection catch (SQLException e) { } } Jayesh -----Original Message----- From: Antonio Fiol Bonnín [mailto:fiol@w3ping.com] Sent: Monday, December 17, 2001 6:55 AM To: Nikola Milutinovic Cc: PostgreSQL JDBC Subject: Re: [JDBC] Servlet problems Hi! I'm afraid I don't speak JSP ;-) but I may siggest something. Can you try to flush every output you send? For every println(), if you use them, just place a flush() immediately after it. That WILL NOT solve your problem, and WILL degrade performance, but MAY help you find the source for your trouble. Once your problem is solved (I let you do the hard work), you can safely take them away. Good luck! Antonio Nikola Milutinovic wrote: >Hi all. > >I'm a bit confused here. I'm using Tomcat JSP/Servlet engine. I have a database in PostgreSQL, which holds a table describing an n-tree structure (hierarchy). I would like my JSP page to able to display (if parameter "print" is present) a complete sub-tree of a given node. This is a simple recursion. What I'm trying to do is make our coroporate E-Mail address book, so I have organizational units and users. > >Without recursion everything works fine, all the direct subnodes are displayed. When I use recusrion to include all subnodes, I get strange errors. > >First, it was killing the response in the middile of a table, in the middle of a row. Now it dies with "NullPointerException: next()". In other words, in one of the "ResultSet.next()" methods, it runs into a non existant ResultSet. > >I have tried putting a "breakpoint" (throwing a ServletException) on all lines of my JSP page. When there is a "breakpoint" I do not run into a NullPointerException. When I remove it, I have exception. > >My conclusion was that I get the error AFTER my JSP page starts emerging from the recursion (if it was inside recursion, it would have been stopped by the "breakpoint"). So, after a page calls itself once, the "insider" does it's thing and then exits and releases control to it's caller, the the exception occurs. > >I must admit that JSP page closes it's JDBC objects, I thought it would be OK, since the page creates them. > >This is the flow of events: > >OUDisplay.jsp [instance 1]: >---------------------------- > DriverManager.getConnection() > Connection.createStatement() > Statement.executeQuery() > while( ResultSet.next() ) { > ... > <jsp.include page="OUDisplay.jsp" /> > > OUDisplay.jsp [instance 2]: > ---------------------------- > DriverManager.getConnection() > Connection.createStatement() > Statement.executeQuery() > while( ResultSet.next() ) { > ... > } > ResultSet.close() > Statement.close() > Connection.close() > --------------[instance 2]- > > } > ResultSet.close() > Statement.close() > Connection.close() >--------------[instance 1]- > >The problem occurs inside the outer "next() {...}", which leads me to believe that Some objects in the "instance 1" get screwed up, but I don't know how. > >Any insight? > >Nix. > >---------------------------(end of broadcast)--------------------------- >TIP 3: if posting/reading through Usenet, please send an appropriate >subscribe-nomail command to majordomo@postgresql.org so that your >message can get through to the mailing list cleanly > >. > ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster
Nikola, I hope that my comments will help. I included some code examples on what to do and what will not work. Didn't you say you like the "Find the differences" game? You have a good example of it below. Take your time to read it, especially because I did not take a long time to write it and there may even be errors in it. Sorry if there are some. Good luck! >>I'm afraid I don't speak JSP ;-) but I may suggest something. >> > >The question is not really JSP related, I could test it in a Java standalone. I would hate to do so... I'm running low ontime/nerves. > We all suffer from that :-( >>Can you try to flush every output you send? >> > >Perhaps... So, JSP would print as far as it goes and I will see where it crashes? Hmm, not bad. The other alternative Iwas thinking of was Log4J package to do some logging along the way. > Try logging on the web page itself. This will help you MATCH the log messages to the web page output. It is dirty, ugly, and you must take the debugging out for any production tests, but it helped me find really ugly mistakes. Something else tou may try is logging as much information as possible about the state prior to an exception, in your catch blocks. Well, I suppose you knew that already... I am not sure whether Log4J is not a bit overkill for your present problem, I never used it yet. Maybe System.err.println() is easier, but that will probably be implementation-dependent. >>For every println(), if you use them, just place a flush() immediately >>after it. That WILL NOT solve your problem, and WILL degrade >>performance, but MAY help you find the source for your trouble. Once >>your problem is solved (I let you do the hard work), you can safely take >>them away. >> > >I'll try it. > >My dilema was whether JDBC calls were thread/recursion safe, I guess. > Stated that way, the answer is YES. Supposing you will be using a single Connecion object, as soon as you use different Statement objects for all your SELECT queries, which will give you different ResultSet objects, it is thread/recursion safe. *WHAT YOU SHOULD DO* : Statement stmt1 = conn.createStatement(); ResultSet rs1 = stmt1.executeQuery(query1); while(rs1.next()) { Statement stmt2 = conn.createStatement(); ResultSet rs2 = stmt2.executeQuery(query2); ... rs2.close(); stmt2.close(); } *YOU MAY ALSO DO* : Statement stmt1 = conn.createStatement(); Statement stmt2 = conn.createStatement(); ResultSet rs1 = stmt1.executeQuery(query1); while(rs1.next()) { ResultSet rs2 = stmt2.executeQuery(query2); ... rs2.close(); } *BUT YOU SHOULD NEVER TRY* : Statement stmt1 = conn.createStatement(); ResultSet rs1 = stmt1.executeQuery(query1); while(rs1.next()) { rs1 = stmt1.executeQuery(query2); ... rs1.close(); } *THIS WILL NOT WORK EITHER* : Statement stmt1 = conn.createStatement(); ResultSet rs1 = stmt1.executeQuery(query1); while(rs1.next()) { ResultSet rs2 = stmt1.executeQuery(query2); ... rs2.close(); } This last example will not work because stmt1.executeQuery(query2) WILL IMPLICITLY CLOSE rs1. In case you close a ResultSet implicitly, I strongly believe that the data set is destroyed, and thus ResultSet.next() will throw a NullPointerException. This kind of problem is always hard to find. Do not despair... Hope that helps, Antonio Fiol W3ping