Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Servlet - jdbc -oracle - on oc4j - runs once and needs server restart to run again????

Hi all,

I'm running a servlet on either oc4j or jserv and get the same symptom - the servlet runs once and brings back data.  On running again, it comes back blank with no data returned?  The only way to get it to run again is to restart the java server?  

The program also runs another servlet that produces a blob, but that does not intefere with the running of this pgrm. It is contained in the img src html call.

 I have taken the same queries and formatted a plsql web page to do the same job, and that runs fine.  So I'm sure that is is not the sql thats causing the problem.

Code:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import oracle.jdbc.driver.*;

public final class showStds extends HttpServlet
{
  Connection conn;
  ResultSet rset;
  ResultSet rset1;
 
  public showStds () {
    String url = new String("jdbc:oracle:thin:@***********:****:*******");
    Properties p = new Properties();
    p.put("user", "query");
    p.put("password", "query");
   
    try {
     Class.forName("oracle.jdbc.driver.OracleDriver");
     conn = DriverManager.getConnection(url, p);
     System.out.println("Connected url & properties");
    }
    catch (Exception e) {
      System.out.println("Not connected: " + e.getMessage());
    }
  }

  public void destroy() {
    try {
      conn.close();
      rset.close();
      rset1.close();
     
      System.out.println("Disconnected");
    }
    catch (Exception e) {
      System.out.println("Disconnecting error: " + e.getMessage());
    }
  }
 

  private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

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

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {

    String pgrm = "";
    String year = "";    
    String term = "";    
    String temp = "";

     temp = request.getParameter("pm");  //where the param is in form       200102>6789>44
   
    int len = temp.length();
    int yrpos = temp.lastIndexOf('>');
    int pgrmpos = temp.indexOf('>');
    String yr = temp.substring(yrpos + 1);
    String pm = temp.substring(pgrmpos + 1, yrpos);
    String tm = temp.substring(0, pgrmpos );
 
    System.out.println(temp);
    System.out.println("the length of temp is: " + len);
    System.out.println("the position of program is: " + pgrmpos);
    System.out.println("the position of year is: " + yrpos);
   
    System.out.println("the value of year is: " + yr);    
    System.out.println("the value of program is: " + pm);
    System.out.println("the value of term is: " + tm);

    pgrm = pm;
    year = yr;
    term = tm;
   
    StringBuffer stringbuffer = new StringBuffer(1024);
    StringBuffer stringbuffer1 = new StringBuffer(4000);    // very big query

    stringbuffer.append( "select *************, " +
                         "*************, " +
                         "**************, " +
                         "**************, " +
                         "***********" +
                         "from ********, ********, ********, ******** " +
                         "where ******** = '");
    stringbuffer.append(pgrm);
    stringbuffer.append("' ******** = '");
    stringbuffer.append(year);
    stringbuffer.append("' and  ******** = ******** ");
   
    System.out.println(stringbuffer.toString());
    stringbuffer1.append(   NOTE - this is an enormous query that takes a couple of seconds to run - but does run fine   );

    rset = null;
    try {
      String q1 = stringbuffer.toString();
      rset = conn.createStatement().executeQuery(q1);
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    rset1 = null;
    try {
      String q2 = stringbuffer1.toString();

      rset1 = conn.createStatement().executeQuery( q2);
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<TITLE>a title for the page</TITLE>");
    out.println("<head><title>show all clients</title></head>");
    out.println("<body>");
    out.println("<BODY background=\"/brg.jpg\">");
    out.println("<P>");
    out.println("<TABLE BORDER=\"0\" WIDTH=\"100%\">");
    out.println("      <TR>");
    out.println("            <TD WIDTH=\"100%\">");
    out.println("                  <P ALIGN=\"CENTER\"><B><FONT SIZE=\"4\" COLOR=\"black\">Client Details</FONT></B>");
    out.println("            </TD>");
    out.println("      </TR>");
    out.println("</TABLE>");
    out.println("</P>");
    out.println("<P>");
    out.println("<HR ALIGN=\"CENTER\">");
    out.println("<TABLE BORDER=\"0\" WIDTH=\"100%\">");

    try{
      while ( rset.next() ) {
        out.println("  <TR>");
        out.println("  <FONT >");       
        out.println("   <TD WIDTH=\"8%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"3\" >  " + rset.getString(1) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"23%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"3\" >  " + rset.getString(2) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"16%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"3\" >  " + rset.getString(4) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"16%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"3\" >  " + rset.getString(3) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"16%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"3\" >  " + rset.getString(5) + "</FONT></TD>");
        //out.println("   <TD WIDTH=\"20%\"><B><FONT FACE=\"Arial, Helvetica\">  " + rset.getString(1) + "</FONT></B></TD>");
        out.println(" </TR>");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
   
    out.println("</TABLE>");
    out.println("<HR ALIGN=\"CENTER\">");

    out.println("<TABLE BORDER=\"0\" WIDTH=\"100%\">");

    try{
      while ( rset1.next() ) {
        out.println("  <TR>");
        out.println("  <FONT >");       
        out.println("   <TD WIDTH=\"8%\"><img src=\"http://www.myserver.ie:7777/j2ee/servlet/showblob?num=" + rset1.getString(1) + "\" width=\"70\" height=\"70\"><br></TD>");
        out.println("   <TD WIDTH=\"8%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"1\" >  " + rset1.getString(1) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"15%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"1\" >  " + rset1.getString(2) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"5%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"1\" >  " + rset1.getString(4) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"5%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"1\" >  " + rset1.getString(5) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"25%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"1\" >  " + rset1.getString(6) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"20%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"1\" >  " + rset1.getString(7) + "</FONT></TD>");
        out.println("   <TD WIDTH=\"10%\"><FONT FACE=\"Arial, Helvetica\" SIZE=\"1\" >  " + rset1.getString(3).substring(0,10) + "</FONT></TD>");        
        out.println(" </TR>");
     }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
   
    out.println("</TABLE>");

    out.println("<P>");
    out.println("</body></html>");

    out.flush();
   
    out.close();
    destroy();
 
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    String pgrm = "";
    String var1 = "";
    String var2 = "";
    String var3 = "";

   doGet(request,response);

    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>show clients</title></head>");
    out.println("<body>");
    out.println("<p>The servlet has received a POST. This is the reply.</p>");
    out.println("</body></html>");
    out.close();
    destroy();
  }
}
0
pmkenna
Asked:
pmkenna
1 Solution
 
Venci75Commented:
what happens if you rename your destroy() method?
0
 
girionisCommented:
Do the logs say anything?
0
 
Venci75Commented:
also - are there any exceptions or error messages printed?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimYatesCommented:
Also, put your call to "destroy" in a finally block, so that you always close everything...

otherwise, you may have troubles...

(I agree with Venci75, that calling it another name other than destroy is probably a good idea -- I think you are overriding a method in HttpServlet)...
0
 
pmkennaAuthor Commented:
Hi,

having changed the destroy method to closeall(), still no change.  I'm getting the same symptom with the embedded JDeveloper oc4j server.  Not that the servlet name is coreimg.showStds. Message from server is:

java.sql.SQLException: Closed Connection

      void oracle.jdbc.dbaccess.DBError.throwSqlException(java.lang.String, java.lang.String, int)

      void oracle.jdbc.dbaccess.DBError.throwSqlException(int, java.lang.Object)

      void oracle.jdbc.dbaccess.DBError.throwSqlException(int)

      java.sql.Statement oracle.jdbc.driver.OracleConnection.privateCreateStatement(int, int, java.lang.String)

      java.sql.Statement oracle.jdbc.driver.OracleConnection.createStatement()

      void coreimg.showStds.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.invoke(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.forwardInternal(javax.servlet.ServletRequest, javax.servlet.http.HttpServletResponse)

      boolean com.evermind.server.http.HttpRequestHandler.processRequest(com.evermind.server.ApplicationServerThread, com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse, java.io.InputStream, java.io.OutputStream, boolean)

      void com.evermind.server.http.HttpRequestHandler.run(java.lang.Thread)

      void com.evermind.util.ThreadPoolThread.run()

java.sql.SQLException: Closed Connection

      void oracle.jdbc.dbaccess.DBError.throwSqlException(java.lang.String, java.lang.String, int)

      void oracle.jdbc.dbaccess.DBError.throwSqlException(int, java.lang.Object)

      void oracle.jdbc.dbaccess.DBError.throwSqlException(int)

      java.sql.Statement oracle.jdbc.driver.OracleConnection.privateCreateStatement(int, int, java.lang.String)

      java.sql.Statement oracle.jdbc.driver.OracleConnection.createStatement()

      void coreimg.showStds.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.invoke(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.forwardInternal(javax.servlet.ServletRequest, javax.servlet.http.HttpServletResponse)

      boolean com.evermind.server.http.HttpRequestHandler.processRequest(com.evermind.server.ApplicationServerThread, com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse, java.io.InputStream, java.io.OutputStream, boolean)

      void com.evermind.server.http.HttpRequestHandler.run(java.lang.Thread)

      void com.evermind.util.ThreadPoolThread.run()

java.lang.NullPointerException

      void coreimg.showStds.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.invoke(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.forwardInternal(javax.servlet.ServletRequest, javax.servlet.http.HttpServletResponse)

      boolean com.evermind.server.http.HttpRequestHandler.processRequest(com.evermind.server.ApplicationServerThread, com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse, java.io.InputStream, java.io.OutputStream, boolean)

      void com.evermind.server.http.HttpRequestHandler.run(java.lang.Thread)

      void com.evermind.util.ThreadPoolThread.run()

java.lang.NullPointerException

      void coreimg.showStds.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

      void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.invoke(javax.servlet.ServletRequest, javax.servlet.ServletResponse)

      void com.evermind.server.http.ServletRequestDispatcher.forwardInternal(javax.servlet.ServletRequest, javax.servlet.http.HttpServletResponse)

      boolean com.evermind.server.http.HttpRequestHandler.processRequest(com.evermind.server.ApplicationServerThread, com.evermind.server.http.EvermindHttpServletRequest, com.evermind.server.http.EvermindHttpServletResponse, java.io.InputStream, java.io.OutputStream, boolean)

      void com.evermind.server.http.HttpRequestHandler.run(java.lang.Thread)

      void com.evermind.util.ThreadPoolThread.run()

Disconnecting error: null

0
 
Venci75Commented:
actually - you create a DB conenction in your contructor - but close it after the first request - in the doGet method :)

what you should do is to create it in the init() method and close it in the destroy() method. Also - you shoudn't call these methods from the doGet method
0
 
pmkennaAuthor Commented:
many many thanks.  Hope I can return the favour sometime.
Pat
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now