Sri lanka web server - Conversations with EJB 3.0 private EntityManager em; @TransactionAttribute(TransactionAttributeType.REQUIRED)

Conversations with EJB 3.0 private EntityManager em; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Bid getMaxBid(Long itemId) { return (Bid) em.createQuery(”…”).getSingleResult(); } … } The EJB container injects an EntityManager when a client of this bean calls get- MaxBid(). The persistence context for that EntityManager is the current persistence context (more about this soon). If no transaction is in progress when getMaxBid() is called, a new transaction is started and committed when getMax- Bid() returns. NOTE Many developers didn t use EJB session beans for DAO classes with EJB 2.1. In EJB 3.0, all components are plain Java objects and there is no reason you shouldn t get the container s services with a few simple annotations (or an XML deployment descriptor, if you don t like annotations). Wiring EJB components Now that ItemDAO is an EJB component (don t forget to also refactor PaymentDAO if you follow the examples from earlier conversation implementations with Hibernate), you can wire it into the also refactored ManageAuction component through dependency injection and wrap the whole operation in a single transaction: @Stateless public class ManageAuctionBean implements ManageAuction { @EJB ItemDAO itemDAO; @EJB PaymentDAO paymentDAO; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Item endAuction(Item item) { // Merge item itemDAO.makePersistent(item); // Set winning bid Bid winningBid = itemDAO.getMaxBid( item.getId() ); item.setWinningBid(winningBid); item.setBuyer( winningBid.getBidder() ); // Charge seller Payment payment = new Payment(item); paymentDAO.makePersistent(payment);
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply