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

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.

Leave a Reply