I just wonder which way is better to provide session management. For example, if i have a session that interact closely with database, should i use Javabean object to hold the connection throughout the session by using scope="session"; or i should use a servlet to open a connection? If i use bean, the object+conncetion will reside in the memory as long as the session does, so no closing and reopening process require for jdbc. Can i do the same thing in pure servlet? My main concern is the connection to database, how can i use the same connection in servlet throughout the session? Any feedback is welcome.
You should NOT try to preseve the connection data in a session. With the default timeout of 30 minutes, you could end up with a lot of useless connection objects lying around. Manage connections with a connection pool - there are plenty of examples around. A connection pool is by far the best way to cut down on the overhead of creating connections. Bill
So, you are saying open/request a connection to database in the beginning of the servlet/JSP and close/return the connection at the end of the servlet/JSP. Am i correct? Can i do the same way in javabean with i use jsp? Thanks alot.
Author and all-around good cowpoke
Joined: Mar 22, 2000
"So, you are saying open/request a connection to database in the beginning of the servlet/JSP and close/return the connection at the end of the servlet/JSP. Am i correct?" Exactly - that way you leave the servlet in a clean state and it won't matter if the user never comes back. "Can i do the same way in javabean with i use jsp?" Sure, but be sure your exception catching is set up correctly so that the connection is always returned to the pool, no matter what exception occurs. Bill
Joined: Jun 21, 2001
I am clear with the concept of connection pooling. However, there is one thing confuses me. If i have a jdbc connection pooling class to handle all the connections to database, when a request from UserA hit the servlet/jsp and the connection class object is instantiated, let say 10 conncections are held by this instance of connection pooler, it maintains all the services requested by the servlet/jsp from UserA. Now another user, UserB accesses the servlet from another places, will another instance of the connection pooler get instanciated, and thus another 10 connections to database are created to serve UserB. Or, the same instance of connection pooler that serves UserA will be used to serve UserB? If is the later case, does it mean only one instance of connection pooler reside in the stack all the time? Thanks for clearing my doubt on this issue in advance.
It depends on your implementation, if you want the pool to have a page scope or application scope or whatever. If you instantiate a pool as a class variable, then all users that access that particular servlet will use it.