File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can I call it in init method" Watch "Can I call it in init method" New topic
Author

Can I call it in init method

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,
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
regards,
Arun
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12681
    
    5
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 java.sun.com 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.
Bill
[ April 03, 2002: Message edited by: William Brogden ]

Java Resources at www.wbrogden.com
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?
regards,
Arun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I call it in init method
 
Similar Threads
Why this stand alone app cannot communicate with servlet
Basic Questions...Please help
Why this stand alone app cannot communicate with servlet
Why this error StreamCorruptedException
Why it fails to update record