Java – spring promotion_ Required transaction attribute?
In http://www.vermatech.com/code/SpringTransactionExamples.html In the first case study given,
testModel.deleteAllCountries(); testModel.initializeCountries();
Initializecountries throws a runtime exception For both methods, the transaction definition attribute is propagation_ required. The transaction is still performed under deleteallcountries. The method is committed, but the transaction under initializecountries is rolled back (according to the log given in the same case study)
According to propagation_ Required definition, which supports the current transaction; If it does not exist, create a new one So my problem is that transactions under the initializecountries method should support transactions under the deleteallcountries method I mean, both methods should be regarded as a single transaction According to my understanding, should I commit or roll back a complete transaction? I don't know how logs handle them separately
Solution
"Need to propagate" is defined as
In the above example, the deleteallcountries method executes and commits in a transaction There is no current transaction when initializecountries is called, so it executes in the second transaction and rolling back it has no effect on the changes made to the first method
Propagation applies to nested method calls, not sequential calls If you look at the documentation:
Then you can see that all this is about the inside and outside - none of them mention continuous calls In your case, the call to deleteallcountries is the outermost transaction method, so when it completes successfully, spring commits the transaction immediately Then your call to initialize countries must be executed in a separate transaction, which is the outermost method
Your assumption seems to be that spring will keep the transaction open after the first method completes, but that's not how it works To get the effect you want, you can create another method on testmodel, which contains calls to deleteallcountries and initializecountries to make the method transactional and provide it with the property propagation_ required. In this way, the rollback of the second method will cause the changes of the first method to be rolled back, because the wrapper method combines them together Otherwise, nothing tells spring that these things should be part of the same deal