View Javadoc
1   package info.ejava.examples.ejb.ejbjpa.ejb;
2   
3   import info.ejava.examples.ejb.ejbjpa.dao.JPAHotelDAO;
4   
5   import javax.annotation.PostConstruct;
6   import javax.annotation.Resource;
7   import javax.ejb.EJB;
8   import javax.ejb.EJBException;
9   import javax.ejb.Singleton;
10  import javax.ejb.Startup;
11  import javax.ejb.TransactionManagement;
12  import javax.ejb.TransactionManagementType;
13  import javax.persistence.EntityManager;
14  import javax.persistence.EntityManagerFactory;
15  import javax.persistence.PersistenceUnit;
16  import javax.transaction.UserTransaction;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  /**
22   * The following EJB provides an example of an EJB using BEAN-managed transactions. 
23   */
24  @Singleton
25  @Startup
26  @TransactionManagement(TransactionManagementType.BEAN)
27  public class HotelInitEJB implements HotelInitRemote {
28      private static final Logger logger = LoggerFactory.getLogger(HotelInitEJB.class);
29      
30      /**
31       * We will still get a resource from the container -- except in this case we
32       * will get an EMF rather than an EntityManager. Note the different @PersistenceUnit
33       * annotation as well.
34       */
35      @PersistenceUnit(unitName="ejbjpa-hotel")
36      private EntityManagerFactory emf;
37      
38      @EJB
39      HotelMgmtLocal hotelMgmt;
40      
41      /**
42       * We get a UTx injected to control our overall JTA transaction.
43       */
44      @Resource
45      private UserTransaction tx;
46      
47      @PostConstruct
48      public void init() {
49          logger.debug("*** HotelInit:init({})", super.hashCode());
50          clearAll();
51          populate();
52          logger.debug("we have {} floors", hotelMgmt.getFloors(0, 0).size());
53      }
54      
55      @Override
56      public void clearAll() {
57          logger.debug("clearing ejbjpa hotel");
58          EntityManager em=emf.createEntityManager();
59          try {
60              JPAHotelDAO dao = new JPAHotelDAO();
61              dao.setEntityManager(em);
62              tx.begin();
63              em.joinTransaction(); //tells the EM to join the JTA UTx we are managing
64              dao.clearAll();
65              tx.commit();
66              logger.debug("clearing complete");
67          } catch (Exception ex) {
68              try {
69                  tx.rollback();
70                  logger.debug("error clearing hotel", ex);
71              } catch (Exception ex2) {
72                  throw new EJBException(ex2);
73              }
74              finally {}
75          } finally {
76              if (em!=null) {
77                  em.close();
78              }
79          }
80      }
81  
82      @Override
83      public void populate() {
84          logger.debug("populating ejbjpa hotel");
85          EntityManager em=emf.createEntityManager();
86          try {
87              JPAHotelDAO dao = new JPAHotelDAO();
88              dao.setEntityManager(em);
89              tx.begin();
90              em.joinTransaction(); //tells the EM to join the JTA UTx we are managing
91              dao.populate();
92              tx.commit();
93              logger.debug("populate complete");
94          } catch (Exception ex) {
95              try {
96                  tx.rollback();
97                  logger.debug("error populating hotel", ex);
98              } catch (Exception ex2) {
99                  throw new EJBException(ex2);
100             }
101             finally {}
102         } finally {
103             if (em!=null) {
104                 em.close();
105             }
106         }
107     }
108 }