Package com.vaadin.flow.internal
Class SessionLockScope
java.lang.Object
com.vaadin.flow.internal.SessionLockScope
- All Implemented Interfaces:
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 Summary
Modifier and TypeMethodDescriptionvoidclose()Unlocks the session and then makes the instances that were current before locking the only current instances, also when unlocking fails.static SessionLockScopelock(VaadinSession session) Locks the given session and makes it the current session, remembering the current instances so thatclose()can restore them.static SessionLockScopelock(VaadinSession session, UI ui) Locks the given session and makes it and the given UI current until the scope is closed.onUnlockFailure(Consumer<RuntimeException> unlockFailureHandler) Sets a handler for an exception thrown whenclose()unlocks the session.
-
Method Details
-
lock
Locks the given session and makes it the current session, remembering the current instances so thatclose()can restore them.- Parameters:
session- the session to lock, notnull- Returns:
- the scope that unlocks the session when closed, not
null
-
lock
Locks the given session and makes it and the given UI current until the scope is closed.- Parameters:
session- the session to lock, notnullui- the UI to make current, ornullto have no current UI while the session is locked- Returns:
- the scope that unlocks the session when closed, not
null
-
onUnlockFailure
Sets a handler for an exception thrown whenclose()unlocks the session. The handler gets the exception instead of it being thrown fromclose(), 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, notnull- 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 withonUnlockFailure(Consumer), or thrown if there is none.- Specified by:
closein interfaceAutoCloseable
-