Java – Hibernate Session flush behaviour [ and Spring @Transactional ]

hibernatejavasessionspring

I use Spring and Hibernate in a web-app,

SessionFactory is injected into a DAO bean, and then this DAO is used in a Servlet through webservicecontext.

DAO methods are transactional, inside one of the methods I use … getCurrentSession().save(myObject);

One servlet calls this method with an object passed.

The update seems to not be flushed at once, it takes about 5 seconds to see the changes in the database. The servlet's method in which that DAO's update method is called, takes a fraction of second to complete.

After the @Transactional method of DAO is completed, flushing may NOT happen ? It does not seem to be a rule [ I already see it ].

Then the question is this: what to do to force the session to flush after every DAO method?
It may not be a good thing to do, but talking about a Service layer, some methods must end with immediate flush, and Hibernate Session behavior is not predictable.

So what to do to guarantee that my @Transactional method persists all the changes after the last line of that method code?

 getCurrentSession().flush() is the only solution?

p.s. I read somewhere that @Transactional IS ASSOCIATED with a DB Transaction. Method returns, transaction must be committed.
I do not see this happens.

Best Answer

Once the "top level" @Transactional method has completed (ie the method called from your servlet), then the transaction should be committed, and the default Hibernate behaviour is to flush on the commit.

It sounds like something odd is happening and you should do a bit of investigating.

Investigate your logs, in particular I would set the logging level to DEBUG on your Transaction Manager to see exactly what the transaction manager is doing.

Also, set the logging on for hibernate (set show_sql to true): this will output to System.out when flushing is happening and this might give you some clues.

Do report back if you find anything interesting.