Java servlets and database pooling

Just by looking at examples of pooling in a network, they all implement pooling based on each servlet. Thus, each servlet has its own database connection pool. My question is: why is this preferable to the global db connection pool? Because the global pool seems more efficient than every servlet pool.

Also, since I am thinking about implementing such a pool. Is there a way to initialize a class before servlets (I use jetty btw)? I am just starting to develop a servlet, but it may seem useful for other things, such as configuration. Otherwise, I was just going to use some kind of singleton pattern.

+4
source share
5 answers

1) I would say that the standard practice is to create a connection pool as a JNDI resource in a context descriptor that would not be a task for each servlet.

2) You will want to implement and declare a ServletContextListener .

+7
source

Honestly, I really don't know what you're talking about. Perhaps you could provide the samples you were looking at.

For me, the โ€œrealโ€ connection pool should be completely Servlet agnostic, and using the connection pool based on each servlet is more detailed usage information (and bad IMO). Just have a look at DBCP or c3p0 for good examples of connection pools that you can use in an "out of container" context.

Also note that most (if not all) containers actually provide their own implementation of the connection pool (once based on previous examples), and I see no reason to use them. The standard way to use them is to get a DataSource registered in the JDNI naming service. Today, DataSouce in most cases introduced through the IoC. In the old days, the Service Locator pattern was often used.

For Jetty, see Sample Data Sources in the documentation.

+3
source

My question is: why is this preferable to something like a global db connection pool?

This is not true! All servlets must exchange connections in the pool!

Also, since I am thinking about implementing such a pool.

There are already ways (Spring IoC) to introduce container-managed federated connections.

. Is there a way to initialize a class before servlets (I use jetty btw)?

An IoC container can load every resource and servlet!

You can certify servlets in spring by registering Spring dispatcherServlet and xml containing mappings and servlets as bean definitions!

Use the following definitions if you only want to enter the connection and leave the rest of the servlet like this:

 <bean name="simpleServletHandlerAdapter" class="org.springframework.web.servlet.handler.SimpleServletHandlerAdapter" /> <bean name="simpleServletPostProcessor" class="org.springframework.web.servlet.handler.SimpleServletPostProcessor" /> 
+2
source

Very often, for web containers, JNDI is used to provide ConnectionFactory (or similar), which uses the connection pool, but the way it is configured is not standardized.

Please see http://docs.codehaus.org/display/JETTY/DataSource+Examples for how to do this with Jetty.

+2
source

One of the advantages of having a connection pool on a servlet is that if one of the servlets is not working properly and uses all of its available connections, the other servlets will still have connections available in their separate pools. This improves the stability of your application.

0
source

All Articles