title: Spring JPA : Application managed persistence context with @Transactional and @PersistenceContext
body: <p>Currently im trying out the application managed persistence context, by creating the entity manager manually and store them to enable transaction that spans multiple request calls (perhaps something like extended persistence context) in JSE application.</p>
  
  <p>But, im wondering whether i can avoid sending the entityManager object throughout the service and DAO methods as an additional parameter by making use of the spring's @PersistenceContext injection and mark the methods with @Transactional annotation to use the transaction started manually with that entity manager.</p>
  
  <p>I think i can somehow manage this by using a ThreadLocal for this feature, but i'll be happier to be able to attach this to the spring framework.</p>
  
  <p>This is an example of What i have in mind :</p>
  
  <hr>
  
  <p>The UI action method :</p>
  
  <p>Here we can see the transaction is started by the ui logic, since there iss no facade / command method in the backend to group these callings to the business logic :</p>
  
  <pre><code>Long transactionid = tool.beginTransaction();
  
  // calling business methods
  tool.callBusinessLogic("purchase", "receiveGoods", 
                          paramObject1, transactionid);
  
  tool.callBusinessLogic("inventory", "updateInventory", 
                          paramObject2, transactionid);
  
  tool.commitTransaction(transactionid);
  </code></pre>
  
  <hr>
  
  <p>Inside the tool :</p>
  
  <pre><code>public Long beginTransaction() {
    // create the entity --&gt; for the @PersistentContext
    Entitymanager entityManager = createEntityManagerFromFactory();
    long id = System.currentTimeMillis();
    entityManagerMap.put(id, entitymanager);
  
    // start the transaction --&gt; for the @Transactional ?
    entityManager.getTransaction().begin();
  
    return id;
  }
  
  public void commitTransaction(Long transactionId) {
    EntityManager entityManager = entityManagerMap.get(transactionId);
  
    entityManager.getTransaction().commit();
  }
  
  public Object callBusinessLogic(String module, String function, 
                          Object paramObject, Long transactionid) {
      EntityManager em = entityManagerMap.get(transactionId);
  
      // =================================
      //        HOW TO DO THIS????
      // =================================
      putEntityManagerIntoCurrentPersistenceContext(em);
  
      return executeBusinessLogic(module, function, paramObject, transactionid);
  }
  </code></pre>
  
  <hr>
  
  <p>And the example for the service method :</p>
  
  <pre><code>public class Inventory {
    // How can i get the entityManager that's been created by the tool for this thread ?
    @PersistenceContext
    private EntityManager entityManager;
  
    // How can i use the transaction with that transactionId ?
    @Transactional
    public void receiveGoods(Param param) {
      // ........
    }
  }
  </code></pre>
  
  <hr>
  
  <p>Is there anyway to achieve this ?</p>
  
  <p>Thank you !</p>