Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Hibernate dirty session?

Tags:

java

hibernate

I was wondering if anybody could tell me what a hibernate dirty session is? I seem to be having an issue where a criteria query is performing an insert when it shouldn't. I believe it's related to a dirty session, but without knowing truly what a dirty session is, I'm unable to resolve my issue. Also, how do you create a dirty session. Thanks.

like image 705
Code Junkie Avatar asked May 29 '12 18:05

Code Junkie


People also ask

How can we avoid dirty checking in Hibernate?

A solution to this problem is to change the default configuration of FlushMode from auto to manual by setting FlushMode. MANUAL . In this way the dirty check mechanism will stop causing the aforementioned synchronization. Although the Session is only ever flushed when Session.

What are dirty entities?

Dirty entities are the ones that get UPDATEed to the database. In some circumstances, that process of determining whether an entity is dirty can take a significant time as by default Hibernate must check each of the entity's attribute values one-by-one.

How does Hibernate manage dirty checking?

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to be updated. It also allows a developer to avoid time-consuming database write actions.

What is a Hibernate session?

Advertisements. A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.


1 Answers

The Hibernate session is a cache. It caches entities read from the database, and it also caches changes you've made to entities it contains, as well as added and removed entities, until the session is flushed (i.e. all the pending changes are written to the database).

A session is said dirty when some changes have not been flushed yet. And it's thus perfectly normal to have a dirty session. The session is flushed before the transaction is committed.

like image 187
JB Nizet Avatar answered Sep 18 '22 08:09

JB Nizet