Propagating the Hibernate (Web hosting domains) Session You ve tried the examples
Propagating the Hibernate Session You ve tried the examples in previous chapters and stored and loaded objects inside transactions. Very likely you ve noticed that code examples of five lines are excellent to help you understand a particular issue and learn an API and how objects change their state. If you take the next step and try to apply what you ve learned in your own application, you ll probably soon realize that you re missing two important concepts. The first concept we ll show you in this chapter persistence context propagation is useful when you have to call several classes to complete a particular action in your application and they all need database access. So far, we had only a single method that opened and closed a persistence context (a Session or an Entity- Manager) internally. Instead of passing the persistence context between classes and methods manually, we ll show you the mechanisms in Hibernate and Java Persistence that can take care of propagation automatically. Hibernate can help you to create more complex units of work. The next design problem you ll run into is that of application flow when your application user has to be guided through several screens to complete a unit of work. You must create code that controls the navigation from screen to screen however, this is outside of the scope of persistence, and we won t have much to say about it in this chapter. What is partly the responsibility of the persistence mechanism is the atomicity and isolation of data access for a unit of work that spans possible user think-time. We call a unit of work that completes in several client/server request and response cycles a conversation. Hibernate and Java Persistence offer several strategies for the implementation of conversations, and in this chapter we show you how the pieces fit together with realistic examples. We start with Hibernate and then, in the second half of the chapter, discuss JPA conversations. Let s create more complex data access examples first, to see how several classes can reuse the same persistence context through automatic propagation. 11.1 Propagating the Hibernate Session Recall the use case we introduced in the previous chapter: An event that triggers the end of an auction has to be processed (chapter 10, section 10.1, Transaction essentials ). For the following examples, it doesn t matter who triggered this event; probably an automatic timer ends auctions when their end date and time is reached. It could also be a human operator who triggers the event.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.