title: Correct usage of JPA in jsp application
body: <p>I'm trying to develop a simple JSP based web application with JPA and would like to know the correct usage for developing one. </p>
  
  <p>In my sample application I have two JSP pages and a simple Java class to perform database operations. Both the JSP files use this Java class to perform DB operations.</p>
  
  <p>I've annotated this class with <code>@Stateless</code> and injected an Entity manager as follows:</p>
  
  <pre><code>@PersistenceContext(unitName = "myjpa") 
  EntityManager em;
  </code></pre>
  
  <p>In my <code>persistence.xml</code> I've set the following property:</p>
  
  <pre><code>&lt;property 
      name="hibernate.transaction.jta.platform"
      value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"
  /&gt;
  </code></pre>
  
  <p>I'm calling the class in JSP using JNDI (as the class is annotated for a stateless session bean) as follows:</p>
  
  <pre><code>InitialContext ic = new InitialContext();
  
  Sample sample = (Sample) ic.lookup("java:app/" + application.getContextPath() + "/Sample");
  </code></pre>
  
  <p>I'm facing the following scenarios:</p>
  
  <ol>
  <li><p>When I try to use a transaction <code>em.getTransaction().begin()/commit()</code> for insert and update, it says can not use transaction with JTA case.</p></li>
  <li><p>So in the constructor code of my Java class I use the following code:</p></li>
  </ol>
  
  <pre class="lang-java prettyprint-override"><code>Properties properties = new Properties();
  
  properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
  
  emf = Persistence.createEntityManagerFactory("myjpa",properties);
  
  em = emf.createEntityManager();
  </code></pre>
  
  <p>I tried to use transactions like <code>em.getTransaction().begin()/commit()</code>. 
  But in this case the pages become very slow after 2-3 database update and load operations. Though I'm not getting any exception. Overall in my table I'm having less than 25 records.</p>
  
  <p>To me it seems as if it is waiting internally for some operation to complete.</p>
  
  <p>At the same time I also feel that the way I'm using JPA is wrong and hence soliciting advice for the correct approach for doing even simple web apps with JSP and JPA.</p>
  
  <p>While I'm still exploring Java EE, in case you have any specific reference for such cases I'll like to read and look them too.</p>