Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.Session.clear() considered Harmful?

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).

like image 688
Morten Lauritsen Khodabocus Avatar asked Nov 14 '11 07:11

Morten Lauritsen Khodabocus


People also ask

What is Hibernate Session clear?

sesion. flush(); is used in flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database.

What is the difference between evict () and clear ()?

evict() evicts a single object from the session. clear() evicts all the objects in the session. Calling clear() is like calling evict() on every object associated with the session.

When should I close Hibernate Session?

Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations.

Can I reuse the Session in Hibernate?

So, how can i reuse an Hibernate Session, in the same thread, that has been previously closed? Either use the built-in " managed " strategy (set the current_session_context_class property to managed ) or use a custom CurrentSessionContext derived from ThreadLocalSessionContext and override ThreadLocalSessionContet.


1 Answers

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.

like image 54
Tomasz Nurkiewicz Avatar answered Sep 23 '22 18:09

Tomasz Nurkiewicz