CHAPTER 10 Transactions and concurrency utx.rollback(); } catch
CHAPTER 10 Transactions and concurrency utx.rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn’t roll back transaction”, rbEx); } throw ex; } finally { session1.close(); session2.close(); } (Note that this code snippet can throw some other, checked exceptions, like a NamingException from the JNDI lookup. You need to handle these accordingly.) First, a handle on a JTA UserTransaction must be obtained from the JNDI registry. Then, you begin and end a transaction, and the (container-provided) database connections used by all Hibernate Sessions are enlisted in that transaction automatically. Even if you aren t using the Transaction API, you should still configure hibernate.transaction.factory_class and hibernate.transaction. manager_lookup_class for JTA and your environment, so that Hibernate can interact with the transaction system internally. With default settings, it s also your responsibility to flush() each Session manually to synchronize it with the database (to execute all SQL DML). The Hibernate Transaction API did this automatically for you. You also have to close all Sessions manually. On the other hand, you can enable the hibernate.transaction.flush_before_completion and/or the hibernate.transaction.auto_ close_session configuration options and let Hibernate take care of this for you again flushing and closing is then part of the internal synchronization procedure of the transaction manager and occurs before (and after, respectively) the JTA transaction ends. With these two settings enabled the code can be simplified to the following: UserTransaction utx = (UserTransaction) new InitialContext() .lookup(”java:comp/UserTransaction”); Session session1 = null; Session session2 = null; try { utx.begin(); session1 = auctionDatabase.openSession(); session2 = billingDatabase.openSession(); concludeAuction(session1); billAuction(session2); utx.commit(); } catch (RuntimeException ex) {
Check Tomcat Web Hosting services for best quality webspace to host your web application.