File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Database connection management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database connection management" Watch "Database connection management" New topic
Author

Database connection management

RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63
Hi...!!

I need your opinion regarding the database management in web-app

which one is best approach and why or suggest me any better approch

either to make single database connection and set that in session

or to make new database connection for each request and then close that connection as soon as request is completed

Thanks in advance
Ramandeep S
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
In the web-app, I guess you must be using data source to get the connection. Are you using any jdbc framework like spring/hibernate?


love your job and not your company;
...because you never know when your company will stop loving you!
RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63
hi..

Thanks for your reply first

No i m not using any of the frameworks

Thanks
Ramandeep S
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
In that case, I would suggest to use one connection for one transaction. So essentially transaction can not be spanned across multiple sessions but certainly it can span across multiple requests.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42922
    
  68
In that case, I would suggest to use one connection for one transaction. So essentially transaction can not be spanned across multiple sessions but certainly it can span across multiple requests.

It's possible you mean the right thing, but the wording as it is is not correct (or at the least highly misleading). Transactions really do not figure into this. The usual way to do this would be to maintain a DB connection pool for the web app, and then to obtain a connection from it if a request comes in that needs DB access. After the request is completed, the connection should be returned to the pool. Open connections should *not* be stored in sessions - you'd run out of connections pretty quickly.
rohith das
Greenhorn

Joined: Sep 22, 2009
Posts: 8
#You may create a main method for connection.
#from that you may please inherit to the DAo classes

I think this is one good approach



_rohith
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Database connection management