title: JPA with JTA how to persist many entites in one transaction
body: <p>I have a list of objects. They are JPA "Location" entities.</p>
  
  <pre><code>List&lt;Location&gt; locations;
  </code></pre>
  
  <p>I have a stateless EJB which loops thru the list and persists each one.</p>
  
  <pre><code>public void createLocations() {
      List&lt;Locations&gt; locations = getListOfJPAManagedLocationEntities(); // I'm leaving out the details of this because it has nothing to do with the issue
  
      for(Location location : locations) {
          em.persist(location);
      }
  }
  </code></pre>
  
  <p>The code works fine. I do not have any problems.
  However, the issue is: I want this to be an all-or-none transaction. Currently, each time thru the for loop, the persist() method will insert a new row into the database. Suppose I have 100 location objects and the 54th object has something wrong with it and an exception is thrown.  There will be 53 records inserted into the database.  What I want is: all of them must succeed before any of them succeed.</p>
  
  <p>I'm using the latest &amp; greatest version of Java EE6, EJB 3.x., and JPA 2.  My persistence.xml uses JTA.</p>
  
  <pre><code>&lt;persistence-unit name="myPersistenceUnit" transaction-type="JTA"&gt;
  </code></pre>
  
  <p>And I like having JTA.
  I do not want to stop using JTA.
  90% of the time JTA does exactly what I want it to do. But in this case, I doesn't seem to.</p>
  
  <p>My understanding of JTA must be inaccurate because I always thought the beginning and end of the EJB method marked the boundaries of the JTA transaction (assume only one method is in-play as I've shown above). By my logic, the transaction would not end until the for-loop is done and the method returns, and then at that point the records are persisted.</p>
  
  <p>I'm using the JTDS driver for SqlServer 2008. Perhaps the database doesn't want to insert a record without immediately committing it. The entity id is defined like this:</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  </code></pre>
  
  <p>I've checked the spec., and it is not proper to call the various "UserTransaction" or "getTransaction()" methods in a JTA environment.</p>
  
  <p>So what can I do?
  Thanks.</p>