This is a design question, concrete code not submitted to protect my bottom.
When working with Hibernate the standard workflow is as follows:
- Open Session
- Start Transaction
- Do the business (read and modify data)
- Commit Transaction
- Close Session
with possibly iterations through 2-4.
What are reasonable use cases for Session.clear()?
A: The concrete problem I have is a (big) piece of code which loads and modifies entities, then clear()s the session, essentially throwing away the changes that were made. (The business task to be accomplished does not include modifying the entities, so the code "works").
Seems to me the right design would be to make sure the (big) piece of code does not make changes it does not want to save?
B: I would guess Session.clear() exists for convenience/flexibility, not because it is a good idea to use it.
Have I misunderstood the Hibernate philosophy?
C: Subquestion: Is it a bad idea for framework code to unconditionally clear() the session when a task completes? IMHO, the framework should complain if the session is dirty when a task completes! The session should be closed, seeing as the task is done... (Disregarding performance for the minute)
(Labels A, B and C so you can indicate which part you are answering).