Flush tell hibernate to send any unit-of-work operations to the database, eg, execute outstanding SQL operations. Obviously, the flush needs to be performed (where Hibernate will automatically perform a flush in various situations) within the transaction boundary. The transaction can then be commited or rolledback depending again on success or otherwise or the use case. Don't use the JDBC driver directly for the transaction commit as it could cause the transaction manager to get out of sync. The Hibernate documentation clearly defines the transaction framework you should apply in your code, eg,
Code:
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
//do some work
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}
The above code is all that you need. The tx.commit will perform the flush for you.