Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conrolling number of servlet instances 1

Status
Not open for further replies.

BillyKrow

Programmer
Aug 27, 2001
67
0
0
US
I'm running a web app on a WebSphere server and need to control the maximum number of servlet instances. Is there any way through a WebSphere setting, web.xml or direct code to control the maximum number of servlet instances? By putting System.out message in the init method, it appears to create five instances.
 
The servlet API spec states that only one instance of a servlet should ever exist.
Of course, if you map several different servlets to the same class, then you will get what would appear to be multiple instances.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Actaully the number of instances depends on the application server and whether or not your implementing the SingleThreadModel. I'm using the SingleThreadModel do to the fact that the normal threaded environment will not work since I want each servlet call to act like a single transaction tied to specific instance data.

It appears you can control this in WebSphere by setting the paramater singlethreadmodel.initialpoolsize in the context of the Web App.

Now I'm trying to figure out how to set the maximum number of instances of a Message Driven Bean. Any help here would be appreciated.
 
Using the SingleThreadModel should not actually affect the number of instances. Using SingleThreadModel just means that all requests are thread safe (ie queued in an extraneous synchronized block) - because due to the the fact that only one instance of a servlet is allowed, servlets are inherently not thread-safe.

There is an interesting passage in the servlet API spec :

In a multithreaded environment, most servlets must be written to handle multiple
concurrent requests. The exception is a servlet that implements the
SingleThreadModel interface. Such a servlet will execute only one request thread
at a time.
A servlet responds to a client request according to the servlet engine’s mapping. A
mapping pairs a servlet instance with an URL to which the servlet returns data, for
example, HelloServlet with /hello/index.html.
However, a mapping might pair an URL with more than one servlet instance. For
example, a distributed servlet engine running on more than one server might have a
servlet instance running on each server, to balance the processing load. As a servlet
developer, you cannot assume that a servlet has only one instance.

This in my mind, sounds like if you map one URL to one class - then that class could be instantiated multiple times.

However according to this passage :

public void init(ServletConfig config) throws ServletException;
The servlet engine calls the init method exactly once on a servlet, after the servlet
is instantiated but before it is placed into service. The init method must exit
successfully before you can call the service method.
If the init method throws a ServletException, you must not place the servlet
into service. If the init method does not complete within the timeout period, you
can assume the servlet is nonfunctional and not in service.

... one would assume that certainly init() should only be called once per JVM - which kind of goes against what you are saying is occuring in WebLogic.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I wasn't really conviced that SingleThreadModel effected the number of instances but I could easily test this. I was just going by a couple articles I had read. Mainly, what I have read is that it depends on the J2EE server.

However, since the init is getting called five times, this proves that there is five instances whic is what I want; multiple non threaded instances.

Still researching controlling the number of MDB's in WebSphere.
 
Well, putting aside what the spec says, and whether or not Weblogic implements it ...

... you could easily control it with a static variable in the servlet that indicates whether the servlet has been loaded before - ie :

private static loaded = false;

...

if (!loaded) {

// load stuff ...

loaded = truel

}

....

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top