Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Servlet becomming slow  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I dont know the mistake what i have made in my servlet, when i start execute my servlet it works fine well with good speed. but as i go on execute this program (say 40 times) then my servlet becomes slow and some times i get the exceptions like code included in the last)
Exception occured
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Cannot open any more tables. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source) at UpdateCategory.doPost(UpdateCategory.java:91) at javax.servlet.http.HttpServlet.service(HttpServlet.java:521) at javax.servlet.http.HttpServlet.service(HttpServlet.java:588) at sun.servlet.http.HttpServerHandler.sendResponse(HttpServerHandler.java:165) at sun.servlet.http.HttpServerHandler.handleConnection(HttpServerHandler.java:121) at sun.servlet.http.HttpServerHandler.run(HttpServerHandler.java:90) at java.lang.Thread.run(Unknown Source)
CODE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowCategory extends HttpServlet implements Constants{
Connection con;

public void init(ServletConfig config) throws ServletException {
super.init(config);
try{
Class.forName(Constants.DriverString);
con = DriverManager.getConnection(Constants.DataBaseURL);
}
catch(Exception e){
e.printStackTrace();
}
}// End of Init()

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
String cat = request.getParameter("category");
String randnum=request.getParameter("randnum");
String query;
String rand = null;

Cookie []cook = request.getCookies(); // Use of Cookie

String merid=(String)ht.get(randnum);//added

for(int i = 0; i < cook.length;i++){
if( cook[i].getName().equals(merid) ){
rand = (String)cook[i].getValue() ;
}
}

if( rand == null ) {
out.println("<html><head><title>Error</title></head>");
out.println("<body>");
out.println("<h1>Error! Invalid Login</h1>");
out.println("Your cookie value is invalid");
out.println("Try logging once again");
out.println("</body></html>");
out.close();
return;
}

String merchantid = (String) ht.get(rand);// change the merchantid value

try{
Statement stmt = con.createStatement();
query = "select name from category where categoryid = "+
cat+" ; ";

ResultSet rs = stmt.executeQuery(query);
String cat_name;
if( rs.next() ){
cat_name = rs.getString(1);
out.println("<html><head><title>Viewing Category: "+cat_name+"</title></head>");
}
else{
out.println("<html><head><title>Error!</title></head>");
out.println("<body>");
out.println("<h1>Error!</h1>");
out.println("The given cateogry code "+cat+" does not exist");
out.println("</body></html>");
return;
}// end of If



query = "select categoryid, name from category "
+" where supercategory = "+cat+" ; ";

rs = stmt.executeQuery(query);

out.println("<body>");

out.println("<center><h1>"+cat_name+"</h1></center>");
out.println("<br>");
out.println("<ul>");
int count = 0;


//
out.println("<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
out.println("<tr>");
out.println("<td width=\"24%\" BGCOLOR=\"#3399FF\">");
out.println("<font COLOR=\"#FFFF66\">");
while( rs.next() ){
count++;
out.println("<li>");
out.println("<a href="+Constants.HostName+"/servlet/ShowCategory?category="+rs.getString(1)+"&randnum="+randnum+" > "+rs.getString(2)+" </a>");
}// End of While
// 2 lines new code
out.println("<table width=\"98%\" >");
out.println("<tr>");
out.println("</td>");
out.println("<br><br>");
out.println(" <b>Like to move ");
out.println("<a href="+Constants.HostName+"/servlet/ShowCategory?category=0&randnum="+randnum+"> root </a> classification form </b>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</td>");
out.println("</font>");
out.println("<td width=\"76%\">");

//out.println("</ul>");
out.println("<center>");
out.println("<h2>Update Items</h2>");
out.println("<form action="+Constants.HostName+"/servlet/UpdateItem method = POST> ");
out.println("<input type = hidden name = category value = "+cat+" >");
out.println("<input type = hidden name = merchantid value = "+merid+" >");
out.println("<input type = hidden name = randnum value= "+randnum+" >");// added

//System.out.println(" Merchant id in Show Category merchantid:\t"+merchantid+"\n");
//System.out.println(" Merchant id in Show Category merid :\t"+merid+"\n");


out.println("<table colspan=2>");
out.println("<tr>");
out.println("<td align=right>Item Name : ");
out.println("<td><input type=text name = itemname>");// Name of item
out.println("</tr>");
out.println("<tr>");
out.println("<td align=right>Cost : ");
out.println("<td><input type=text name = cost>");// Cost of item
out.println("</tr>");
out.println("<tr>");
out.println("<td align=right>Delivery days : ");
out.println("<td><input type=text name=deliverydays");// No of Delivery Days
out.println("</tr>");
// code added for inserting figure
out.println("<tr>");
out.println("<td align=right>Image GIF Format : ");
out.println("<td><input type=file name=image");// Insert Image
out.println("</tr>");
/*File file1=new File("F:\\mtp\\varkala\\abc.txt");
try{
//file.mkdir();
file1.createNewFile();
}
catch(Exception e){
System.out.println(" Exception Error in Creating File:\t" + e);
}*/



out.println("</table><br>");
out.println("<input type = submit value = submit>");
out.println("</form>");
out.println("</center>");// end of the Update Items display

//

//3 lines new code

out.println("<hr>");
out.println("<br>");out.println("<br>");
out.println("<center>");
out.println("<h2>Add subcategory</h2>");
out.println("<form action="+Constants.HostName+"/servlet/UpdateCategory method = POST >");
out.println("<input type = hidden name = randnum value= "+randnum+" >");// added
out.println("<input type = hidden name = supercategory value = "+cat+" >");
//out.println("<input type=hidden name

out.println("<table>");
out.println("<tr>");
out.println("<td>Category Name : ");
out.println("<td><input type=text name = catname >");
out.println("</tr>");
out.println("</table><br>");
out.println("<input type = submit value = submit>");
out.println("</form>");
out.println("</center>");

out.println("</td>");
out.println("</tr>");
out.println("</table>");
//out.println("<table>");
out.println("</body></html>");
out.close();
}
catch(Exception exp) {
out.println("<html><head><title>Error</title></head>");
out.println("<body>");
out.println("<h1>Error! Exception occured</h1>");
exp.printStackTrace(out);
out.println("</body>");
out.println("</html>");
}


}// End of doGet

}// End of ShowCategory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
thanx for all, helping me.
prabhakar varkala
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just from the SQLException msg, it seems like every client that visits your serv creating a connection object. And you eventually ran out of resources. You may want to search this forum for connection pooling.

Bosun
 
varkala prabhakar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bosun,
I have been trying that servlet with single user and i got that error. I was inserting data with single user. kindly help me where the mistake is.
thanx
prabhakar varkala
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try when you are done with your resultset the line
con = null;
Garbage collection will then clear the connection.
 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should one use con.close() or con=null?
which is better?
[This message has been edited by Bruce Jin (edited October 09, 2001).]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!