Conversations with JPA Naturally, an interceptor that wraps (Cheapest web hosting)
Tuesday, November 20th, 2007Conversations with JPA Naturally, an interceptor that wraps the getAuction() and endAuction() methods and supplies the correct EntityManager instance can be more convenient. It also avoids the concern leaking upward to the presentation layer. You d get this interceptor for free if you wrote your controller as a stateful EJB session bean. When you try to apply this strategy with an extended persistence context that spans the whole conversation, you ll probably run into an issue that can break atomicity of the conversation automatic flushing. Preventing automatic flushing Consider the following conversation, which adds an event as an intermediate step: // Begin persistence context and conversation EntityManager em = emf.createEntityManager(); ManageAuctionExtended controller = new ManageAuctionExtended(em); // First event Item item = controller.getAuction( 1234l ); // Item is displayed on screen and modified… item.setDescription(”[SOLD] An item for sale”); // Second event if ( !controller.sellerHasEnoughMoney(seller) ) throw new RuntimeException(”Seller can’t afford it!”); // Third event controller.endAuction(item); // End persistence context and conversation em.close(); From looking at this new conversation client code, when do you think the updated item description is saved in the database? It depends on the flushing of the persistence context. You know that the default FlushMode in JPA is AUTO, which enables synchronization before a query is executed, and when a transaction is committed. The atomicity of the conversation depends on the implementation of the sellerHasEnoughMoney() method and whether it executes a query or commits a transaction. Let s assume you wrap the operations that execute inside that method with a regular transaction block: public class ManageAuctionExtended { … public boolean sellerHasEnoughMoney(User seller) { EntityTransaction tx = em.getTransaction(); tx.begin();
We recommend high quality webhost to host and run your jsp application: christian web host services.