Solved

connecting pool and session

Posted on 2004-10-01
1
189 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There is no doubt that cloud is gaining importance. Many of you must have read about this technology and its growing importance. More and more organisations are embracing this technology not forgetting start-ups. The process begins by dipping …
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now