Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is a design question, concrete code not submitted to protect my bottom.

When working with Hibernate the standard workflow is as follows:

  1. Open Session
  2. Start Transaction
  3. Do the business (read and modify data)
  4. Commit Transaction
  5. 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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
222 views
Welcome To Ask or Share your Answers For Others

1 Answer

Ad. A: Looks like you are aware what clear() does. The reason to call it explicitly is to remove all managed entities from L1 cache, so that it does not grow infinitely when processing large data sets in one transaction.

It discards all the changes that were made to managed entites not explicitly persisted. This means that you can safely modify an entity, update it explicitly and clear the session. This is the right design. Obviously if no changes are made (long, but read only session), clear() is always safe.

You can also use stateless sessions.

Ad. B: No, it exists for the reasons above: to make sure L1 (session cache) does not grow too much. Of course maintaining it manually is a poor idea and an indication that another tool should be used for large data sets, but sometimes it is a must.

Note that in JPA specification there is also clear() and flush() method. In this case you should always call flush() first to push changes into the database (explicit update) prior to calling clear().

Ad. C: It's actually a good idea to warn the user (maybe by issuing warning message rather than throwing an exception) when he/she clears the session with dirty changes. Also I don't think a framework code should call clear() unconditionally, unless it is sure that the user code it runs flushes or does not make any changes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...