JPA and EJB - When do I need to use transaction?
<p>I'm learning persistence in Java following some tutorial.</p>

<p>I'm using Java EE 7 and Payara server.</p>

<p>I noticed that each uses a different method for persistence.</p>

<p>Examples:</p>

<ul>
<li><p>simple</p>

<pre><code>@Stateless
public class BookServiceBean implements BookService {
    @PersistenceContext
    private EntityManager em;

    public void createOrUpdate(Book book) {
        em.persist(book);
    }
    public void remove(Book book) {
        em.remove(book);
    }
}
</code></pre></li>
<li><p>with <code>flush()</code>, this is used when validation strategy isn't set on "AUTO" in <em>persistene.xml</em>, right?</p>

<pre><code>@Stateless
public class BookServiceBean implements BookService {
    @PersistenceContext
    private EntityManager em;

    public void createOrUpdate(Book book) {
        em.persist(book);
        em.flush();
    }
    public void remove(Book book) {
        em.remove(book);
        em.flush();
    }
}
</code></pre></li>
<li><p>with transaction</p>

<pre><code>@Stateless
public class BookServiceBean implements BookService {
    @PersistenceContext
    private EntityManager em;

    public void createOrUpdate(Book book) {
        utx.begin();
        em.persist(book);
        utx.commit();
    }
    public void remove(Book book) {
        utx.begin();
        em.remove(book);
        utx.commit();
    }
}
</code></pre></li>
</ul>

<p>When and why do I have to use the last one?</p>

<p>Is it necessary to use <code>em.close()</code> at the end of each method?</p>

<p>What are the good practices?  </p>