find transaction id of spring @Transactional

Viewed 4666

I am using @Transactional to manage the transaction

  @Override
  @Transactional
  public List<FooBo> save(Foo foo, Foo foo2) {
    logger.debug(() -> String.format("FooService.save()"));
    repository.save(foo);
    return repository1.save(foo2);
  }

I am unable to find transaction id while using TransactionAspectSupport.currentTransactionInfo()

is there any other way? (if Spring maintaining the Unique Id of transaction)

Update: My Scenario is that I want to put some information on static context based on transaction id. And after transaction commit need that information.

For example .. based on transaction ID, I want to put some objects in map in static context and after commit need to utilize these objects.. that is what I am thinking

2 Answers

The only way you could maybe do this is to create the Transaction yourself in the method by getting the Transaction Manager through your Spring Application Context and calling getTransaction() with a new TransactionDefinition that contains a custom ID that you can control. You'll then be able to use the ID you've just set for your logic but you just then manually commit() or rollback() the transaction when you're done

I have a similar problem as you do and seeing that there is no solution, what might work is that each Spring transaction is bound to a thread, so use either thread-local storage, or use unique thread-id as the key. For these solutions, you have to be sure that on commit the TX specific info will be cleared from the thread local storage.

Related