user1960555
user1960555

Reputation: 161

Hibernate Query auto Flush

we have just made a massive upgrade for our web application - jumped from jboss 4 to jboss 7. we have also moved from hibernate 3 to hibernate 4.

I have encountered this weird behavior (which did not happen under hibernate 3): 1. I run an hql (lets say the hql filters the status field - status = 1) and get one object back. 2. I change its status to 2. 3. I run the hql again and get the same object (its even the same instance - with the changed status!) - which does not match the criteria anymore!

I found out that the problem is that the session is not flushed automatically before the query, this is weird because the session has flushmode.auto.

can someone help?

Upvotes: 4

Views: 12241

Answers (2)

user1960555
user1960555

Reputation: 161

I found the problem, it was the transaction factory class (property name - hibernate.transaction.factory_class). It basically comes down to the fact that in a JTA scenario with the JtaTransactionFactory configured, hibernate does not detect that it is in a transaction and will therefore not execute intermediate flushes. when changing it to org.hibernate.transaction.CMTTransactionFactory, everything works.

Upvotes: 3

Nayan Wadekar
Nayan Wadekar

Reputation: 11602

You can try setting the flush mode explicitly to a appropriate value, other modes are available.

session.setFlushMode(FlushMode.ALWAYS); 

Also, can flush manually session.flush()

From Documentation :

  • ALWAYS : The Session is flushed before every query.

  • AUTO : The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.

(emphasis from my part)


Edit : I haven't tried it applying at application level, but you can specify it in persistence.xml

<property name="org.hibernate.FlushMode" value="always" />

And in hibernate.cfg.xml as

<property name="hibernate.flushMode">ALWAYS</property>

Upvotes: 3

Related Questions