• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How do I design Java code (Servlet) for Connection Pooling?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Coming from this link, I understand why Connection Pooling is a good thing.

If we analyze the sequence of steps involved in a typical database connection life cycle, we'll understand why:

Opening a connection to the database using the database driver
Opening a TCP socket for reading/writing data
Reading / writing data over the socket
Closing the connection
Closing the socket


From my point of understanding, main idea is to reuse connection in our web app and not create new ones. But, can you help me how to best design it in my code (inside Servlets)?

Just so we are on the same page, notice that every request is served in separate thread, while Servlet is initialized only once. Threads share every instance variable of Servlet.

1. Do I have one connection from pool for each request?

2. Do I have one connection shared between all requests?

---
In these examples I removed try/catch block for cleaner code.

Can someone nicely explain to me which "design" I should choose? One Request = One Connection OR One Servlet = One Connection?
Meaning do I take connection from pool on each user request, or I take a single connection from pool that should be used for every request on that Servlet?
 
Saloon Keeper
Posts: 6995
164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not design you own connection pool, but use the one built into the servlet container. See https://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html for how to use Tomcat's.
 
Stefan Jankovic
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well thanks. I will check it out in detail for sure.

But can you tell me based on my question, how do I organize my code? Please pay attention to original question once again.

Do I "serve" each user a separate connection or users can share connection? How to code it?
 
Saloon Keeper
Posts: 13014
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you organize your persistence layer as a set of repositories, you would use a database connection per method call: https://coderanch.com/t/717146/databases/Java-provide-automatic-connection-pooling

The connection pool is there to make sure that opening and closing multiple connections (even within the same request) isn't prohibitively expensive.
 
reply
    Bookmark Topic Watch Topic
  • New Topic