CHAPTER 10 Transactions and (Free web servers) concurrency public void endAuction(Item

CHAPTER 10 Transactions and concurrency public void endAuction(Item item) { Session session1 = auctionDatabase.openSession(); Session session2 = billingDatabase.openSession(); concludeAuction(session1, item); billAuction(session2, item); } … } The container notices your declaration of a TransactionAttribute and applies it to the endAuction() method. If no system transaction is running when the method is called, a new transaction is started (it s REQUIRED). Once the method returns, and if the transaction was started when the method was called (and not by anyone else), the transaction commits. The system transaction is automatically rolled back if the code inside the method throws a RuntimeException. We again show two SessionFactorys for two databases, for the sake of the example. They could be assigned with a JNDI lookup (Hibernate can bind them there at startup) or from an enhanced version of HibernateUtil. Both obtain database connections that are enlisted with the same container-managed transaction. And, if the container s transaction system and the resources support it, you again get a two-phase commit protocol that ensures atomicity of the transaction across databases. You have to set some configuration options to enable CMT with Hibernate: The hibernate.transaction.factory_class option must be set to org. hibernate.transaction.CMTTransactionFactory. You need to set hibernate.transaction.manager_lookup_class to the right lookup class for your application server. Also note that all EJB session beans default to CMT, so if you want to disable CMT and call the JTA UserTransaction directly in any session bean method, annotate the EJB class with @TransactionManagement(TransactionManagementType. BEAN). You re then working with bean-managed transactions (BMT). Even if it may work in most application servers, mixing CMT and BMT in a single bean isn t allowed by the Java EE specification. The CMT code already looks much nicer than the programmatic transaction demarcation. If you configure Hibernate to use CMT, it knows that it should flush and close a Session that participates in a system transaction automatically. Furthermore, you ll soon improve this code and even remove the two lines that open a Hibernate Session. Let s look at transaction handling in a Java Persistence application.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Leave a Reply