CHAPTER 10 Transactions and concurrency billAuction(billingEM, item); } (Dedicated web hosting)
CHAPTER 10 Transactions and concurrency billAuction(billingEM, item); } … } Again, what happens inside the concludeAuction() and billAuction() methods isn t relevant for this example; assume that they need the EntityManagers to access the database. The TransactionAttribute for the endAuction() method requires that all database access occurs inside a transaction. If no system transaction is active when endAuction() is called, a new transaction is started for this method. If the method returns, and if the transaction was started for this method, it s committed. Each EntityManager has a persistence context that spans the scope of the transaction and is flushed automatically when the transaction commits. The persistence context has the same scope as the endAuction() method, if no transaction is active when the method is called. Both persistence units are configured to deploy on JTA, so two managed data base connections, one for each database, are enlisted inside the same transaction, and atomicity is guaranteed by the transaction manager of the application server. You declare that the endAuction()method may throw an AuctionNotValidException. This is a custom exception you write; before ending the auction, you check if everything is correct (the end time of the auction has been reached, there is a bid, and so on). This is a checked exception that is a subtype of java.lang. Exception. An EJB container treats this as an application exception and doesn t trigger any action if the EJB method throws this exception. However, the container recognizes system exceptions, which by default are all unchecked RuntimeExceptions that may be thrown by an EJB method. A system exception thrown by an EJB method enforces an automatic rollback of the system transaction. In other words, you don t need to catch and rethrow any system exception from your Java Persistence operations let the container handle them. You have two choices how you can roll back a transaction if an application exception is thrown: First, you can catch it and call the JTA UserTransaction manually, and set it to roll back. Or you can add an @ApplicationException(rollback = true) annotation to the class of AuctionNotValidException the container will then recognize that you wish an automatic rollback whenever an EJB method throws this application exception. You re now ready to use Java Persistence and Hibernate inside and outside of an application server, with or without JTA, and in combination with EJBs and con tainer-managed transactions. We ve discussed (almost) all aspects of transaction
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.