Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces fileUpload and session timout

Could a session timeout happen if there are several very big files are uploaded? Imagine I upload one 5 GByte big file and a short session timeout is set. Could the session timeout occur during streaming the file?

like image 726
opfau Avatar asked Sep 25 '15 10:09

opfau


1 Answers

Yes, it can. The servlet specification does nowhere forbid that a session could be destroyed during an active request. You'll thus risk a ViewExpiredException when such an upload arrives at bean.

If this is your concern, you've several options:

  1. Let the upload form asynchronously poll to the server at intervals to keep the session alive. You can in EL use #{session.maxInactiveInterval} to obtain the current timeout in seconds.

    <p:fileUpload ... />
    <p:poll interval="#{session.maxInactiveInterval - 10}" async="true" />
    

    The 10 seconds difference is just to prevent that it arrives a few seconds too late because the page itself may also take some time to load all the HTML and to initialize the poll. You can if necessary conditionally start/render the poll on start of upload.


  2. Let the "onstart" event of upload increase the session timeout to a certain limit (hour?) and let the "oncomplete" event of upload put it back.

    <p:fileUpload ... onstart="increaseTimeout()" oncomplete="resetTimeout()" />
    <p:remoteCommand name="increaseTimeout" listener="#{bean.increaseTimeout}" />
    <p:remoteCommand name="resetTimeout" listener="#{bean.resetTimeout}" />
    

    You can in bean use ExternalContext#setSessionMaxInactiveInterval() to set the desired session timeout in seconds.


  3. Use a stateless JSF form. The view will never expire, regardless of how the HTTP session behaves.

    <f:view transient="true">
        ...
    </f:view>
    

    Note: any view scoped beans tied to such a view will behave like request scoped ones. To avoid confusion, replace the annotations if necessary.

like image 75
BalusC Avatar answered Sep 28 '22 05:09

BalusC