Hi All,
I am a J2EE Developer.
I am using JDBC Transaction not JTA. Also using JBoss, but doesn't matter as in standalone i think behavior is same.
By default Session flush mode is auto, it means at any query execution and at transaction.commit(); it should work.
Flush is not working in 3 of the four ways.
1. Not worked, Default auto flush.
2.Not worked, Session session = sessionFactory.openSession();
session.setFlushMode(FlushMode.COMMIT);
3.Not worked, Set the property in hibernate.cfg.xml <property name="hibernate.transaction.flush_before_completion">true</property>
4. Worked ,
session.flush();
tx.commit();
Why the first three are not working, as according to hibernate doc all should work.
My code is as follows,
public void mySave(MyDTO dto) throws Exception {
Session session = null;
Transaction tx = null;
try {
session = hibernate.openSession();
tx = session.beginTransaction();
session.save(dto);
/////////////////
//session.flush(); // I f i remove/comment this line then problem occurs
//////////////////
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
throw new BSException(e);
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
}
finally {
if (session != null) {
session.close();
}
}
}
output is much like
Hibernate: Select max(id) from mytable;
=================End of output.
In 4th case when I do session.flush b4 tx.commit() manually; then output is much like, and it works fine
Hibernate: Select max(id) from mytable;
Hibernate: insert into mytable(a, b) values (?, ?);
|