Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-10
7
Medium Priority
?
409 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
ID: 12026774
what happens if you rename your destroy() method?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12026786
Do the logs say anything?
0
 
LVL 9

Expert Comment

by:Venci75
ID: 12026806
also - are there any exceptions or error messages printed?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Expert Comment

by:TimYates
ID: 12026838
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
ID: 12026979
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 500 total points
ID: 12027074
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
ID: 12028212
many many thanks.  Hope I can return the favour sometime.
Pat
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

972 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