Free web host - CHAPTER 11 Implementing conversations 11.1.2 Propagation through thread-local
CHAPTER 11 Implementing conversations 11.1.2 Propagation through thread-local Hibernate offers automatic persistence-context propagation for stand-alone Java applications with plain Java SE and for any application that uses JTA with or without EJBs. We strongly encourage you to consider this feature in your own application, because all the alternatives are less sophisticated. In Hibernate, you can access the current Session to access the database. For example, consider the ItemDAO implementation with Hibernate: public class ItemDAO { public Bid getMaxBid(Long itemId) { Session s = getSessionFactory().getCurrentSession(); return (Bid) s.createQuery(”…”).uniqueResult(); } … } The method getSessionFactory() returns the global SessionFactory. How it does that is entirely up to you and how you configure and deploy your application it could come from a static variable (HibernateUtil), be looked up from the JNDI registry, or be injected manually when the ItemDAO is instantiated. This kind of dependency management is trivial; the SessionFactory is a thread-safe object. The getCurrentSession() method on the SessionFactory is what we want to discuss. (The PaymentDAO implementation also uses the current Session in all methods.) What is the current Session, what does current refer to? Let s add transaction demarcation to the controller that calls ItemDAO and PaymentDAO, see listing 11.2. Listing 11.2 Adding transaction demarcation to the controller public class ManageAuction { ItemDAO itemDAO = new ItemDAO(); PaymentDAO paymentDAO = new PaymentDAO(); public void endAuction(Item item) { try { // Begin unit of work sf.getCurrentSession().beginTransaction(); // Reattach item itemDAO.makePersistent(item); // Set winning bid Bid winningBid = itemDAO.getMaxBid( item.getId() );
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.