Solved

connecting pool and session

Posted on 2004-10-01
1
191 Views
Last Modified: 2010-04-01
is it a good idea to save the db connection getting from the pool in the session? I have a servlet that does not work well:
1) the servlet get the db-conn from the pool then put it in the session: with doing so, it enables other beans making use of the db-conn
2) the db-conn is bieng closed at the and of the session

please ..ideas

thanks


0
Comment
Question by:aimueller
1 Comment
 
LVL 7

Accepted Solution

by:
lhankins earned 50 total points
ID: 12212440
Storing the connection in the session is a bad idea...

First, connections are not serializable (only things which are serializable should be put in the session).   The servlet container can serialize sessions out to disk at any time (and in scenarios where you're doing clustering, objects in the session may be replicated to other members of the cluster).

Second - the whole idea of  a JDBC connection pool is to use a smaller number of connections to service a larger number of users.   For example, you may have a connection pool of size 100, that you use to service a concurrent user population of size 200.      For this to work, you should grab a connection from the Connection pool, do something with it, then "close" the connection (which doesn't really close it, just sticks it back in the pool).

For pure webapps, you can grab a connection at the beginning of a request, and close it once you're done servicing the request.    One common way to do this in a pure webapp is to implement your own ServletRequestFilter (grab connection just before starting the request, close it at the end).   If you're using struts, you could also add your own BaseAction class which does this sort of thing (and exposes it to all concrete descendants).  

Here's an example of how to do something similar with a Hibernate Session object (a Hibernate Session is analagous to a JDBC Connection - in fact, it wraps a JDBC Connection):

http://www.hibernate.org/43.html
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question