Some clarifications on stateful repositories around persistence context#1421
Open
njr-11 wants to merge 3 commits intojakartaee:mainfrom
Open
Some clarifications on stateful repositories around persistence context#1421njr-11 wants to merge 3 commits intojakartaee:mainfrom
njr-11 wants to merge 3 commits intojakartaee:mainfrom
Conversation
Member
|
I agree that this could be clarified by saying that the association between persistence context and repository might be scoped to a thread, or to a transaction, or whatever, but I'm not keen on these specific changes. |
Member
|
All you need to really say is that the association is contextual. |
b829df3 to
294ba54
Compare
Member
Author
Alright, I reduced the scope of this to only cover the first item, to at least avoid the misinterpretation and restate things along those lines. We can discuss the other other items separately from this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The language we currently have implies in some places there is only one persistence context associated with a repository:
Methods of a repository can be invoked by multiple threads at the same time. Persistence context is not designed to be accessed by multiple threads or from multiple transactions, as stated in another line from the Data spec
The first 3 references cited above need to be adjusted, which is done in commit 1.
Commit 2 strengthens requirements and guarantees to the developer around using a stateful repository within a transaction.
Usage of a stateless repository outside of a transaction is another problem. Considering the following pattern,
How does the Jakarta Data provider know that the application is done with the persistence context such that updates ought to be flushed to the database versus whether the application intends to make another update,
The only way I know of to indicate this currently is to use a resource accessor method on a JPA-backed Data provider to explicitly flush,
Resource accessor methods are an advanced pattern. I also don't like the problem the user is confronted with here of whether it's a good idea or terrible idea to close the EntityManager obtained from the stateful repository. Assuming usage outside of a transaction is intended to be supported -- the Data spec doesn't currently require a transaction -- I think we can make this easier on users by providing a built-in
flush()operation. But that will require an interface forStatefulRepository. I haven't included that in this PR because we should probably discuss it first.