# How to inject the JPA EntityManager in a Tomcat-based web application?

**URL:** <https://discourse.hibernate.org/t/how-to-inject-the-jpa-entitymanager-in-a-tomcat-based-web-application/2304>\
**Category:** Hibernate ORM\
**Created:** [February 16, 2019, 1:02am UTC](https://discourse.hibernate.org/t/how-to-inject-the-jpa-entitymanager-in-a-tomcat-based-web-application/2304 "2019-02-16T01:02:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![masber](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/masber/32/132_2.png) [@masber](https://discourse.hibernate.org/u/masber)\
**Post date:** [February 16, 2019, 1:02am UTC](https://discourse.hibernate.org/t/how-to-inject-the-jpa-entitymanager-in-a-tomcat-based-web-application/2304/1 "2019-02-16T01:02:52Z")

</div>

Dear hibernate community,  
I am running into problems because tomcat is not a complaint java EE server, hence I need to manage entitymanager myself (application managed entitymanager). However hibernate throws an exception “org.hibernate.HibernateException: Flush during cascade is dangerous” when multiple threads works under the same tables… my question is is this the right way to manage entitymanager in a thread-safety.

```auto
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EntityManagerHelper {

    private static final EntityManagerFactory emf;
    private static final ThreadLocal<EntityManager> threadLocal;

    static {
        emf = Persistence.createEntityManagerFactory("Persistent_Name");
        threadLocal = new ThreadLocal<EntityManager>();
    }

    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 beginTransaction() {
        getEntityManager().getTransaction().begin();
    }

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

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

```

thank you very much

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [February 16, 2019, 12:11pm UTC](https://discourse.hibernate.org/t/how-to-inject-the-jpa-entitymanager-in-a-tomcat-based-web-application/2304/2 "2019-02-16T12:11:43Z")

</div>

It’s much better if you’re using a framework for that, so you have 2 choices:

1. You can use TomEE which implements the Java EE CDI spec
2. You can use Spring to inject the `EntityManager` and use declarative transaction boundaries
