Quote:
Well, it looks you are not taking into account what the authors mentioned in HiA as per #1...are you sure on this count?
Indeed I am. What you can do in the situations where you want to avoid a flush, you can do something like this:
Code:
Session session = HibernateUtil.getCurrentSession();
session.setFlushMode(FlushMode.NEVER);
//Do some query stuff here
Query query = session.createQuery("from Foo where id = :id");
...
session.setFlushMode(FlushMode.AUTO);
So here, we can use the default of FlushMode.AUTO for most cases, and in this specific case, you we override it by suspend automatic sesison flushing while we execute the query and set it back to auto once we're done. If you set the default to NEVER, you'd have to manually flush the session at the end of each use case. It pretty much make using CMT usless. If use FlushMode.NEVER and don't ever call flush(), nothing will be in your database.
On the flip side, even when you use FlushMode.AUTO, you can still call session.flush() explicitly when needed. I have had situations where this was needed.
Quote:
Well do you mean to say that session.close need not be called in a JTA situation? If so, then what about in non-managed environment(JDBC- standalone or Tomcat) - does commit only perform calling session.flush - if not then what else it does?
Yes. With a JTA Session Context, you do not need to call session.close(). Hibernate will automatically clean up all resources when the transaction commits. As for JDBC managed environments, I am 98% same is true, but I'd have to double check. I only use JDBC transactions for JUnits, not for production code. Unlike JTA however, the container doesn't know when the transaction is done, so you must make sure that you commit the transaction when your use case has completed.