问题
I have a JTA transcation which I commit. Can I roll it back after I commit? If yes, how? I have following situation.
I have a backend layer which updated 3 DBs. I have used JTA user transcation for it. If update in any DB fails all updates to 3 DBs are rolled back using utx.rollback
Now I have a layer on top of backend layer which updates some other DB. Now I want that step 1 and step 2 should both succeed or both fail, so I want to roll back JTA transcation of step 1 in case step 2 fails.
It's difficult for me to put code of step 2 into 1 as we are using some existing APIs to update DB in step 2.
回答1:
I think that the answer is that you cannot do anything like this using JTA, or other RDBMs.
Transactions are either committed, or they are rolled back. Once successfully committed they cannot be rolled back.
The only possible "out" might be to try and use nested transactions, and rollback the outer transaction. But that probably isn't going to work:
- Not all JTA implementations support nested transactions.
- Even if they did, there is no guarantee that the outer transaction will successfully commit. And that could leave you with the "other" DB committed and the JTA transaction rolled back.
It sound like you are going to have to rethink your persistence APIs.
回答2:
You can't rollback a transaction after it is committed.
回答3:
As far as I know you cannot rollback a committed transaction. The underlying database will not support it. For instance oracle will not allow committed transaction to rollback.
Of course there is a way to return to previous state and the terminology is called "point in time recovery". Oracle's point in time recovery mechanism is called FlashBack.
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28424/adfns_flashback.htm
This will allow you to revert your database state to a previous snapshot.
But the catch is, there is no JTA interface to perform this. (Infact it is outside the scope of Transaction Manager.)
The Best bet is to register savepoints in your transaction without committing and rolling back to the save point if one or more operation fails.
回答4:
You cannot rollback a committed transaction, whether it is an XA transaction or nonXA one.
To solve the problem you mentioned, one has to include ALL involved resources inside a single XA transaction.
If the above solution is not feasible for some reason, you can do this: a) Ask the first layer (of 3 DBs) to prepare. b) If outcome of (a) is OK, do a commit of the 4th DB. c) If outcome of (b) is OK, call the second phase commit over the 3 DBs.
HTH.
Thanks, Nitin
来源:https://stackoverflow.com/questions/4766667/can-i-roll-back-a-jta-transcation-after-i-commit-it