aspose file tools*
The moose likes Servlets and the fly likes db connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "db connection" Watch "db connection" New topic
Author

db connection

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
hi all,
was wondering whats the best practise when opening and closing db connections from a servlet ? initialise the connection in init() and close it in destroy() ?? Am using glassfish v2.1 app server and was also wondering if i should leave the connection pooling to the app server ??


many thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Best practice is to NOT handle the connections in the servlets. Either use container-managed connection pooling, or let an ORM tool handle the connections.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
thanks for the reply. i have set up a connection pool in the app server and am looking it up in my servlet by using its JNDI name i.e



I wanted to know if its a good idea to establish connection

1) only when its needed. in my case it will be in the doPost(); so the container is responsible for establishing and closing connections.

OR

2) to open and hold a connection i.e in the init() (in order to save time asking the container to establish db connection everytime its needed). And disconnect in destroy() ??


thanks again.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60046
    
  65

Only when its needed. Otherwise you are holding open a connection needlessly for the duration of the servlet lifetime (which is likely the lifetime of the application itself). Why bother to use connection pooling if you're never going to return the connection to the pool?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Overall, it is always best to use a utility class (call it as ConnectionUtil or DBUtil) and let it do the stuff. You just call it from your servlet.

The time duration to which the connection is kept open should still be minimized. Say for example you make a call to the utility/helper from your servlet and that method takes care of opening and closing the connections as and when it is done with the job! This would be ideal and better choice!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
i like the idea of a utilities class. Also do you know if i need to synchronise my prepared statements i.e




or does the servlet container handle that as well ??
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I think you are very confused.

Why do you synchronize the prepared statement? It is NOT at all necessary until you are going to use ONLY ONE prepared statement for the entire application and it is going to be shared by many threads!
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
i was planning on using the same prepared statement to handle all db queries from my servlet. so have delcared a



and was planning on using 'ps' for this particualr servlet for all db queries.


ta.
Saravanan Mahalingam
Greenhorn

Joined: May 18, 2006
Posts: 4
What is the purpose of declaring 'ps' as private ?

I would suggest to delcare it as local variable inside doPost() or doGet()
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
the only reason i declared it private is so i could reuse it at different places within the servlet since i am making multiple db queries. Please correct me if this doesnt sound like the right way to go.

ta.
 
 
subject: db connection
 
Similar Threads
App to server connection
Application configuration GUI
EJB Transactions and Java beans interaction
connection.close
Form-based authentication - check db connection