2

I am writing a CDI-JPA DAO pattern that not using EJB because I am using Tomcat. Here is my code:

@ApplicationScoped
public class UserDao {

    @PersistenceContext(unitName = "unitName1")
    EntityManager entityManager;

    public void saveUser(User user) {
        this.entityManager.persist(user);
    }

    public void removeUser(User user) {
        this.entityManager.remove(user);
    }

    public void getUser(int id) {
        this.entityManager.find(User.class, id);
    }
}

Since all my DAO classes are annotated with @ApplicationScoped so I was wondering whether IT IS SAFE to inject entityManager using @PersistenceContext as I did? Can someone tell me is that ok? If NOT, please give me your ideas.

palacsint
  • 28,416
  • 10
  • 82
  • 109
  • 2
    possible duplicate of [@ApplicationScoped CDI bean and @PersistenceContext - is this safe?](http://stackoverflow.com/questions/13885918/applicationscoped-cdi-bean-and-persistencecontext-is-this-safe) – Fabricio Lemos Jan 27 '14 at 19:01
  • 1
    Not directly related to your question. But you'll probably have issues by developing a CDI DAO under tomcat. Tomcat doesn't provide a transaction manager and CDI doesn't include one. It allows to use one without EJB thru the new @Transactional included in JTA 1.2 but tomcat doesn't have JTE implementation included. for that reason, I would recommend you to use TomEE instead of Tomcat, you'll have CDI, JPA and JTA included without having to bother their integration. – Antoine Sabot-Durand Jan 28 '14 at 08:16
  • The Deltaspike Module for JPA provides @Transactional – Karl Kildén Jan 29 '14 at 13:15

0 Answers0