jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes How often should we create Connection Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How often should we create Connection Object?" Watch "How often should we create Connection Object?" New topic
Author

How often should we create Connection Object?

Alamu Vinai
Greenhorn

Joined: Feb 01, 2001
Posts: 19
Is it efficient to create Connection Object for each servlet(for retreiving, updating, deleting data to DB)? Or to store the connection Object in Session variable and use it throughout the session? How you all do in coding?
Any feedback is appreciated.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Actually, neither.
You are correct in saying that opening a new connection for each operation is inefficient, but if there was only one connection per servlet it would tend to choke the performance of busy servlets (that get hit more often).
Likewise, creating one for each session ends up assigning resources which largely remain idle (and is therefore a waste) and limits the scalability of the app. (if you want to design for 100 concurrent users, how many databases can be expected to handle this?)
The solution is that many (?) J2EE complient Application servers provide a connection pool. When the server starts up, it creates a set of connections that it maintains separate from the servlets. Each servlet can then ask for a connection from a common pool. If an Application server doesn't provide Connection Pooling it isn't too difficult to implement a simple version yourself.
Dave.
Sophia Oscario
Greenhorn

Joined: May 14, 2001
Posts: 22
I use Pool Manager to handle that ....
U can get it from http://www.codestudio.com/PoolMan/index.shtml

------------------
** Science belongs to the world **


** Science belongs to the world **
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Good reasons not to store Connections in the session:
- most database engines are limited to a fixed number of connections by their license. If a lot of sessions are open, this number gets exceeded and the server may even lock up. At the very least it will disallow future connections until some are released. When that happens depends on the session timeout interval, the working of the garbage collector as well as the connection timeout interval on the server.
- If the connection timeout interval on the server is set to a low value, it may be that the next database operation on the stored connection encounters a problem if the connection was timed out.
- Waste of resources. Storing things in the session takes up RAM as well as CPU cycles.


42
Abdul Kalam
Greenhorn

Joined: May 28, 2001
Posts: 5
Hi all,
continuing with the discussion i would like to add few more things
1.Session object take up resource at the server
2.If the server decides to passivate some of the objects in session bcoz the server can maintain only certain session then as the connection object is not serializable hence if it is passivated by the server then it cannot be retrieved back in the same state.
bye


Abdul Kalam
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How often should we create Connection Object?
 
Similar Threads
Opening the Closed Connection
Database Connection and Session
jdbc thin cliend thread problem-inactive sessions
session bean creation
Can I do raw DML with Hibernate?