As I said before Session.flush() should NOT be called outside of transation. You just use this method in bad context.
Quote:
Must be called at the end of a unit of work, before commiting the transaction and closing the session
Perhaps Hibernate should throw an exception if flush is closed NOT within an transaction.
I've also checked testcase you provided -
these are results:
1) because flush is used outside of transaction - it is not commited
session.flush() causes INSERT/UPDATE SQL to be executed:
Code:
INSERT INTO CUSTOMER VALUES(.........something..........)
2) changes are not commited nor rolled back.
3) what is strange - if flush is used this way - all SQL will be commited / rolled back during
next transaction (that might be bit dangerous and breaking rules of ACID)
please try add following code
Code:
session.flush();
session.close();
// --- BEGIN OF FIX ----
Session s = sessionFactory.openSession();
s.beginTransaction();
s.getTransaction().rollback();
s.close();
// --- END OF FIX ---
Session session2 = sessionFactory.openSession();
this time your test will be passed...
and if you try to commit changes in the next transation:
Code:
session.flush();
session.close();
// --- BEGIN OF FIX ----
Session s = sessionFactory.openSession();
s.beginTransaction();
s.getTransaction().commit(); [b]//this will save changes to DB!!![/b]s.close();
// --- END OF FIX ---
Session session2 = sessionFactory.openSession();
changes to customer will be saved!
----------------------
Dominik