So now that I enabled this fancy envers and gotten myself some revisions. I'm ready for some complex that should've been easy.
Let me explain the setup and why I need this:
I have 2 threads, each their own hibernate session
Thread A, with Session A does some database changes. Then queue's some stuff to be handled by Thread B, Thread A will wait on the result of Thread B's work. Thread B, will need to actual updated database changes from Thread A, so Thread A commits to database. Thus forfeiting any rollback transaction I could have done. Incase an error occurs in Thread B I will need to revert the changes done in Thread A to the database reverted.
My first thought to this problem was, Hey we use envers, it creates revisions, so why can't we use that to revert to a revision? Google didn't help on this "hibernate envers rollback to revision" query results into only other people asking the same question...
So my 2nd thought was, ok then I'll probably have to implement it myself.
Normal process continues as described above until error in Thread B occurs and is received by Thread A.
Now I'm in need of all revisions that are made by Thread A, so I can use CrossTypeRevisionChangesReader.findEntities(Number revision) http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/envers/CrossTypeRevisionChangesReader.html#findEntities(java.lang.Number) to find all objects changed and use Session.merge(object) to restore the state of the object(s) changed to a state before Thread A started doing it's changes.
Anyhow to achieve this should've been easy task?
|