image from database servlet problem -- pooling issue?

2002-03-10 Thread Matthew Kennedy

I am seeing strange behaviour with a servlet I wrote to return an image
from a database table. I'm not sure if it's struts related or not, but I
am relying on a datasource defined in the struts-config.xml. It might be
a silly mistake in the servlet code itself even.

I've attached the struts-config.xml which shows my datasource
definition, the web.xml where I define the servlet and provide an
initialization parameter, the servlet code (PhotoServlet.java) and a
simple test.jsp page.

The code I have seems to work most of the time (about 25/30 requests)
before failing a couple of times. The log.txt attachment shows the
result of doing:

   while true
   do 
 lwp-request \
 'http://localhost:8080/shona/servlets/photo?id=1class=thumbnail' \
 | wc -c 
   done

(just sequential requests)

The error.txt attachment shows the exception information generated when
a request fails. Note there seem to be two types of exceptions occuring
here: a low level jdbc error, and a no results returned error.

I've played with the maxCount and minCount parameters in the struts
datasource definition. Same results, except in the case of several
simultaneous requests to the servlet -- in which case raising maxCount
seems to prolong the interval between exceptions.

It's really been baffling me for a while now. What could be going wrong?
Am I seeing a limitation of Struts' connection pool perhaps? Even if
it's not struts related, suggestions would still be very welcome.

Matt


/*
 * PhotoServlet.java
 *
 * Created on February 23, 2002, 2:53 AM
 */

package mbkennedy.pshop.servlets;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.io.*;
import java.sql.*;

/**
 *
 * @author  mkennedy
 * @version
 */
public class PhotoServlet extends org.apache.struts.action.ActionServlet {

private static final long DEFAULT_BUFFER_SIZE =  1024;

/**
 * bufferSize to use when transfering blob to servlet response.
 */
private long bufferSize;

private DataSource dataSource = null;

/** 
 * Initializes the servlet.
 */
public void init(ServletConfig config) throws ServletException {
super.init(config);
String bufferSizeParam = null;

if ((bufferSizeParam = config.getInitParameter(bufferSize)) != null) {
// Servlet deployer intends to override default bufferSize
this.bufferSize = Long.parseLong(config.getInitParameter(bufferSize));
if (this.bufferSize = 0) {
throw new ServletException(bufferSize must be  0: bufferSize ==  + 
this.bufferSize);
}
} else {
this.bufferSize = this.DEFAULT_BUFFER_SIZE;
}
dataSource = this.findDataSource(default);
}


/** 
 * Processes requests for both HTTP codeGET/code and codePOST/code methods.
 *
 * @param request servlet request
 * @param response servlet response
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
  
// Check request parameters
if (request.getParameter(id) == null || request.getParameter(class) == null) {
throw new ServletException(invalid request parameters: id =  + 
request.getParameter(id) + , class =  + request.getParameter(class));
}
// Validate parameters
int paramId = 0;
try {
paramId = Integer.parseInt(request.getParameter(id));
} catch (NumberFormatException e_nf) {
throw (ServletException) new ServletException(id must be an integer: id =  + 
request.getParameter(id)).initCause(e_nf);
}
Connection conn = null;
try {
String paramClass = null;
conn = this.dataSource.getConnection();
PreparedStatement stmt1 = 
conn.prepareStatement(SELECT class FROM image_class WHERE class = ?);
stmt1.setString(1, request.getParameter(class));
if (stmt1.executeQuery().getFetchSize() == 0) {
throw new ServletException(class is invalid: class =  + 
request.getParameter(class));
} else {
paramClass = request.getParameter(class);
}
stmt1.close();

// At this point parameters have been validated for correctness (it 
// should be safe to process the request)

PreparedStatement stmt2 = conn.prepareStatement(SELECT image, mime_type  + 
FROM item_photos WHERE (item_id = ?) AND (image_class = ?));
stmt2.setInt(1, paramId);
stmt2.setString(2, paramClass);
ResultSet results = stmt2.executeQuery();
  
ServletOutputStream out = response.getOutputStream();
if (results.getFetchSize() == 0) {

Re: image from database servlet problem -- pooling issue?

2002-03-10 Thread SUPRIYA MISRA

try this on your jsp
%@ page buffer=2000kb /%


From: Matthew Kennedy [EMAIL PROTECTED]
Reply-To: Struts Users Mailing List [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: image from database servlet problem -- pooling issue?
Date: 10 Mar 2002 02:49:01 -0600

I am seeing strange behaviour with a servlet I wrote to return an image
from a database table. I'm not sure if it's struts related or not, but I
am relying on a datasource defined in the struts-config.xml. It might be
a silly mistake in the servlet code itself even.

I've attached the struts-config.xml which shows my datasource
definition, the web.xml where I define the servlet and provide an
initialization parameter, the servlet code (PhotoServlet.java) and a
simple test.jsp page.

The code I have seems to work most of the time (about 25/30 requests)
before failing a couple of times. The log.txt attachment shows the
result of doing:

while true
do
  lwp-request \
  'http://localhost:8080/shona/servlets/photo?id=1class=thumbnail' \
  | wc -c
done

(just sequential requests)

The error.txt attachment shows the exception information generated when
a request fails. Note there seem to be two types of exceptions occuring
here: a low level jdbc error, and a no results returned error.

I've played with the maxCount and minCount parameters in the struts
datasource definition. Same results, except in the case of several
simultaneous requests to the servlet -- in which case raising maxCount
seems to prolong the interval between exceptions.

It's really been baffling me for a while now. What could be going wrong?
Am I seeing a limitation of Struts' connection pool perhaps? Even if
it's not struts related, suggestions would still be very welcome.

Matt
 PhotoServlet.java 
 error.txt 
 log.txt 
 struts-config.xml 
 web.xml 
--
To unsubscribe, e-mail:   
mailto:[EMAIL PROTECTED]
For additional commands, e-mail: 
mailto:[EMAIL PROTECTED]




_
Join the world’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: image from database servlet problem -- pooling issue?

2002-03-10 Thread Eddie Bush

From what everyone says, the Struts connection pool is not robust enough to
handle much of a load.  I would seriously consider snagging Poolman off of
sourceforge.net and plugging that into your application to see if you still
have the same behavior.

HTH - GL

Eddie

- Original Message -
From: Matthew Kennedy [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Sunday, March 10, 2002 2:49 AM
Subject: image from database servlet problem -- pooling issue?


 I am seeing strange behaviour with a servlet I wrote to return an image
 from a database table. I'm not sure if it's struts related or not, but I
 am relying on a datasource defined in the struts-config.xml. It might be
 a silly mistake in the servlet code itself even.

 I've attached the struts-config.xml which shows my datasource
 definition, the web.xml where I define the servlet and provide an
 initialization parameter, the servlet code (PhotoServlet.java) and a
 simple test.jsp page.

 The code I have seems to work most of the time (about 25/30 requests)
 before failing a couple of times. The log.txt attachment shows the
 result of doing:

while true
do
  lwp-request \
  'http://localhost:8080/shona/servlets/photo?id=1class=thumbnail' \
  | wc -c
done

 (just sequential requests)

 The error.txt attachment shows the exception information generated when
 a request fails. Note there seem to be two types of exceptions occuring
 here: a low level jdbc error, and a no results returned error.

 I've played with the maxCount and minCount parameters in the struts
 datasource definition. Same results, except in the case of several
 simultaneous requests to the servlet -- in which case raising maxCount
 seems to prolong the interval between exceptions.

 It's really been baffling me for a while now. What could be going wrong?
 Am I seeing a limitation of Struts' connection pool perhaps? Even if
 it's not struts related, suggestions would still be very welcome.

 Matt







 --
 To unsubscribe, e-mail:
mailto:[EMAIL PROTECTED]
 For additional commands, e-mail:
mailto:[EMAIL PROTECTED]


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: image from database servlet problem -- pooling issue?

2002-03-10 Thread Adam Skobodzinski - subscriptions

It looks that the problem is in the way you check whether query returned
results:
--cut---
if (stmt1.executeQuery().getFetchSize() == 0) {
throw new ServletException(class is invalid: class =  +
request.getParameter(class));
--cut---
fetchSize is only a hint for the JDBC driver as to how many records should
it fetch from a database. It does not tell you actual number of records
returned from your query.
You servlet fails when there is no records for a given combination of id
and class parameters

To check if query returned any records simply use:
if (results.next()){
  write image to output
}else{
//no records found
}

If there is no image for a given combination id and class you should
probably returne 404 status code to the client instead of throwing an
exception.
Check you access logs to see which requests are causing those errors.
Regards,
Adam


- Original Message -
From: Matthew Kennedy [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Sunday, March 10, 2002 3:49 AM
Subject: image from database servlet problem -- pooling issue?


 I am seeing strange behaviour with a servlet I wrote to return an image
 from a database table. I'm not sure if it's struts related or not, but I
 am relying on a datasource defined in the struts-config.xml. It might be
 a silly mistake in the servlet code itself even.

 I've attached the struts-config.xml which shows my datasource
 definition, the web.xml where I define the servlet and provide an
 initialization parameter, the servlet code (PhotoServlet.java) and a
 simple test.jsp page.

 The code I have seems to work most of the time (about 25/30 requests)
 before failing a couple of times. The log.txt attachment shows the
 result of doing:

while true
do
  lwp-request \
  'http://localhost:8080/shona/servlets/photo?id=1class=thumbnail' \
  | wc -c
done

 (just sequential requests)

 The error.txt attachment shows the exception information generated when
 a request fails. Note there seem to be two types of exceptions occuring
 here: a low level jdbc error, and a no results returned error.

 I've played with the maxCount and minCount parameters in the struts
 datasource definition. Same results, except in the case of several
 simultaneous requests to the servlet -- in which case raising maxCount
 seems to prolong the interval between exceptions.

 It's really been baffling me for a while now. What could be going wrong?
 Am I seeing a limitation of Struts' connection pool perhaps? Even if
 it's not struts related, suggestions would still be very welcome.

 Matt







 --
 To unsubscribe, e-mail:
mailto:[EMAIL PROTECTED]
 For additional commands, e-mail:
mailto:[EMAIL PROTECTED]


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]