The problem you are trying to solve is not propely solved by trying to limit the container to a single servlet instance (which you probably cannot control anyway, unless your container allows some sort of proprietary setting).

The way you should be thinking to solve this problem is a connection pool.

Your servlet needs to be written in a thread-safe manner anyway, and that allows the same servlet instance to handle multiple requests at the same time. Having a connection pool underneath that will allow you to share your database connections efficiently.

There are numerous options for connection pooling... the first thing you'll probably want to look into is what your container itself offers in this area. Most containers have some sort of pooling facility available. Another option is to build on top of a persistance framework like Hibernate or iBATIS, both of which offer some degree of pooling support. There are libraries for doing it as well, DBCP (a Jakarta Commons package) for instance. You could always write your own, it isn't too tough, but for something that has been done to death you are definitely better off not bothering.

Of course, since your posting to the Tomcat list, I can tell you that Tomcat offers such support:

http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html

Frank


James Black wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Caldarale, Charles R wrote:

From: James Black [mailto:[EMAIL PROTECTED] Subject: re: making a singleton servlet

I am going to make my servlet be static, with the hope that it will only have one instance running, regardless of how many clients connect to it.


What do you mean by "servlet be static"?  What syntactical construct are
you employing?

If you mean using static fields in your servlet class, then you will
have to make use of synchronization clauses to insure concurrent
requests are serialized.  It's my understanding that the container
(Tomcat or whatever) is free to process as many requests in parallel as
needed, as well as create multiple servlet instances - see the servlet
spec.

What problem are you trying to solve?


  My plan is to try:
public static class SomeServlet extends HttpServlet {  ... }

  That way there should only be one servlet.

  I am writing a servlet to save grades to a database, but,
unfortunately, instructors will procrastinate like students do. So, I
expect that 2000+ instructors will submit their grades in the last hour
or so, before the deadline.  If each instructor had their own db
connection then the system will be useless, as students won't be able to
get connections, since all the connections will be used up.

  For the first test I want to limit them to only one connection that
will read from an input queue, and just process all the grades. Later it
may be bumped up to 20-50 connections, to speed it up.

  That is the basic problem I am trying to solve.

  I am actually using XmlHttpRequest to connect to the servlet so it
doesn't lock up the browser.

- --
Corruptisima republica plurimae leges. [The more corrupt a republic, the
more laws.]
Tacitus from Annals III, 116AD
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)

iD8DBQFDlkneJ/zyYkX46joRAlfpAJ0cdiTxXrSSdLfZ3znd63dSJesvJACgiFes
PfU+fddjZNUPTT1gq0Ft69g=
=tKjO
-----END PGP SIGNATURE-----

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





--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: [EMAIL PROTECTED]

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

Reply via email to