0

I am dealing with a JSP-Web-Application and get troubles in the moment the session expires. As placed in the web.xml the timeout is defined after 30 Minutes. But I want to keep the session alive during the time the user just keeps the window opened. I thought first about a simple AJAX-Component that says "hello" to the Server every 10 Minutes and maybe shoots a simple Test-Statement to the database (e.g. "Select 1").

But two things make me suspicious about this:

  1. If I'd put a simple setTimeout(sayHi(),600) in a endless while-JS-loop is this effective and a wise decision?
  2. Is even the concept of this a good idea?

All the users of the system share a common pooled BoneCP connection to the database to execute short queries from the database. Also several data rows are fetched via Hibernate. The Hibernate-part is the most error-prone part of the whole application, after the timeout, those sets need to be reloaded as well. Is there a way to keep the Hibernate-Session alive as well?

Thank you for your help!

Qohelet
  • 1,459
  • 4
  • 24
  • 41
  • In my eyes what you are doing is counterproductive: on the one hand, you want to limit the timespan, which a user has to study the website doing whatever he wants to do and on the other hand extending this limit artificially. Why not simply increase the timeout to 2 hours and you are done? – Thomas Junk Aug 02 '13 at 15:20
  • 1) If it's contra-productive, please give me a better advice, that'S what I asked for. 2) This solution is useless if someone is away for 2h and 1 Minute 3) The solution Ray Wadkins provided has the great side-effect make it possible to work on a proper session management in case the users didn't log out – Qohelet Aug 05 '13 at 04:07
  • You should ask yourself: why do I want a timeout at all. Literally your timeout is no timeout, when there is no point, where the time is really out. – Thomas Junk Aug 06 '13 at 18:42

1 Answers1

0

I can only speak to the timeout issue. An endless while-JS-loop won't work, because javascript in the browser runs in a single thread, the loop will block all other activity, effectively freezing your page.

the setInterval method of the window object will perform an action (semi) regularly at the interval you define

setInterval(sayHi, 600000)

will cause sayHi to run as soon as possible after 10 minutes (600000 milliseconds). But other operations will be allowed in the meantime.

(also note that I removed the parentheses from your sayHi. sayHi() will resolve immediately, which isn't what you want.)

Ray Wadkins
  • 876
  • 1
  • 7
  • 16
  • Sounds great, so far, this is the function what I was looking for. Due to JS can't handle Threads the timeout would mess everything up. Thank you! Do you know what might be necessary to keep also the Hibernate alive? – Qohelet Aug 02 '13 at 15:28
  • There's also a way to use setTimeout, too. But it's a level more complex in an unnecessary fashion, so I didn't mention it. Unfortunately, I'm not a Java developer, so I can't help you at all on the server side. – Ray Wadkins Aug 02 '13 at 17:14