java.lang.IllegalStateException: Transaction already active, Transaction not active
<p>I have got a JavaFX application, which gets access to MySQL database with the help of JPA technology. </p>

<p><strong>Here is how I create and use entity manager in my application:</strong></p>

<pre><code>import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EntityManagerHelper {

    private static final EntityManagerFactory emf;
    private static final ThreadLocal&lt;EntityManager&gt; threadLocal;

    static {
        if (InitPersistence.persistenceMap != null &amp;&amp; InitPersistence.getNewIP() != null)
            emf = Persistence.createEntityManagerFactory("TonalityJPA",
                    InitPersistence.persistenceMap);
        else
            emf = Persistence.createEntityManagerFactory("TonalityJPA");
        threadLocal = new ThreadLocal&lt;EntityManager&gt;();
    }

    public static EntityManager getEntityManager() {
        EntityManager em = threadLocal.get();

        if (em == null) {
            em = emf.createEntityManager();
            // set your flush mode here
            threadLocal.set(em);
        }
        return em;
    }

    public static void closeEntityManager() {
        EntityManager em = threadLocal.get();
        if (em != null) {
            em.close();
            threadLocal.set(null);
        }
    }

    public static void closeEntityManagerFactory() {
        emf.close();
    }


    public static void begin() {
        getEntityManager().getTransaction().begin();
    }

    public static &lt;T&gt; void remove(T thingToRemove) {
        getEntityManager().remove(thingToRemove);
    }

    public static &lt;T&gt; void persist(T thingToPersist) {
        getEntityManager().persist(thingToPersist);
    }

    public static void rollback() {
        getEntityManager().getTransaction().rollback();
    }

    public static void commit() {
        getEntityManager().getTransaction().commit();

    }

    public static &lt;T&gt; T find(Class&lt;T&gt; a, long id) {
        return getEntityManager().find(a, id);
    }
}
</code></pre>

<p>I took this class from the answer to this post <a href="https://stackoverflow.com/questions/14888040/java-an-entitymanager-object-in-a-multithread-environment/18698497#18698497">JAVA: an EntityManager object in a multithread environment</a> and added some functions to it.</p>

<p>My application goes through authorization through DB, then shows the list of users from DB. 
<strong>Application user chooses one of them and deletes him. 
Here how it works:</strong></p>

<pre><code>delBtn.setOnAction(event -&gt; { 
                long id = currentUser.getUserId();
                new UserActions().delete(id);
                UserLogEntity userLog = new UserLogEntity(UserData.getUser().getUserId(), "deleted user: " +
                        currentUser.getUserLogin(), 3, new Date());
                UserLogActions log_action = new UserLogActions();
                log_action.add(userLog);
            } else {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Помилка");
                alert.setHeaderText("");
                alert.setContentText("Оберіть користувача для видалення");

                alert.showAndWait();
            }

        });
</code></pre>

<p>And here are the UserEntity and UserActions classes:</p>

<p><strong>UserEntity:</strong></p>

<pre><code>package com.Model.DataBase.Entities;

import org.mindrot.jbcrypt.BCrypt;

import javax.persistence.*;

/**
 * Created by User on 07.03.2016.
 */
@Entity
@Table(name = "user", schema = "newsmonitoringdb")
public class UserEntity {
    private Long userId;
    private String userLogin;
    private String userPass;
    private int userAccessLvl;

    public UserEntity(){}

    public UserEntity(String login, String pass, int accessLvl)
    {
        this.userLogin = login;
        this.userPass = pass;
        this.userAccessLvl = accessLvl;
    }    

    @Id
    @Column(name = "userID")
    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Basic
    @Column(name = "userLogin")
    public String getUserLogin() {
        return userLogin;
    }

    public void setUserLogin(String userLogin) {
        this.userLogin = userLogin;
    }

    @Basic
    @Column(name = "userPass")
    public String getUserPass() {
        return userPass;
    }

    public void setUserPass(String userPass) {
        this.userPass = userPass;
    }

    @Basic
    @Column(name = "userAccessLvl")
    public int getUserAccessLvl() {
        return userAccessLvl;
    }
}
</code></pre>

<p><strong>UserAction:</strong></p>

<pre><code>package com.Model.DataBase.EntitiesActions;

import com.Model.DataBase.Entities.UserEntity;
import com.Model.DataBase.EntityManagerHelper;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

public class UserActions{

    public List&lt;UserEntity&gt; list() {
        Query query = EntityManagerHelper.getEntityManager().createQuery("SELECT a FROM UserEntity a", UserEntity.class);
        return  (List &lt;UserEntity&gt;) query.getResultList();

    }

    public void add(UserEntity user) {
        try {
            EntityManagerHelper.begin();
            EntityManagerHelper.persist(user);
            EntityManagerHelper.commit();

        } catch (Exception ex) {
            //entityManager.getTransaction().rollback();
            //ex.printStackTrace();
        }
    }

    public void delete(long id) {
        try {
            UserEntity user = EntityManagerHelper.find(UserEntity.class, id);
            EntityManagerHelper.begin();
            EntityManagerHelper.remove(user);
            EntityManagerHelper.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            EntityManagerHelper.rollback();
        }
    }
}
</code></pre>

<p>In case if I have</p>

<pre><code>EntityManagerHelper.begin();
</code></pre>

<p>in delete method, I am send to the catch block at this line with two exceptions:</p>

<pre><code>javax.persistence.RollbackException: Transaction marked as rollbackOnly
</code></pre>

<p>and</p>

<pre><code>Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Transaction not active
</code></pre>

<p>I understand the first one, but the second one is unexpected.</p>

<p>if I take away this line, I enter the catch block from the line:</p>

<pre><code>EntityManagerHelper.commit();
</code></pre>

<p>The first exception is the same, the second exception is 
Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Transaction not active</p>

<p>How can I solve this issue to get no exceptions?</p>