Originally posted by miguel lisboa:
do i get back to (moderate) try catch?
my answer is yes, in view of the need of the roll back.
do you agree?
Yes, to be make your application robust in the face of failures,
you should be doing this. Ninety-nine times out of a hundred, it won't matter. But problems do occur, even in a single-user desktop application.
Sessions are designed to be lightweight and quick to create, thus the use of the SessionFactory which maintains the configuration. You will be fine creating one for each transaction with your application.
However, you don't normally want your DAO managing the transactions or session. This should be left to the business logic. If you are going to change a user's email address, for example. You want to
begin a transactionfind and load the userset the new email addresssave the userflush the sessioncommit the transaction This is another place Spring helps out. It provides classes to maintain thread-bound sessions and transactions. When a session is needed, if the
thread doesn't have one already, a new one is opened and bound to the thread.
This is important because the session maintains the
unit of work to flush to the database. If you load it in one session and then save it in a different session, you'll have to use saveOrUpdateCopy() so it can reload the user to tell which objects and values have changed.
For managing the transactions, Spring provides EJB-like method-level transaction demarcation. Spring will begin and commit/rollback transactions based on each method's transaction property.