Solved

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

Posted on 2004-09-10
7
398 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 106
configure dependency in POM for new database 3 34
ejb stateless example 2 27
eclipse console opening separately 2 29
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

830 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