Archive for October, 2007

Make a web site - CHAPTER 11 Implementing conversations public void endAuction(Item item)

Wednesday, October 31st, 2007

CHAPTER 11 Implementing conversations public void endAuction(Item item) { // Reattach item itemDAO.makePersistent(item); // Set winning bid Bid winningBid = itemDAO.getMaxBid( item.getId() ); item.setWinningBid(winningBid); item.setBuyer( winningBid.getBidder() ); // Charge seller Payment payment = new Payment(item); paymentDAO.makePersistent(payment); // Notify seller and winner … } … } The current Session is bound to the transaction that is started for the endAuction() method, and it s flushed and closed when this method returns. All code that runs inside this method and calls sessionFactory.getCurrentSession() gets the same persistence context. If you compare this example with the first nonworking example, listing 11.1, you ll see that you had to add only some annotations to make it work. The @TransactionAttribute is even optional it defaults to REQUIRED. This is why EJB 3.0 offers a simplified programming model. Note that you haven t used a JPA interface so far; the data access classes still rely on the current Hibernate Session. You can refactor this easily later concerns are cleanly separated. You now know how the persistence context is scoped to transactions to serve a particular event and how you can create more complex data-access operations that require propagation and sharing of a persistence context between several objects. Hibernate internally uses the current thread or the current JTA system transaction to bind the current Sessionand persistence context. The scope of the Session and persistence context is the same as the scope of the Hibernate Transaction or JTA system transaction. We now focus on the second design concept that can significantly improve how you design and create database applications. We ll implement long-running conversations, a sequence of interactions with the application user that spans user think-time.
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Propagating the Hibernate Session utx.rollback(); } catch (Exception (Make web site)

Tuesday, October 30th, 2007

Propagating the Hibernate Session utx.rollback(); } catch (Exception rbEx) { log.error(”Couldn’t roll back transaction”, rbEx); } throw ex; } } … } This code is free from any Hibernate imports. And, more important, the ItemDAO and PaymentDAO classes, which internally use getCurrentSession(), are unchanged. A new persistence context begins when getCurrentSession() is called for the first time in one of the DAO classes. The current Session is bound automatically to the current JTA system transaction. When the transaction completes, either through commit or rollback, the persistence context is flushed and the internally bound current Session is closed. The exception handling in this code is slightly different compared to the previ ous example without JTA, because the UserTransaction API may throw checked exceptions (and the JNDI lookup in the constructor may also fail). You don t have to enable this JTA-bound persistence context if you configure your Hibernate application for JTA; getCurrentSession() always returns a Ses sion scoped and bound to the current JTA system transaction. (Note that you can t use the Hibernate Transaction interface together with the getCurrentSession() feature and JTA. You need a Session to call begin- Transaction(), but a Session must be bound to the current JTA transaction a chicken and egg problem. This again emphasizes that you should always use JTA when possible and Hibernate Transaction only if you can t use JTA.) 11.1.4 Propagation with EJBs If you write your controller as an EJB and apply container-managed transactions, the code (in listing 11.4) is even cleaner. Listing 11.4 Transaction demarcation with CMT in the controller @Stateless public class ManageAuctionBean implements ManageAuction { ItemDAO itemDAO = new ItemDAO(); PaymentDAO paymentDAO = new PaymentDAO(); @TransactionAttribute(TransactionAttributeType.REQUIRED)
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Cool web site - CHAPTER 11 Implementing conversations pattern.) You have to

Monday, October 29th, 2007

CHAPTER 11 Implementing conversations pattern.) You have to enable this binding in your Hibernate configuration by setting the hibernate.current_session_context_class property to thread. If you deploy your application with JTA, you can enable a slightly different strategy that scopes and binds the persistence context directly to the system transaction. 11.1.3 Propagation with JTA In previous sections, we always recommended the JTA service to handle transactions, and we now repeat this recommendation. JTA offers, among many other things, a standardized transaction demarcation interface that avoids the pollution of code with Hibernate interfaces. Listing 11.3 shows the ManageAuction controller refactored with JTA. Listing 11.3 Transaction demarcation with JTA in the controller public class ManageAuction { UserTransaction utx = null; ItemDAO itemDAO = new ItemDAO(); PaymentDAO paymentDAO = new PaymentDAO(); public ManageAuction() throws NamingException { utx = (UserTransaction) new InitialContext() .lookup(”UserTransaction”); } public void endAuction(Item item) throws Exception { try { // Begin unit of work utx.begin(); // Reattach item itemDAO.makePersistent(item); // Set winning bid Bid winningBid = itemDAO.getMaxBid( item.getId() ); item.setWinningBid(winningBid); item.setBuyer( winningBid.getBidder() ); // Charge seller Payment payment = new Payment(item); paymentDAO.makePersistent(payment); // Notify seller and winner … // End unit of work utx.commit(); } catch (Exception ex) { try {
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Propagating the Hibernate Session item.setWinningBid(winningBid); item.setBuyer( winningBid.getBidder() ); (Web hosting comparison)

Sunday, October 28th, 2007

Propagating the Hibernate Session item.setWinningBid(winningBid); item.setBuyer( winningBid.getBidder() ); // Charge seller Payment payment = new Payment(item); paymentDAO.makePersistent(payment); // Notify seller and winner … // End unit of work sf.getCurrentSession().getTransaction().commit(); } catch (RuntimeException ex) { try { sf.getCurrentSession().getTransaction().rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn’t roll back transaction,” rbEx); } throw ex; } } … } The unit of work starts when the endAuction() method is called. If sessionFactory.getCurrentSession() is called for the first time in the current Java thread, a new Session is opened and returned you get a fresh persistence context. You immediately begin a database transaction on this new Session, with the Hibernate Transaction interface (which translates to JDBC transactions in a Java SE application). All the data-access code that calls getCurrentSession() on the global shared SessionFactory gets access to the same current Session if it s called in the same thread. The unit of work completes when the Transaction is committed (or rolled back). Hibernate also flushes and closes the current Session and its persistence context if you commit or roll back the transaction. The implication here is that a call to getCurrentSession() after commit or rollback produces a new Session and a fresh persistence context. You effectively apply the same scope to the database transaction and the persistence context. Usually, you ll want to improve this code by moving the transaction and exception handling outside of the method implementation. A straightforward solution is a transaction interceptor; you ll write one in chapter 16. Internally, Hibernate binds the current Session to the currently running Java thread. (In the Hibernate community, this is also known as the ThreadLocal Session
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Free web host - CHAPTER 11 Implementing conversations 11.1.2 Propagation through thread-local

Saturday, October 27th, 2007

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.

Web hosting domain - Propagating the Hibernate Session objects called data access

Friday, October 26th, 2007

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.

CHAPTER 11 Implementing conversations To process the event, (My web server)

Thursday, October 25th, 2007

CHAPTER 11 Implementing conversations To process the event, you need to execute a sequence of operations: check the winning bid for the auction, charge the cost of the auction, notify the seller and winner, and so on. You could write a single class that has one big procedure. A better design is to move the responsibility for each of these steps into reusable smaller components and to separate them by concern. We ll have much more to say about this in chapter 16. For now, assume that you followed our advice and that several classes need to be called inside the same unit of work to process the closing of an auction. 11.1.1 The use case for Session propagation Look at the code example in Listing 11.1, which controls the processing of the event. Listing 11.1 Controller code that closes and ends an auction public class ManageAuction { ItemDAO itemDAO = new ItemDAO(); PaymentDAO paymentDAO = new PaymentDAO(); public void endAuction(Item item) { // Reattach item itemDAO.makePersistent(item); // Set winning bid Bid winningBid = itemDAO.getMaxBid( item.getId() ); item.setSuccessfulBid(winningBid); item.setBuyer( winningBid.getBidder() ); // Charge seller Payment payment = new Payment(item); paymentDAO.makePersistent(payment); // Notify seller and winner … } … } The ManageAuction class is called a controller. Its responsibility is to coordinate all the steps necessary to process a particular event. The method endAuction() is called by the timer (or user interface) when the event is triggered. The controller doesn t contain all the code necessary to complete and close the auction; it delegates as much as possible to other classes. First, it needs two stateless service
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Propagating the Hibernate (Web hosting domains) Session You ve tried the examples

Thursday, October 25th, 2007

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.

Implementing conversations This chapter covers Implementation of (Post office web site)

Wednesday, October 24th, 2007

Implementing conversations This chapter covers Implementation of conversations with Hibernate Implementation of conversations with JPA Conversations with EJB 3.0 components 476
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Summary Table 10.1 shows (Most popular web site) a summary you can

Tuesday, October 23rd, 2007

Summary Table 10.1 shows a summary you can use to compare native Hibernate features and Java Persistence. Table 10.1 Hibernate and JPA comparison chart for chapter 10 Hibernate Core Java Persistence and EJB 3.0 The TransactionAPI can be configured for JDBC and JTA. The EntityTransaction API is only useful with resource-local transactions. Hibernate can be configured to integrate with JTA and container-managed transactions in EJBs. With Java Persistence, no extra configuration besides the database connection name changes between Java SE and Java EE. Hibernate defaults to optimistic concurrency control for best scalability, with automatic versioning. Java Persistence standardizes optimistic concurrency control with automatic versioning. We ve now finished our discussion and exploration of the fundamentals involved in storing and loading objects in a transactional fashion. Next, we ll bring all the pieces together by creating more realistic conversations between the user and the application.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.