Class SessionLockScope

java.lang.Object
com.vaadin.flow.internal.SessionLockScope
All Implemented Interfaces:
AutoCloseable

public final class SessionLockScope extends Object implements AutoCloseable
Holds the lock of a session and restores the current instances when the lock is released, for use with try-with-resources.

Code that sets the current UI for work it does with the session lock held uses this so that no code runs with a current UI without holding the lock of its session: the current instances that were in place before locking are restored right after unlocking, also when unlocking fails. The locked session is current until it is unlocked, as deadlock detection and the access tasks that unlocking runs rely on it.

For internal use only. May be renamed or removed in a future release.

Since:
25.5
  • Method Details

    • lock

      public static SessionLockScope lock(VaadinSession session)
      Locks the given session and makes it the current session, remembering the current instances so that close() can restore them.
      Parameters:
      session - the session to lock, not null
      Returns:
      the scope that unlocks the session when closed, not null
    • lock

      public static SessionLockScope lock(VaadinSession session, UI ui)
      Locks the given session and makes it and the given UI current until the scope is closed.
      Parameters:
      session - the session to lock, not null
      ui - the UI to make current, or null to have no current UI while the session is locked
      Returns:
      the scope that unlocks the session when closed, not null
    • onUnlockFailure

      public SessionLockScope onUnlockFailure(Consumer<RuntimeException> unlockFailureHandler)
      Sets a handler for an exception thrown when close() unlocks the session. The handler gets the exception instead of it being thrown from close(), for code that can only log such a failure, e.g. because the session error handler would need the lock.
      Parameters:
      unlockFailureHandler - the handler for an exception thrown by unlocking the session, not null
      Returns:
      this scope, for chaining
    • close

      public void close()
      Unlocks the session and then makes the instances that were current before locking the only current instances, also when unlocking fails. An exception thrown by unlocking is passed to the handler set with onUnlockFailure(Consumer), or thrown if there is none.
      Specified by:
      close in interface AutoCloseable