File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Infinite loop in Init method of Servlet

 
Manoj Gundawar
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Burnham
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doing this in init() method
Ok
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic