I'm trying to use Hibernate for the first time. I have a need to make use of optimistic locking, so that the first commit wins. I worked out how to do that by way of hibernate managed version numbers in a test project. The test project uses just Hibernate, no Spring, so the code for working with the database goes something like this:
get session from sessionfactory
open transaction
perform database actions
commit transaction with error handling
My understanding is that I can make use of Spring to get transaction management and reduce the code to something like this:
perform database actions
What I do not know:
- How to set up Spring at all. I'd like to use XML over annotations. Just a link to a good reference would be awesome.
- How does the error handling come in with the Spring implementation? Where would I catch the
StaleObjectStateException
? - What is the best design structure? I've seen a DAO singleton that all threads use to interact with the database, and that seemed reasonable to me.