8

I've been working on an JSF application. At one place, I've to call an action in managed bean. The action actually process hundreds of records and the session timesout before the processing is finished.

Though all the records are processed successfully, the session expires and the user is sent to login page.

I'd tried adding

session.setMaxInactiveInterval(0);

before the processing of the records with no effect.

How to prevent the session time out during such process.

darkapple
  • 539
  • 1
  • 7
  • 19
  • have you configured session timeout in web.xml to appropriate value – Nayan Wadekar Dec 24 '10 at 09:25
  • yes i did. but my client required less session timout period for rest of others. currently its 3 mins. – darkapple Dec 24 '10 at 09:27
  • try using session.setMaxInactiveInterval(-1), setting value 0 actually invalidates the session. – Nayan Wadekar Dec 24 '10 at 10:55
  • @Nayan Wadekar - I tried using session.setMaxInactiveIntercal(-1). Since in javadoc its mention as "An interval value of zero or less indicates that the session should never timeout." So, I tried using zero. – darkapple Dec 26 '10 at 05:43

2 Answers2

10

Introduce ajaxical polls to keep the session alive as long as enduser has the page open in webbrowser. Here's a kickoff example with a little help of jQuery.

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function() {
        setInterval(function() {
            $.get('poll');
        }, ${(pageContext.session.maxInactiveInterval - 10) * 1000});
    });
</script>

Here ${pageContext.session.maxInactiveInterval} returns the remnant of seconds the session has yet to live (and is been deducted with 10 seconds -just to be on time with poll- and converted to milliseconds so that it suits what setInterval() expects).

The $.get('poll') should call a servlet which is mapped on an url-pattern of /poll and contains basically the following line in the doGet() method.

request.getSession(); // Keep session alive.

That's it.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This would eventually disturb the sites functionality as the questioner requires 3mins session timeout. – niksvp Dec 24 '10 at 12:55
  • @niksvp: feel free to post a better solution. – BalusC Dec 24 '10 at 12:57
  • @BalusC - I would like to add to your answer, to wrap the script in any JSF component and render that part only when action is called, ideally it should not be rendered. – niksvp Dec 24 '10 at 13:20
  • @BalusC - I doubt if this works because I tried to keep the session alive by introducing a4j:poll. Once the form is submitted, aj4:poll stops sending request to the server. – darkapple Dec 26 '10 at 05:28
  • Either just submit the form by ajax as well, or redo the poll in the result page of the form submit. – BalusC Dec 26 '10 at 05:33
  • I'll try once sending ajax request to make a4j:poll to work. Thanks BalusC – darkapple Dec 26 '10 at 06:50
  • Hi BalusC. Do you have any idea how to use a heartbeat with SEAM and Richfaces? See my question on http://stackoverflow.com/q/14647608/42659. – Thomas Feb 01 '13 at 15:44
  • why am I getting Expected , but found { }, ${(pageContext.session.maxInactiveInterval - 10) * 1000}); Expected ; but found ) }, ${(pageContext.session.maxInactiveInterval - 10) * 1000}); ---- error in netbeans? – kinkajou Sep 20 '15 at 06:08
  • @kinkajou: Ignore'n'run it. If it works, then it's just Netbeans itself being wrong -again. – BalusC Sep 20 '15 at 08:54
  • @BalusC I have the code saved in .js file. It complains ) is missing ! It works if I put a number. `$(document).ready(function() { setInterval(function() { $.get('poll'); }, 5000); });` . Also poll works only if it is deployed on root I think. Some **project name/poll** – kinkajou Sep 20 '15 at 10:18
  • @kinkajou: EL expressions are by default not evaluated in a .js file. – BalusC Sep 20 '15 at 11:37
  • @BalucC So, I send it as a parameter? – kinkajou Sep 20 '15 at 11:42
0

To prevent session timeout, try using thread to do processing.

By doing this way, user will be able to do other activities & don't hang up waiting for the process to complete.

Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • since the process is financial transaction related, it must be synchronous and client need to wait for the process to finish. – darkapple Dec 24 '10 at 10:27
  • wadekar - I'm afraid to introduce thread as it may bring unpleasant surprises in web applications. Thanks. – darkapple Dec 26 '10 at 05:30
  • yes may be, but threads are inevitable part of any application & it depends on how we manage them. – Nayan Wadekar Dec 26 '10 at 06:17