• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to handle a hanging servlet / servlet timeout?

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My servlet connects to a database, but sometimes the db connection hangs for 3 minutes.

Until I get that particular problem sorted, does anyone know if there is a conventional way do set a timeout for the servlet response?

I want it so that if the servlet has not responded within, say, 10 seconds then the doGet / doPost methods throw a ServletException/IOException.

Is there a conventional way to do this, or do I need to include a timer/monitoring thread in my servlet code?

Thanks,
James
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's taking that long, something is wrong.
I would put my energy into finding out what the problem is before writing timeout code.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The db problem is being looked into, but I think having a timeout isn't a bad thing anyway.

Do you know will I have to code the timeout myself?

Cheers,
James
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use the method System.currentTimeMillis() to get the current time.

you can use a while loop to keep an eye on the time elapsed. And use a flag

say

boolean flag = flase;
while(System.currentTimeMillis()!=23612376){

//perform something
// if successful
flag = true

}

if(!flag)
throw ServletException

Hop this solves your problem

SCJP 91
SCWCD preparing
ISDX perparing
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Durgaprasad,
Welcome to JavaRanch!

We're pleased to have you here with us in the servlets forum, but there
are a few rules that need to be followed, and one is that proper names are
required. Please take a look at the
JavaRanch Naming Policy and
adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here




As
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//perform something
// if successful
flag = true

}



what if //perform something is a blocking call ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Hodgkiss:
The db problem is being looked into, but I think having a timeout isn't a bad thing anyway.



Personally, I disagree.
I think the effort required to implement this would outweigh the benefits.
On my own site, I have a sample project for handling long running processes.
In it, after the first request is made, the app starts the task in a new thread and immediately returns a status page. The status page refreshes itself every n seconds to see how the task is going. Once the task is complete, the app forwards to a results page.
http://simple.souther.us/not-so-simple.html
See LongRunningProcess.

Something like that might work for you.
If after n refreshes, the process isn't done, you could forward to an error page.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've used something like this in 1.3 ...

Note this doesn't even attempt to stop the other thread. If it's hung on some blocking operation there isn't much we can do. It runs to completion and uses whatever resources that takes.

Java5 has some new bits like Future that help with such things.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic