david wrote:
The hibernate wrapper provides some independence from the transaction layer that has been configured, eg, straight JDBC or JTA. It also allows Hibernate to manage things such as flushing the unit-of-work queues since it knows that a commit/rollback is going to occur. It hides some of the details so you don't need to worry about it. Why don't you write two small sample programs and see which works best for you. I would not say there a right way as such. Coding is an Art - see what feels the best and go with it.
Well, in the case of the UserTransactions vs Hibernate transactions I've thought about it a bit more (probably should have done that in the first place :) and come to the following conclusion:
When your transactions operate purely within the database domain it is usually better to manage the transactions through Hibernate. However when your transactions involve other actions outside of hibernate (e.g. JMS) you will need to manage them yourself via JTA UserTransaction.
Sound right? Placed here for future reference.
But back to the flush issue. If Hibernate is managing the transactions shouldn't it also manage the interaction of the database and transaction? In other words, this should work:
Code:
Transaction tx = session.beginTransaction();
id = (Long) session.save(myObject);
// Shouldn't need this
// session.flush();
tx.rollback();
But it doesn't, I need to put the flush there. Is this a bug? If not, it might be good to put a note in the manual to that effect, as it currently implies that the flush is handled by the transaction.
Cheers,
Steve[/i]