Hi,
I am using jboss(version-4.3.0.GA) with hibernate(hibernate.version>3.2.5.ga, hibernate.entitymanager.version>3.3.2.GA).
I have written a small stateless bean to test a scenario with entitymanager and found that flush is not actually writing changes to the database till the transaction commits.
==pseudo code==
Code:
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class TestClass {
@PersistenceContext()
private EntityManager em;
public testFunc(){
1. fetch an existing entity from database using em.find()
2. modify the entity, merge the changes using em.merge() and invoke em.flush()
3. sleep for a minute using Thread.sleep() to verify the database
}
Now, when I invoke the testFunc() and look at the database during the sleep in step 3, I see that the changes made to the entity in step 2 are not actually yet written to the database. However, they get saved to the db only when transaction commit happens( after the call to testFunc ends). Is this the expected behaviour?
The hibernate EntityManger's document says
Quote:
Flush occurs by default (this is Hibernate specific and not defined by the specification) at the following points:
* before query execution*
* from javax.persistence.EntityTransaction.commit()*
* when EntityManager.flush() is called*
Going by the documentation, since flush() is called in step2, the changes should have actually got written to the database but that is not happening. Hibernate seems to write changes only as part of the auto-flush which happens at transaction commit time but not when flush() is called explicitly. Is this an issue with this version of hibernate ?
I tried using hibernate's FlushMode.MANUAL but the result still is the same.
Code:
public testFunc(){
org.hibernate.Session session = (Session) em.getDelegate();
session.setFlushMode(org.hibernate.FlushMode.MANUAL);
System.out.println("from em.getDelegate session flushmode="+session.getFlushMode()); //printing FlushMode.MANUAL
System.out.println("em flushmode="+em.getFlushMode()); // printing null
1. fetch an existing entity from database using em.find()
2. modify the entity, merge the changes using em.merge() and invoke em.flush()
3. sleep for a minute using Thread.sleep() to verify the database
}
Even with this manual flushmode, I see that the changes in step2 are not written to database by the flush() call. They are only getting reflected after the transaction commit.
Any suggestions or details on how to make em.flush() write changes to the database would be helpful.
Thanks,
Lokesh.