This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Servlets and the fly likes store Connection in HttpSession ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "store Connection in HttpSession ?" Watch "store Connection in HttpSession ?" New topic
Author

store Connection in HttpSession ?

heiner weilandt
Ranch Hand

Joined: Sep 10, 2002
Posts: 46
hi,
i have to save a jdbc-connection into a httpsession-object. if i do this in one servlet (save and restore) it works fine. if i do this with two servlets or http-requests the restore works not. what do i wrong ?
i know it's not a fine way to do this, but i have to do it !



thanks a lot for help
heiner

[ November 28, 2002: Message edited by: heiner weilandt ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12267
    
    1
Storing a connection is a session is a REALLY BAD IDEA. No good can come of this for a huge variety of reasons. Why can't you use a connection pool?
Bill


Java Resources at www.wbrogden.com
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

William is 100% correct (surprise surprise )
NO to putting the Connection on the session. As he said, there are soooo many reasons this is a bad thing, including the fact that it claims resources on the database that you aren't using and you can't close the Connection if the user never comes back.
If Connection Pooling isn't an answer, even creating and discarding Connections when required is an inefficient but better solution.
Dave
heiner weilandt
Ranch Hand

Joined: Sep 10, 2002
Posts: 46
hi,
yes, it' an bad idea. but i have to do this, because some of my bosses said, we have to realize commit-7rollback and logging over more than two http-request. i don't want to use ejb's ......
Kees van Oosterhout
Ranch Hand

Joined: Jul 08, 2002
Posts: 34
No, no, no. !!
A DBConnection should not be placed in the session object. Placing this in the context is definitly the way to go. The DBConnection will be accesible by all servlets in all requests. Off course the results returned from the connection can be placed in a session, wether or not in a self-defined data structure.
Browse in the J2EE Api to find more information about the context, for a quick solution.


"...you've got to ask yourself one question, Do I feel lucky?"
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5369
Originally posted by heiner weilandt:
[QB][/QB]

Have you enabled session tracking ??
just curious .
AW use getSession(false) in second servlet.
[ November 29, 2002: Message edited by: Ravish Kumar ]

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12267
    
    1
yes, it' an bad idea. but i have to do this, because some of my bosses said, we have to realize commit-7rollback and logging over more than two http-request. i don't want to use ejb's ......

Why does that require that you save the connection? (I don't use JDBC much so I'm just curious!)
Bill
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

What might be happening is: Your session is serialized, which JDBC Connections don't do so well. So when you try to retrieve it, the object is still there, but it's now "dead".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: store Connection in HttpSession ?
 
Similar Threads
apache axis fault and more questions about returning "array of classes"
JSP and a role on WebSphere 4.02 (iseries V5R1)
Connection Objects & Scope
JSP and MVC (role or not a role ?)
CommunicationsException and Connection Pooling