Windows 2003 server web - CHAPTER 11 Implementing conversations Disabling flushing by disabling
CHAPTER 11 Implementing conversations Disabling flushing by disabling transactions The official solution, according to the EJB 3.0 specification, mixes these two concerns. You prevent automatic flushing by making all steps of the conversation (except the last one) nontransactional: @Stateful @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public class ManageAuctionBean implements ManageAuction { @PersistenceContext(type = PersistenceContextType.EXTENDED) EntityManager em; public Item getAuction(Long itemId) { return em.find(Item.class, itemId); } public boolean sellerHasEnoughMoney(User seller) { boolean sellerCanAffordIt = (Boolean) em.createQuery(”select…”).getSingleResult(); return sellerCanAffordIt; } @Remove @TransactionAttribute(TransactionAttributeType.REQUIRED) public void endAuction(Item item, User buyer) { // Set winning bid // Charge seller // Notify seller and winner item.setBuyer(…); } } In this implementation, you switch to a different default for all methods, TransactionAttributeType.NOT_SUPPORTED, and require a transaction only for the endAuction() method. This last transaction also flushes the persistence context at commit time. All methods that now call the EntityManager without transactions are effectively running in autocommit mode, which we discussed in the previous chapter. Complex transaction assemblies You ve now used a few different TransactionAttributeType annotations; see the complete list of available options in Table 11.1. The most commonly used transaction attribute type is REQUIRED, which is the default for all stateless and stateful EJB methods. To disable automatic flushing of the extended persistence context for a method in a stateful session bean, switch to NOT_SUPPORTED or even NEVER.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.