This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JSP and the fly likes Is it a Good way.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Is it a Good way.." Watch "Is it a Good way.." New topic
Author

Is it a Good way..

Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
Hi,
Whenever we are dealing with Connection in JSP, can we put the connection in the session and use it through the session..
like:
Connection conn=DriverManager.getConnection(url,user,password);
session.putValue("conn",conn);
And in another page:
Connection conn=(Connection)session.getValue("conn");
So we need to create another connection rite..
Just taking it from session..
Is it recommended insteadof Connection Pooling..
Thanks in advance..
Pranit..
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Pranit Saha:
Whenever we are dealing with Connection in JSP
Stop right there. You shouldn't be dealing directly with a Connection in a JSP. Even forgetting about maintainability issues, it's very hard to get right and not end up with connection or statement leaks if, for example, an impatient user presses STOP or REFRESH on the browser. Instead of this, consider using an MVC (Model II) architecture where the hard work is being done by controller command objects (action objects for Struts aficionados) and the JSPs merely render the results.
can we put the connection in the session and use it through the session..
First, this would consume one connection per user, something you can only afford in very low traffic situations. Second, session-scoped objects may be accessed by multiple threads simultaneously and Connection objects do not always take kindly to that, even disregarding that it would wreak havoc with any transactional stuff you might be doing.
Is it recommended insteadof Connection Pooling..
No. (You saw that one coming, right? )
- Peter
[ April 22, 2002: Message edited by: Peter den Haan ]
Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
Hi,
Thanks a lot for your reply.. It has helped me a lot..
Pranit..
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
I agree with Peter.
For furthur reference check out "core patterns in J2EE" which explains different server side, client side layers in different scenarios.
Kishore.


Kishore
SCJP, blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it a Good way..
 
Similar Threads
how does this declaration differs !!!
Issue with Hibernate transaction in CMT
[Hib3] Accessing the Connection first
jdbc connection suddenly closed
AbstractFlushingEventListener - Could not synchronize database state with session