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?