16
votes

This is an academic question; I have no broken code in relation to this. I just want to expand my understanding of what is happening under the hood.

The code pattern I have been using (copied from books and tutorials) in my JPA DAO for my typical JSF web applications is basically this:

public class someDAO {

    @PersistenceContext protected EntityManager   em;
    @Resource           private   UserTransaction utx;    

    public void persist(Entity entity) {
        try {
            utx.begin();
            em.persist(entity);
            utx.commit();
        } catch ( // gawd awful long list of possible exceptions ) 

        // etc

So my questions are as follows:

  1. Why are the EntityManager instance and the UserTransaction instance injected with annotations from two seemingly unrelated packages?

  2. Why is the annotation @Resource and @PersistenceContext rather than @ManagedProperty or perhaps @Inject used?

  3. How does the persist() method access and interact with the utx object? If I forget the utx.begin() call the entity manager knows about it and throws and exception. It must be finding the UserTransaction object in some magic way. Wouldn't it have been better architecture to define the interface like: em.persist(utx, entity)?

  4. If utx is some sort of singleton -- is it possible to have more than one UserTransaction open at a time?

Much thanks for any discussion.

1
Did you consider EJB? EJBs use container managed transactions. This way you don't need to worry about managing transactions yourself (and it keeps your service methods free from try-catch clutter).BalusC
My code patterns are learned from books like Burns JSF 2. I don't mind this code pattern much because I have managed to keep all of it confined to one abstract DAO object. All my concrete DAOs extend from that one object so this junky (if that word applies) part of it is pretty much out of sight. But not out of mind.AlanObject

1 Answers

13
votes
  1. Because UserTransaction is part of Java Transaction API (JTA) and EntityManager is part of Java Persistence API (JPA). JTA is not part of JPA. JPA uses services provided by JTA.

  2. Isn't ManagedProperty is some annotation which is valid only in classes annotated with @ManagedBean. Maybe it was considered better to not inject UserTransaction different way in managed beans.

  3. JNDI lookup for active transaction. Reserved name seems to be java:comp/UserTransaction. One implementation: http://www.java2s.com/Open-Source/Java-Document/Database-ORM/hibernate/org/hibernate/transaction/JTATransactionFactory.java.htm

  4. It is not some sort of singleton, you can have more than one. But only one per thread can be active.