Solved

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

Posted on 2004-09-10
7
396 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 125 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 54
arguments to jar 5 26
SHA2 certs for IIS AND Java? 2 93
hibernate example using maven 12 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

809 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