title: Servlet does not persist an Entity
body: <p><strong>What do I want to do:</strong></p>
  
  <p>I just started with JPA and therefore i want to understand how persisting Entitys from different perspectives work (Application-Managed, Container-Managed). Application-Managed works fine, however i have problems persisting an Entity with an Entity Manager from within a Web Application using a TestServlet.</p>
  
  <p><strong>What have I done so far:</strong></p>
  
  <p>IDE: NetBeans 8.0.2
  Server: GlassFish 4.1
  Mysql 5.6
  JPA 2.1: EclipseLink</p>
  
  <ol>
  <li>I've created a Web Application Project.</li>
  <li>I've imported the Entitys from the Database using the NetBeans Wizard.
  For some reason I am not able to import them from jdbc/__default.
  I have to create a new Data Source. In this case it was jdbc/__onlineshop</li>
  </ol>
  
  <p>I simply want to create an Entity within a Servlet and persist it in the database just to understand how it works before I move on but apparently I don't quite get it.</p>
  
  <p>I am connected to the database and i can access it via a Java-Application but not using a Servlet from a Web-Container.</p>
  
  <p>The MySQL Driver is in the classpath MAIN - Libraries.</p>
  
  <p>No Exceptions are thrown. The Servlet is called which I am able to see in the browser. However the redirection through the RequestDispatcher does not work either, the browsers URL stays at web-jpa/test.</p>
  
  <p><strong>My Question:</strong></p>
  
  <p>Is there anything wrong with my Servlet or the persistence.xml since persisting does not work ?</p>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/ persistence" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
           http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
  
  &lt;persistence-unit name="jpa-webPU" transaction-type="JTA"&gt;
   &lt;jta-data-source&gt;jdbc/__onlineshop&lt;/jta-data-source&gt;
   &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
   &lt;properties/&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>TestServlet.java :</p>
  
  <pre><code>package jpa;
  
  import java.io.IOException;
  import javax.annotation.Resource;
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.PersistenceUnit;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.transaction.UserTransaction;
  
  @WebServlet("/test")
  public class TestServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
  
  @PersistenceUnit
  EntityManagerFactory emf;
  
  @Resource
  private UserTransaction ut;
  
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
  
      EntityManager em = emf.createEntityManager();
  
      Customer customer = new Customer();
  
      customer.setEmail("email@test.com");
      customer.setPassword("password");
  
      try 
      {
          ut.begin();
          em.persist(customer);
          ut.commit();
  
      } catch (Exception e) {
  
          try 
          {
              ut.rollback();
  
          } catch (Exception e1) 
          {
              e1.printStackTrace();
          }
  
          throw new ServletException(e.getMessage());
  
      }finally
      {
          em.close();
      }
  
      RequestDispatcher dispatcher = request.getRequestDispatcher    ("index.html");
      dispatcher.forward(request, response);
  
  }
  }
  </code></pre>
  
  <p>Thank you !</p>
  
  <p>p.s.: Any Hint is appreciated !</p>