Conversations with Hibernate could write your own implementation

Conversations with Hibernate could write your own implementation and name the class in the configuration. This usually isn t necessary, because the built-in options cover most cases. The Hibernate built-in implementation you just enabled is called managed because it delegates the responsibility for managing the scope, the start and end of the current Session, to you. You manage the scope of the Session with three static methods: public class ManagedSessionContext implements CurrentSessionContext { public static Session bind(Session session) { … } public static Session unbind(SessionFactory factory) { … } public static boolean hasBind(SessionFactory factory) { … } } You can probably already guess what the implementation of a session-per-conversation strategy has to do: When a conversation starts, a new Session must be opened and bound with ManagedSessionContext.bind() to serve the first request in the conversation. You also have to set FlushMode.MANUAL on that new Session, because you don t want any persistence context synchronization to occur behind your back. All data-access code that now calls sessionFactory.getCurrentSession() receives the Session you bound. When a request in the conversation completes, you need to call Managed- SessionContext.unbind() and store the now disconnected Session somewhere until the next request in the conversation is made. Or, if this was the last request in the conversation, you need to flush and close the Session. All these steps can be implemented in an interceptor. Creating a conversation interceptor You need an interceptor that is triggered automatically for each request event in a conversation. If you use EJBs, as you ll do soon, you get much of this infrastructure code for free. If you write a non- Java EE application, you have to write your own interceptor. There are many ways how to do this; we show you an abstract interceptor that only demonstrates the concept. You can find working and tested interceptor implementations for web applications in the CaveatEmptor download in the org.hibernate.ce.auction.web.filter package. Let s assume that the interceptor runs whenever an event in a conversation has to be processed. We also assume that each event must go through a front door controller and its execute() action method the easiest scenario. You can now wrap an interceptor around this method; that is, you write an interceptor that is
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Leave a Reply