sre
sre

Reputation: 689

Hibernate flush doesn't update database

I'm using hibernate to store a set of objects from a web service.

As the object are received each I am saving them using hibernate.

Receiving the objects is wrapped in a transaction and all the objects appear in the database after the final object is received.

I am now trying have each object appear in the database when saved. I've tried to achieve this with

getHibernateTemplate().saveOrUpdate( foo );

getHibernateTemplate().flush();
getHibernateTemplate().clear();

My understanding is this should remove the values hibernate's cache and write the values to the database.

Any learning or directions?

Upvotes: 0

Views: 9232

Answers (2)

sre
sre

Reputation: 689

Thanks for the help Brian. The problems turned out to be a for loop in another class wrapping the save call.

The solution was to remove the for loop and replace it with an iterator.

Hibernate was keeping the same transaction for the entire for loop. Using the iterator, Hibernate seems to start a new transaction and hence performs the commit to the database and then a flush before beginning the next transaction.

Upvotes: 1

Brian Deterling
Brian Deterling

Reputation: 13724

If you're still inside a transaction then only the session or connection that opened the transaction will be able to see the records. In some databases, you should see them from another session if you do a dirty/uncommitted read. I would try running a select using the same Hibernate session after the flush to verify that it really is in the database. Just don't query by the primary key or you may get it from the cache.

Upvotes: 0

Related Questions