Transaction essentials utx.rollback(); } catch (RuntimeException rbEx) {

Transaction essentials utx.rollback(); } catch (RuntimeException rbEx) { log.error(”Couldn’t roll back transaction”, rbEx); } throw ex; } finally { em.close(); } The persistence context of the EntityManager is scoped to the JTA transaction. All SQL statements flushed by this EntityManager are executed inside the JTA transaction on a database connection that is enlisted with the transaction. The persistence context is flushed and closed automatically when the JTA transaction commits. You could use several EntityManagers to access several databases in the same system transaction, just as you d use several Sessions in a native Hibernate application. Note that the scope of the persistence context changed! It s now scoped to the JTA transaction, and any object that was in persistent state during the transaction is considered detached once the transaction is committed. The rules for exception handling are equivalent to those for resource-local transactions. If you use JTA in EJBs, don t forget to set @TransactionManagement(TransactionManagementType.BEAN) on the class to enable BMT. You won t often use Java Persistence with JTA and not also have an EJB container available. If you don t deploy a stand-alone JTA implementation, a Java EE 5.0 application server will provide both. Instead of programmatic transaction demarcation, you ll probably utilize the declarative features of EJBs. Java Persistence and CMT Let s refactor the ManageAuction EJB session bean from the earlier Hibernate- only examples to Java Persistence interfaces. You also let the container inject an EntityManager: @Stateless public class ManageAuctionBean implements ManageAuction { @PersistenceContext(unitName = “auctionDB”) private EntityManager auctionEM; @PersistenceContext(unitName = “billingDB”) private EntityManager billingEM; @TransactionAttribute(TransactionAttributeType.REQUIRED) public void endAuction(Item item) throws AuctionNotValidException { concludeAuction(auctionEM, item);
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Leave a Reply