Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 13th, 2003, 04:01 AM
ronaldo9 ronaldo9 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 15 ronaldo9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sessions+database

does anyone know how to put a database connection into a session varible in a servlet?or is this possible?

thanks

Reply With Quote
  #2  
Old March 13th, 2003, 04:47 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,696 ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level)ishnid User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 6 Days 20 h 15 m 13 sec
Reputation Power: 1507
You can set any type of object as a session attribute:
Code:
session.setAttribute("attributeName", objectName);

You have to cast it when you're getting it back out again:
Code:
ObjectType objectName = (ObjectType) session.getAttribute("attributeName");

The only problem is that if you're storing a java.sql.Connection (if that's what you're using) as a session attribute is that you don't know when to close() the connection, because when the session ends, you've lost your pointer to the Connection so you have to wait for the automatic collection to close it, which isn't always the most reliable or efficient.
~ishnid

Reply With Quote
  #3  
Old March 13th, 2003, 05:09 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
It is generally considered a very bad idea to store a users connection to the database in their session. This keeps the connection open for the life of the session. Typical sessoins are 30 minutes. Databases will deny new connections after a predetermined limit has been reached. This can be anywhere from 10-100. Regardless of the limit, that leaves very little scalability of your web app.

If you are trying to avoid the performance penalty of opening a new connection each time a user accesses a servlet, then I would suggest Connection Pooling. Tomcat has a built in Connection Pool you can use. I believe this link is the how-to to set it up in Tomcat. Using connection pooling will allow you to get a connection each time a user accesses a servlet and return it after the servlet finished running, but without the cost of creating a new connection everytime. The connectoin pool only creates a new connection if all pre-made connections are currently "checked out".

If you are interested I would suggest searching on google and the sun developer forum on tomcat connection pooling. Hope this helps.

Last edited by Nemi : March 13th, 2003 at 05:12 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Sessions+database


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT