diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2012-11-23 17:43:37 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2012-11-23 16:40:17 +0000 |
commit | e4206c2d2aaed646dbeeb052f88e5ec4f8607dac (patch) | |
tree | 82e0705af51530d18c1cae08a6796544ec0b9b37 /server | |
parent | 464a7d721c3c97f7f3033bd7e22fe94c33fe698c (diff) | |
download | vaadin-framework-e4206c2d2aaed646dbeeb052f88e5ec4f8607dac.tar.gz vaadin-framework-e4206c2d2aaed646dbeeb052f88e5ec4f8607dac.zip |
Defer session closing until the end of a request like with UIs (#10252)
Change-Id: I6231977a4d4f44cbee4a95664f0bef6acf4ca034
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/server/VaadinService.java | 43 | ||||
-rw-r--r-- | server/src/com/vaadin/server/VaadinSession.java | 31 |
2 files changed, 49 insertions, 25 deletions
diff --git a/server/src/com/vaadin/server/VaadinService.java b/server/src/com/vaadin/server/VaadinService.java index ea363dc9cf..5d3f79fddb 100644 --- a/server/src/com/vaadin/server/VaadinService.java +++ b/server/src/com/vaadin/server/VaadinService.java @@ -755,21 +755,25 @@ public abstract class VaadinService implements Serializable { VaadinRequest request, Class<? extends UI> uiClass); /** - * Closes the VaadinServiceSession and discards all associated UI state. + * Sets the given session to be closed and all its UI state to be discarded + * at the end of the current request, or at the end of the next request if + * there is no ongoing one. + * <p> * After the session has been discarded, any UIs that have been left open - * will give an Out of sync error ( - * {@link SystemMessages#getOutOfSyncCaption()}) error and a new session - * will be created for serving new UIs. + * will give a Session Expired error and a new session will be created for + * serving new UIs. * <p> * To avoid causing out of sync errors, you should typically redirect to * some other page using {@link Page#setLocation(String)} to make the * browser unload the invalidated UI. * + * @see SystemMessages#getSessionExpiredCaption() + * * @param session * the session to close */ public void closeSession(VaadinSession session) { - session.removeFromSession(this); + session.close(); } /** @@ -784,9 +788,13 @@ public abstract class VaadinService implements Serializable { closeInactiveUIs(session); removeClosedUIs(session); } else { - getLogger().fine( - "Closed inactive session " + session.getSession().getId()); - closeSession(session); + if (!session.isClosing()) { + getLogger().fine( + "Closing inactive session " + + session.getSession().getId()); + closeSession(session); + } + session.removeFromSession(this); } } @@ -896,19 +904,24 @@ public abstract class VaadinService implements Serializable { /** * Returns whether the given session is active or whether it can be closed. * <p> - * A session is always active if - * {@link #getUidlRequestTimeout(VaadinSession) getUidlRequestTimeout} is - * negative. Otherwise, it is active if and only if the timeout has not - * expired. + * A session is active if and only if its {@link #isClosing} returns false + * and {@link #getUidlRequestTimeout(VaadinSession) getUidlRequestTimeout} + * is negative or has not yet expired. * * @param session * The session whose status to check + * * @return true if the session is active, false if it could be closed. */ private boolean isSessionActive(VaadinSession session) { - long now = System.currentTimeMillis(); - int timeout = 1000 * getUidlRequestTimeout(session); - return timeout < 0 || now - session.getLastRequestTimestamp() < timeout; + if (session.isClosing()) { + return false; + } else { + long now = System.currentTimeMillis(); + int timeout = 1000 * getUidlRequestTimeout(session); + return timeout < 0 + || now - session.getLastRequestTimestamp() < timeout; + } } private static final Logger getLogger() { diff --git a/server/src/com/vaadin/server/VaadinSession.java b/server/src/com/vaadin/server/VaadinSession.java index 3ebdb94248..5deb54f57c 100644 --- a/server/src/com/vaadin/server/VaadinSession.java +++ b/server/src/com/vaadin/server/VaadinSession.java @@ -117,6 +117,8 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { private long lastRequestTimestamp = System.currentTimeMillis(); + private boolean closing = false; + private transient WrappedSession session; private final Map<String, Object> attributes = new HashMap<String, Object>(); @@ -271,7 +273,6 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { @Deprecated public void removeFromSession(VaadinService service) { assert (getForSession(service, session) == this); - session.setAttribute( VaadinSession.class.getName() + "." + service.getServiceName(), null); @@ -866,23 +867,33 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { } /** - * Closes this session and discards all associated UI state. After the - * session has been discarded, any UIs that have been left open will give an - * Out of sync error ({@link SystemMessages#getOutOfSyncCaption()}) error - * and a new session will be created for serving new UIs. + * Sets this session to be closed and all UI state to be discarded at the + * end of the current request, or at the end of the next request if there is + * no ongoing one. + * <p> + * After the session has been discarded, any UIs that have been left open + * will give a Session Expired error and a new session will be created for + * serving new UIs. * <p> * To avoid causing out of sync errors, you should typically redirect to * some other page using {@link Page#setLocation(String)} to make the * browser unload the invalidated UI. - * <p> - * This method is just a shorthand to - * {@link VaadinService#closeSession(VaadinSession)} * - * @see VaadinService#closeSession(VaadinSession) + * @see SystemMessages#getSessionExpiredCaption() * */ public void close() { - getService().closeSession(this); + closing = true; } + /** + * Returns whether this session is marked to be closed. + * + * @see #close() + * + * @return + */ + public boolean isClosing() { + return closing; + } } |