~{4tM74tDT~} PapayaHead wrote:
> Craig,
>
> Thanks for all the explanation about this. I think I need to seek
> further advice from here:)
>
> say, you have a connection pool to hold all the connections to the
> database for different threads. I was trying to write my own
> connection pool, and let other servlets share this information. how do
> you manage to do this?
>
> Thanks again!
>
> Emily
>
To follow my suggestion, consider this approach:
(1) Define a servlet with intiialization parameters describing the
database connection you want, and tell your servlet engine
to load it at startup time. Note that this servlet need not do
anything useful in terms of HTML output -- all you are after
is the fact that its init() method gets called as the servlet engine
is loaded.
(2) In the init() method of that startup servlet, include code like this:
DbConnectionPool pool = new DbConnectionPool(....);
getServletContext().setAttribute("myConnectionPool", pool);
(3) In other servlets that need to access the connection
pool (to allocate themselves a thread):
DbConnectionPool pool =
(DbConnectionPool)
getServletContext().getAttribute("myConnectionPool");
Connection conn = pool.allocate(); // Or whatever
... use the connection ...
pool.deallocate(conn); // Or whatever
There are several examples of connection pool objects lying around the net.
The details of what method calls they support will change a little, but the
basic idea of sharing a single object across multiple servlets is the
important point of this example.
By the way, a missing feature of the servlet context attributes is a graceful
way to (in this case) close down the connection pool when the servlet engine
wants to exit. I would not be surprised to see something similar to the
HttpSessionBindingListener mechanism in a future version of the servlet API,
to make this possible.
Craig
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html