title: 'No active transaction' and 'Cannot use an EntityTransaction while using JTA' when persisting object in filter?
body: <p>I want to log all requests to my web server using a filter. </p>
  
  <p>At first I tried the normal way I used in my EJBs and web service classes. My program fails as soon as I attempt to persist. I AM able to query (not shown here):</p>
  
  <pre><code>@PersistenceContext(unitName = "ConsoleManagement")
  protected EntityManager em;
  ...
  private void someMethod(){
          Request userRequest = new Request();
          userRequest.setApikey(apikey);
          userRequest.setFormat(requestedFormat);
          userRequest.setMethod(method);
          userRequest.setRequestIp(ip);
          userRequest.setRequestUrl(requestUrl);
          em.persist(userRequest); // fails here
          em.flush();
  }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>[ERROR   ] SRVE0315E: An exception occurred: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: javax.persistence.TransactionRequiredException: No active transaction for PuId=WebApi#WebApi.war#PersistenceUnitName
      at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:250)
  </code></pre>
  
  <p>From this I gathered (although could not find any documentation) that filters are not container managed.</p>
  
  <p>I then tried the two following:</p>
  
  <pre><code>@PersistenceContext(unitName = "ConsoleManagement")
  protected EntityManager em;
  ...
  private void someMethod(){
          Request userRequest = new Request();
          userRequest.setApikey(apikey);
          userRequest.setFormat(requestedFormat);
          userRequest.setMethod(method);
          userRequest.setRequestIp(ip);
          userRequest.setRequestUrl(requestUrl);
          em.getTransaction().begin(); // fails here
          em.persist(userRequest);
          em.flush();
          em.getTransaction().commit()
  }
  
  
  @PersistenceUnit(unitName = "ConsoleManagement")
  EntityManagerFactory emf;
  
  public void someMethod() {
      EntityManager em = emf.createEntityManager();
      em.getTransaction().begin(); // fails here
      em.persist(userRequest);
      em.flush();
      em.getTransaction().commit();
      em.close();
  }
  </code></pre>
  
  <p>Error in both cases:</p>
  
  <pre><code>[ERROR   ] SRVE0315E: An exception occurred: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: java.lang.IllegalStateException: 
  Exception Description: Cannot use an EntityTransaction while using JTA.
  </code></pre>
  
  <p>[EDIT] In the case that I inject an EntityManagerFactory via @PersistenceUnit and remove the em.getTransaction().begin(), I get a different error.</p>
  
  <pre><code>@PersistenceUnit(unitName = "ConsoleManagement")
  EntityManagerFactory emf;
  
  public void someMethod() {
      EntityManager em = emf.createEntityManager();
      //em.getTransaction().begin(); 
      em.persist(userRequest);
      em.flush(); // fails here now
      //em.getTransaction().commit();
      em.close();
  }
  </code></pre>
  
  <p>Error: </p>
  
  <pre><code>[ERROR   ] SRVE0315E: An exception occurred: com.ibm.ws.webcontainer.webapp.WebAppErrorReport: javax.persistence.TransactionRequiredException: 
  Exception Description: No externally managed transaction is currently active for this thread
  </code></pre>
  
  <p>I'm a little stuck now.</p>
  
  <p>[EDIT]
  Persistence unit:</p>
  
  <pre><code>&lt;persistence-unit name="ConsoleManagement"&gt;
      &lt;description&gt;This unit manages user data including user information, user projects, and project api keys&lt;/description&gt;
      &lt;jta-data-source&gt;jdbc/db2dbcpdb/develop&lt;/jta-data-source&gt;
      &lt;class&gt;com.utoronto.websphere.jaxrs.entities.Project&lt;/class&gt;
      &lt;class&gt;com.utoronto.websphere.jaxrs.entities.User&lt;/class&gt;
      &lt;class&gt;com.utoronto.websphere.jaxrs.entities.Api&lt;/class&gt;
      &lt;class&gt;com.utoronto.websphere.jaxrs.entities.Apikey&lt;/class&gt;
      &lt;class&gt;com.utoronto.websphere.jaxrs.entities.Version&lt;/class&gt;
      &lt;class&gt;com.utoronto.websphere.jaxrs.entities.Request&lt;/class&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
  &lt;/persistence-unit&gt;
  </code></pre>