CHAPTER 11 Implementing conversations boolean sellerCanAffordIt = (Boolean)
CHAPTER 11 Implementing conversations boolean sellerCanAffordIt = (Boolean) em.createQuery(”select…”).getSingleResult(); tx.commit(); return sellerCanAffordIt; } … } The code snippet even includes two calls that trigger the flushing of the Entity- Manager s persistence context. First, FlushMode.AUTO means that the execution of the query triggers a flush. Second, the transaction commit triggers another flush. This obviously isn t what you want you want to make the whole conversation atomic and prevent any flushing before the last event is completed. Hibernate offers org.hibernate.FlushMode.MANUAL, which decouples transaction demarcation from the synchronization. Unfortunately, due to disagreements among the members of the JSR-220 expert group, javax.persistence.FlushMode only offers AUTO and COMMIT. Before we show you the official solution, here is how you can get FlushMode.MANUAL by falling back to a Hibernate API: // Prepare Hibernate-specific EntityManager parameters Map params = new HashMap(); params.put(”org.hibernate.flushMode,” “MANUAL”); // Begin persistence context with custom parameters EntityManager em = emf.createEntityManager(params); // Alternative: Fall back and disable automatic flushing ((org.hibernate.Session)em.getDelegate()) .setFlushMode(org.hibernate.FlushMode.MANUAL); // Begin conversation ManageAuction controller = new ManageAuction(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();
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.