Intercept transaction only when is sure to be committed but before is committed

前端 未结 2 2227
谎友^
谎友^ 2021-02-12 23:36

Context is Java - JPA with Hibernate and Spring.

Let\'s take the scenario of two-phase commit protocol (but only with one resource):

  1. Query to commit fro

2条回答
  •  别跟我提以往
    2021-02-13 00:31

    I just want to firstly go back to a transaction, being a unit of work which either all passes or all fails. With what you are saying about your method about that it cannot be rolled back, and needs to be executed between the start and end of the transaction, then I'm sorry to say you don't have a transaction.

    You could do a lot of checks in your method to make sure the likely hood of a rollback is extremely slim, but there will always be a chance your method will execute and a rollback occurs, but this chance could be extremely negligible and you may be happy with that, I don't know.

    Edit: I think an analogy would be nice.

    Case 1: So classic example of a transaction is buying a banana at the store, if you have no money, or the store has no banana, the store doesn't get money, you don't get a banana, this is what normally happens. But if all conditions are set then the transaction will commit successfully.

    Case 2: Now for what you are asking. For arguments sake lets say you are a thief, and you only want to steal the banana if you'll get away with it. You can't know this for certain, because your asking about the future, unless you are a fortune teller, when you go to steal the banana you could get caught.

    If you go back to the 1st case, you could check that you have money, the store has a banana, give the store your money, throw all the customers out, lock the doors to the store so the store is in an unchangeable state. Go do your method which you can't rollback, come back to the store, and take the banana. No one can assure you that the banana will still be there, but it's very likely.

提交回复
热议问题