I'm writing an application in Java, with Hibernate, and I'm trying to do the following:
- Query a list of objects from the database, then for each object:
- Remove it from the database.
- Perform an operation on it.
- If the operation fails, modify the object and insert it back into the database.
However, I'm working with an unreliable database connection, and it is essential that the object is successfully removed from the database (step 2) before an operation is performed on it (step 3). It's important but not critical that the object is successfully re-inserted (step 4) on failure.
(The higher level requirement is that if the operation is successfully performed on the object, it CANNOT remain in the database -- that's why I concluded that I have to remove it first and ensure that it is actually removed before attempting the operation.)
I don't fully understand Hibernate sessions, transactions, and object states (e.g. "detached"). How can I accomplish this?
I have tried too many permutations of opening/closing sessions, committing/rolling back transactions, evicting/merging/persisting/saving/deleting/updating objects to list here, but everything I try fails with one error or another. I've been referring to the Hibernate docs, but basically I'm swinging in the dark here because I don't really understand what I'm doing, so any suggestions are welcome.