title: Is there a better way of detecting if a Spring DB transaction is active than using TransactionSynchronizationManager.isActualTransactionActive()?
body: <p>I have some legacy code that I am now trying to re-use under Spring. This code is deeply nested in other code so it's not practical to redesign and is called in many situations, only some of which are through Spring. What I'd like to do is to use the Spring transaction if one has been started; otherwise, continue to use the existing (legacy) db connection mechanism. Our first thought was to make our legacy class a bean and use an injected <code>TransactionPlatformManager</code>, but that does not seem to have any methods germane to our situation. Some research showed that Spring has a class called <code>TransactionSynchronizationManager</code> which has a static method <code>isActualTransactionActive()</code>. My testing indicates this method is a reliable way of detecting if a Spring transaction is active:</p>
  
  <ul>
  <li>When called via Spring service without the <code>@Transactional</code> annotation, it returns false</li>
  <li>When called via Spring service with <code>@Transactional</code>, it returns true</li>
  <li>In my legacy method called the existing way, it returns false</li>
  </ul>
  
  <p>My question: Is there a better way of detecting if a transaction is active?</p>