iluwatar / java-design-patterns

Design patterns implemented in Java
https://java-design-patterns.com
Other
90.09k stars 26.61k forks source link

Open Session In View pattern #103

Open iluwatar opened 9 years ago

iluwatar commented 9 years ago

Description

The Open Session In View (OSIV) design pattern is aimed at solving the problem of lazy loading in web applications. It allows an open persistence context for the duration of a request, enabling lazy-loaded associations to be accessed and resolved within the view layer.

Main Elements of Open Session In View Pattern:

  1. Persistence Context: Maintains the session open throughout the entire request.
  2. Interceptor or Filter: Manages the lifecycle of the session, ensuring it starts at the beginning of the request and is closed at the end.
  3. Lazy Loading: Allows associations to be lazily loaded and accessed in the view layer without encountering LazyInitializationException.
  4. Transactional Boundaries: Ensures transactions are committed before the view is rendered but keeps the session open to resolve lazy-loaded associations.

References

Acceptance Criteria

  1. Implement an interceptor or filter to manage the session lifecycle, ensuring the session is opened at the start of the request and closed at the end.
  2. Ensure that the transactional boundaries are respected, with transactions being committed before rendering the view.
  3. Validate that lazy-loaded associations can be accessed in the view layer without triggering LazyInitializationException.
pierDipi commented 6 years ago

@iluwatar The link provided is incorrect.

IAmPramod commented 6 years ago

@iluwatar @pierDipi I think the domain is no longer maintainted. we can use https://vladmihalcea.com/the-open-session-in-view-anti-pattern/ and https://www.linkedin.com/pulse/open-session-view-pattern-hamed-hatami/

iluwatar commented 6 years ago

Thanks @IAmPramod :+1:

iluwatar commented 6 months ago

Updated task description