What does transaction.commit() do when the flushmode is set manual in Hibernate?

Posted by wei on Stack Overflow See other posts from Stack Overflow or by wei
Published on 2010-04-15T21:43:16Z Indexed on 2010/04/16 0:23 UTC
Read the original article Hit count: 580

Filed under:

Here is a block of code in the Java Persistence with Hibernate book by Christian and Gavin,

    
Session session = getSessionFactory().openSession();
session.setFlushMode(FlushMode.MANUAL);
// First step in the conversation
session.beginTransaction();
Item item = (Item) session.get(Item.class, new Long(123) );
session.getTransaction().commit();
// Second step in the conversation
session.beginTransaction();
Item newItem = new Item();
Long newId = (Long) session.save(newItem); // Triggers INSERT!
session.getTransaction().commit();
// Roll back the conversation!
session.close();//enter code here

I am confused that why the first step and second step need to be wrapped into two separate transactions? Since the flushmode is set manual here, no operations (suppose we ignore the insert here) will hit the database anyway. So why bother with transactions here?

thanks

© Stack Overflow or respective owner

Related posts about hibernate