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

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.

Leave a Reply