-1

I have created a servlet to access a database and giving response to a BB application...it was running fine during development...but after loading it on a tomcat server 6.0 after goining live the servlet has to be reloaded every morning on the tomcat server....after that it works fine during the whole day..but the next morning when i request something it gives a blank page as response and my server admin tells the servlet has to be reloaded ... other application hosted on the server are working fine and do not need a restart...

what might be the problem....

adding the code ..if it helps

    package com.ams.servlets;
    import java.io.*;
    import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

import com.cms.dbaccess.DataAccess;
import com.cms.utils.ApplicationConstants;
import com.cms.utils.ApplicationHelper;

import java.sql.ResultSet;
public class BBRequestProcessorServlet extends HttpServlet {
/**
 * 
 */String userString;
 private static final long serialVersionUID = 1L;
 String jsonString = "";
 ResultSet rs = null;
 Connection connection = null;
 Statement statement=null;

 public enum db_name
 { 
     //Test
     resource_management_db,osms_inventory_db; 

 }
 public void init(ServletConfig config)throws ServletException
 {
     super.init(config);

     System.out.println("Inside init");




 }

 public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException 
 {
     try{
         connection = DataAccess.connectToDatabase("xxx", connection);
         statement = DataAccess.createStatement(connection); 

         statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

         rs = statement.executeQuery("query is here");
     }
     catch(SQLException e)
     {
         e.printStackTrace();
     }
     String db =request.getParameter("db");
     System.out.println("DATABASE NAME :"+ db);
     if(db.equalsIgnoreCase("xxx")){ 
         //Call to populate JSONArray with the fetch ResultSet data
         jsonString = ApplicationHelper.populateJSONArray(rs);
     }
     response.setContentType(ApplicationConstants.JSON_CONTENT_TYPE);
     PrintWriter out = response.getWriter();
     out.print(jsonString);      
     out.flush();
     out.close();
     System.out.println("json object sent");
     try {
        rs.close();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 }   
 }

the only errors i could find was Jul 20, 2012 9:57:24 AM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(/usr/local/tomcat/apache-tomcat-6.0.20/webapps/MobileServlet /WEB-INF/lib/servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

sunil shetty
  • 75
  • 1
  • 8
  • what to look at in the logs...anything specific – sunil shetty Jul 20 '12 at 05:59
  • No. We can only guess why you're seeing a blank page. My guess would be that some connections are closed during the night, and that causes an exception which could be traced in the logs. But you could also have swallowed the exception in your code, which could lead to a blank page. Try to step through the code using a debugger the morning, to understand what happens. – JB Nizet Jul 20 '12 at 06:04
  • okay..will look .thanks for prompt reply – sunil shetty Jul 20 '12 at 06:12

2 Answers2

0

I am guessing and will be more clear after seeing your logs.

Its seems like you have putted your servlet-api.jar in the WEB-INF lib but its already in tomcat's lib.

Maneesh Kumar
  • 1,367
  • 2
  • 9
  • 13
0

The culprit is most likely the way how you handle external DB resources like the Connection. This problem can happen when you keep a DB Connection open all the time without closing it. When a DB Connection is been opened for a too long time, then the DB will timeout and reclaim it. This is most likely what was happening overnight.

You should redesign your DataAccess and BBRequestProcessorServlet that way so that you are nowhere keeping hold of Connection, Statement and ResultSet as an instance variable, or worse, a static variable of the class. The Connection should be created in the very same scope as where you're executing the SQL query/queries and it should be closed in the finally block of the very same try block as where you've created it.

By the way your jsonString should absolutely also not be declared as an instance variable of the servlet, it's not threadsafe this way.

See also:

As to the error which you're seeing in the log, you should definitely remove the offending JAR. See also How do I import the javax.servlet API in my Eclipse project?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555