title: Spring Transaction Manager not threadsafe?
body: <p>I'm currently experiencing an unexpected behaviour with the Spring transaction manager: the same transaction is assigned to multiple threads.
  The scenario is a CXF webservice running in a Tomcat server. The service itself is a Spring bean (singleton). When sending many parallel requests (in my test I used 10) to the server it sometimes happens that two requests (running in different threads) work with the same transaction. Since the logic is designed to use one transaction per request this behaviour causes a faulty behaviour of the application.</p>
  
  <p>Below my service class:</p>
  
  <pre><code>package ...clientsupport_v1;
  
  import...
  
  @WebService(targetNamespace = ...)
  @Transactional(rollbackFor = MyException.class)
  public class ClientSupportFacade extends AbstractServiceFacade implements ClientSupportService {
  
  private static final Logger LOG = LoggerFactory.getLogger(ClientSupportFacade.class);
  
  @Autowired
  private HibernateTransactionManager transactionManager;
  
  @Autowired
  private ClientOrderImporter clientOrderImporter;
  
  @Override
  public Advice receiveClientPreAdvice(User user, PreAdvice preAdvice) throws MyException {
      LOG.debug("PreAdvice: " + preAdvice + ", Transaction: " + getTransactionId() + ", Thread: "
              + Thread.currentThread().getName());
  ...
      return advice;
  }
  
  private int getTransactionId() {
      return transactionManager.getSessionFactory().getCurrentSession().getTransaction().hashCode();
  }
  
  }
  </code></pre>
  
  <p>receiveClientPreAdvice is the method exposed as service operation.</p>
  
  <p>When calling with multiple threads I get the following log lines: </p>
  
  <pre><code>2013-10-29 13:59:25.135 DEBUG e.h.p.s.c.c.ClientSupportFacade.receiveClientPreAdvice:42 - PreAdvice: PreAdvice [barcode=90000000002161, ...], Transaction: 420660542, Thread: http-bio-8080-exec-9
  2013-10-29 13:59:25.135 DEBUG e.h.p.s.c.c.ClientSupportFacade.receiveClientPreAdvice:42 - PreAdvice: PreAdvice [barcode=90000000002163, ...], Transaction: 420660542, Thread: http-bio-8080-exec-8
  </code></pre>
  
  <p>As you can see two different objects have been received and two different threads are running. Yet the transaction is the same.</p>
  
  <p>How can this happen? And - even more important - how can it be avoided?</p>