Solved

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

Posted on 2004-09-10
7
391 Views
Last Modified: 2013-11-24
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
Comment
Question by:pmkenna
7 Comments
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
what happens if you rename your destroy() method?
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Do the logs say anything?
0
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
also - are there any exceptions or error messages printed?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
 

Author Comment

by:pmkenna
Comment Utility
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
 
LVL 9

Accepted Solution

by:
Venci75 earned 125 total points
Comment Utility
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
 

Author Comment

by:pmkenna
Comment Utility
many many thanks.  Hope I can return the favour sometime.
Pat
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now