Web hosting domain - Propagating the Hibernate Session objects called data access
Propagating the Hibernate Session objects called data access objects (DAOs) to complete its work they re instantiated directly for each instance of the controller. The endAuction() method uses the DAOs when it needs to access the database. For example, the ItemDAO is used to reattach the detached item and to query the database for the highest bid. The PaymentDAO is used to make a transient new payment persistent. You don t even need to see how the seller and winner of the auction are notified you have enough code to demonstrate that context propagation is required. The code in listing 11.1 doesn t work. First, there is no transaction demarcation. All the code in endAuction() is to be considered an atomic unit of work: It either all fails or all completes successfully. So, you need to wrap a transaction around all these operations. You ll do that with different APIs next. A more difficult problem is the persistence context. Imagine that ItemDAO and PaymentDAO use a different persistence context in every method (they re stateless). In other words, itemDAO.getMaxBid() and paymentDAO.makePersistent() both open, flush, and close their own persistence context (a Session or an EntityManager). This is an anti-pattern that should be avoided at all times! In the Hibernate world, it s known as session-per-operation, and it s the first thing a good Hibernate developer should take care of when examining an application design for performance bottlenecks. A single persistence context shouldn t be used to process a particular operation, but the whole event (which, naturally, may require several operations). The scope of the persistence context is often the same scope as the database transaction. This is also known as session-per-request; see figure 11.1. Let s add transaction demarcation to the ManageAuctioncontroller and propagate the persistence context between data access classes. Figure 11.1 A particular event is served with a single persistence context.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.