Search code examples
springtransactionsspring-transactions

TransactionSynchronizationRegistry vs TransactionSynchronizationManager


I have an application running no weblogic with ejb, with JTA provided by WLS. Now I am trying to adapt all application to work in tomcat without ejb, using spring instead. Also will use JPATransactionManager that spring provides, instead of JTA.

I used to use TransactionSynchronizationRegistry.getTransactionKey() in order to get tx object and use it for cache purposes. Now I am adapting spring and I have TransactionSynchronizationManager of spring, where I can't findv such an API.

My question is - do you know some analog of TransactionSynchronizationRegistry.getTransactionKey() in spring ?

Or where I could get some unique object with info about transaction?


Solution

  • Problem was solved by using some generated logical transaction ID. This transaction ID is stored by calling TransactionSynchronizationManager.bindResource(key, value) and retrieved by TransactionSynchronizationManager.getResource(key).

    However it will not work properly in case if transaction interleaving.

    Assume that you have flow with transaction interleaving when upstream transaction A is suspended whereas downstream transaction B is running.

    In case if in two this transaction you try to store transaction ID with some key as mentioned above, you could have an issue that you mixing your resources.

    In this case recommended use ResourceHolderSupport which is responsible to listen various states of transaction and bind or unbind resources.

    In our case assume that tx1 is stores resource = "txID", "123456", and tx1 suspended because tx2 is starting to run, resource holder will unbind resource and when tx1 will be resumed - it will bind it again.