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 Infinite loop in Init method of Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Infinite loop in Init method of Servlet" Watch "Infinite loop in Init method of Servlet" New topic
Author

Infinite loop in Init method of Servlet

Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Hi,

In our test environment DB2 connection gets lost, if no queries are made in the span of 2 hrs (if connection remains idle for 2 hrs). This causes some other serious issues.
To keep the connection always alive, I have written a servlet that fires query to this DB, after every 1 hr. But to achive this I had to write infinite loop in the Init method of the servlet as follows:



And in servlet tag of web.xml, I added LOAD-ON-STARTUP=1 tag, so that no one has to manually invoke this servlet.

Is this ok, if Init never returns to the servlet container?
[ October 08, 2004: Message edited by: Manoj Gundawar ]

Manoj<br />SCJP, SCJD, SCWCD, IBM WSAD.
Mike Burnham
Greenhorn

Joined: Jun 06, 2001
Posts: 24
This solution seems problamatic to me. Why couldn't you just check if the connection was still alive before using this? If the connection is bunk you can just reconnect then.


SCJP 1.2
Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Thanks Mike,
Actually its little complext scenario. Connection is really done thru third party software (Documanage). Servlets just calls Documangae API to fire the query. It can not directly make JDBC call. The same connection opened by Documanage is also used by some process which runs on UNIX server every day twice. This is some automated process that does some daily updates. This process fails if the connection to DB2 is lost.

Basically I want to know the implications of the solution discussed above. (in my first post)
If anyone has any idea or thoughts, please let me know.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Is this ok, if Init never returns to the servlet container?

Yow! that is a scary thought. That certainly does not fit the servlet programming model.
If you really need that functionality, why not create a "helper" class that implements Runnable and give it its own Thread.
Bill
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Is this ok, if Init never returns to the servlet container? Basically I want to know the implications of the solution discussed above.

The Servlet Specification says this about the init() method:
The servlet container cannot place the servlet into service if the init method
1. Throws a ServletException
2. Does not return within a time period defined by the Web server

So, I presume that if your init() method is unable to return successfully within a particular time period (could not get more details on where this is defined or set), the servlet will never load.

As answered previously, what you are trying to do in the init() would be best done elsewhere in the application.

Sheldon Fernandes
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
doing this in init() method
Ok
Manoj Gundawar
Ranch Hand

Joined: Nov 17, 2002
Posts: 169
Hi William,
I liked the idea of doing this in helper class. I was planning to do in the init method, cause, the requirement was to have no manual intervention to start this process. But helper class solutions sounds promising.
Thanks for the suggestion.
Manoj
Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254
I think it is not a good practice . Don't you use thing like connection pooling . Worst case you can Use a Singleton class as a place holder for db Connections.

Originally posted by Manoj Gundawar:
Hi,

In our test environment DB2 connection gets lost, if no queries are made in the span of 2 hrs (if connection remains idle for 2 hrs). This causes some other serious issues.
To keep the connection always alive, I have written a servlet that fires query to this DB, after every 1 hr. But to achive this I had to write infinite loop in the Init method of the servlet as follows:



And in servlet tag of web.xml, I added LOAD-ON-STARTUP=1 tag, so that no one has to manually invoke this servlet.

Is this ok, if Init never returns to the servlet container?

[ October 08, 2004: Message edited by: Manoj Gundawar ]


# Help an unprivileged kid.<br /> Whatever u do will make a difference...<br /> ...to a child's life & ur own #<br /><a href="http://www.cry.org/" target="_blank" rel="nofollow">www.cry.org/</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Infinite loop in Init method of Servlet