File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Can I call it in init method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can I call it in init method" Watch "Can I call it in init method" New topic

Can I call it in init method

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
I was trying following code in my servlet:

My questions are:-
1. calling Database connectivity (dbconnect()) in init is a better approach?

2. or shall i call it in service?
3. In case i have to make more than one connection objects. I think better would be to have connection pooling. Can you give me a good reference literature for same
4. If I go for connection pooling where should i call it - Init or service
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12982
You should go for a connection pool and only try to get a connection when a request comes in. If you do it in the init method, there is every chance that the DB will close the connection due to inactivity.
Try going to and doing a search on connection pool.
Incidently, having all these as instance variables:

can only lead to disaster when more than one request is executing at the same time.
[ April 03, 2002: Message edited by: William Brogden ]
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Thanks William for your reply.
BUt I think Service is suppose to be a synchronized method and if I made all other method in servlet as synchronized which (deals with database) will it still have a chance of crash in case more than one request is going through?
I agree. Here's the link:
subject: Can I call it in init method
jQuery in Action, 3rd edition