The problem described by you is solved by the “pattern” of Conversations. An example of a “long-playing” dialogue:
- The first screen of the dialog opens. The data shown to the user is loaded in a separate session session and database transaction. The user can modify any dialog fields.
- After five minutes of editing, the user uses the UI element to save. Changes are reflected in the database. The user also expects exclusive access to the data during the editing session.
Although in the described example there are several cases of access to the database, from the user's point of view, this series of steps represents one unit of perfect work (Unit of Work). There are many ways to implement this in an application.
The first (naive) method is to keep Session sessions and transactions open while the user is editing, using database synchronization mechanisms to provide exclusive user access to editable data, and prevent other users from accessing them, ensuring isolation and atomicity. This is an anti-pattern, since unnecessary synchronization is a bottleneck for performance problems arising in high-load applications.
Another method is the use of a number of database transactions to implement a dialogue with the database. In this case, ensuring the isolation of business processes falls on the shoulders of the application. One conversation usually covers multiple transactions. Multiple database accesses can be atomic if only one transaction (usually the last one) writes to the database. All others only read the data. A typical implementation path is through the creation of a wizard-style dialog covering several steps of a request / response cycle. Hibernate includes some features that allow you to implement similar functionality:
- Automatic Versioning: Hibernate can perform concurrency control for you. It can automatically detect whether third-party updates were made while the user was waiting.
- Detached objects: if you choose to use the session-per-request template, all downloaded instances will be disconnected while the user is waiting. Hibernate allows you to connect objects back and save modifications. This pattern is called session-per-request-with-disconnected objects. Automatic versioning is used to isolate concurrently running queries.
- Extended Session: A Session can be disconnected from the underlying JDBC connection after the DB transaction is committed, and reconnected when a new client request occurs. This pattern is called session-to-dialogue, which makes the reattachment of objects unnecessary. Automatic versioning is used to isolate parallel modifications, and the session cannot be flushed automatically, only explicitly.
That is, Session-to-query-with-detached-objects and session-to-talk patterns can be used to implement dialogs in Hibernate.
I use Hibernate to describe the Dialog pattern, since it definitely has built-in support for the pattern and it is described in the official documentation. It's hard for me to talk about pattern support in JPA, but fast and superficial googling shows that the pattern can be implemented in JPA.
Sources:
- Hibernate Developer Documentation - Chapter II. Transactions and multithreading
- Hibernate ORM User Guide
- Implementing conversations
In the last source there are examples of various implementations of the Dialog pattern.