Servlet hang when performing to much insert function
michael yue
Ranch Hand
Joined: Nov 20, 2003
Posts: 204
posted
0
hi, I have done servlet that perform inserting into a table. The problem is that when inserting to much data in a loop, the connection just hangs there. My servlet is like this. When user put in 1 in first number and 100 in last number and AA in the prefix field, the servlet will insert records from AA1 till AA100 in a loop. It still works fine if the amount to be inserted is less than 100 but the connection hangs up when the amount exceed 200 or sometimes 150. I need this servlet to sometime insert more than 1000 records. Anyone have any ideas to improve on the code. Below is the for loop code for insert records. Is too much of insert records at once too overwhelming for the database connection? for(int count=intfirstno; count<=intlastno; count++){ String strnum = String.valueOf(count); String covernoteno = prefixno+strnum; strsql = "SELECT cncvnoteno FROM covernote WHERE cncvnoteno=?";
Rollin, my suggestion is to do the inserting work on the database side with a stored procedure. You are causing too much network traffic from your Servlet to the Database. If you know the begining and end, then just pass those two variables to a stored procedure that will do the rest of the work. I guarantee that you will find it sooooo muccchhh faster. Mark
The database I am using is DB2 using JDBC connection on windows98 OS. I currently close the connection and not the statement. Is this a possible cause of problem?
michael yue
Ranch Hand
Joined: Nov 20, 2003
Posts: 204
posted
0
I followed mike's advise but still encounter hangs. My jsp send info to the servlet and it stops there without closing connection and my codes clearly closes connections in finally. In short my servlet just stop working when the amount of records inserted is too big (>300 rec). below is my updated codes. Please help me someone. As for stored procedure, it is not feasible for my project and maybe will face problems since it is db2 database. Thanks String prodcode = req.getParameter("prodcode"); String firstno = req.getParameter("firstno"); String lastno = req.getParameter("lastno"); String xeffdate = req.getParameter("effdate"); String s1=xeffdate.substring(0,2); String s2=xeffdate.substring(3,5); String s3=xeffdate.substring(6,10); String effdate=s2+"/"+s1+"/"+s3;
Might it simply be that your servlet response is timing out ?
Do the database updates actually occur?
I still don't know what you mean by "In short my servlet just stop working"
In what WAY does it stop working? Does the browser throw a 404? Does it not do the 'send redirect', so your JSP page submitting to the servlet seems to take forever? Is anything printed to the console from the catch statement?
That your finally block is being run makes me think the updates are all working, but your response time (because you're doing over xxxx updates) is taking too long, and by the time the servlet gets around to saying "response.sendRedirect()" the browser has since moved on to other things (like "I'm not waiting any more.. page not found!")
Thanks for all the reply. I managed to catch the exception. It prints out: consubCOM.ibm.db2.jdbc.app.DB2Exception: [IBM][CLI Driver][DB2/6000] SQL0954C Not enough storage is available in the application heap to process the statement. SQLSTATE=57011 I think it means not enough of resources on my hardware part. Am I wrong? How can I make sure that I can catch this particular exception and redirect to another error page? Can the code below works for catching this exception cause I think comparing the error code string is too long and maybe not accurate. Thanks
String err = e.toString(); if err.equals(COM.ibm.db2.jdbc.app.DB2Exception: [IBM][CLI Driver][DB2/6000] SQL0954C Not enough storage is available in the application heap to process the statement. SQLSTATE=57011) { res.sendRedirect("http://"+host+"/CVN/error2.jsp"); } }
brams mun
Greenhorn
Joined: Nov 27, 2003
Posts: 25
posted
0
Rollin, fine, finally u got the exception and discuss over it with the administrator and for the next insertion use beans instead of working on the servlets and use batch updates instead of prepare statements ok, take care.
'on the hardware part'.. if you mean your database, then I think you're partly right.
You might have plenty of available memory on your database server, but you will have to re-configure a setting to make this monster query run. (or, try alternate approaches, two of them are mentioned in this thread)