Archive for November, 2007

X web hosting - Conversations with EJB 3.0 Table 11.1 EJB 3.0

Friday, November 30th, 2007

Conversations with EJB 3.0 Table 11.1 EJB 3.0 declarative transaction attribute types Attribute name Description REQUIRED A method must be invoked with a transaction context. If the client doesn t have a transaction context, the container starts a transaction and enlists all resources (datasources, and so on) used with that transaction. If this method calls other transactional components, the transaction is propagated. The container commits the transaction when the method returns, before the result is send to the client. NOT_SUPPORTED If a method is invoked within the transaction context propagated from the client, the caller s transaction is suspended and reactivated when the method returns. If the caller has no transaction context, no transaction is started for the method. All used resources aren t enlisted with a transaction (autocommit occurs). SUPPORTS If a method is invoked within the transaction context propagated from the client, it joins that transaction context with the same result as REQUIRED. If the caller has no transaction context, no transaction is started, with the same result as NOT_SUPPORTED. This transaction attribute type should be used only for methods that can handle both cases correctly. REQUIRES_NEW A method is always executed inside a new transaction context, with the same consequences and behavior as with REQUIRED. Any propagated client transaction is suspended and resumes when the method returns and the new transaction is completed. MANDATORY A method must be called with an active transaction context. It then joins this transaction context and propagates it further, if needed. If no transaction context is present at call time, an exception is thrown. NEVER This is the opposite of MANDATORY. An exception is thrown if a method is called with an active transaction context. You have to be aware of the transaction- and persistence-context propagation rules when you design your conversations with stateful EJBs, or if you want to mix stateless and stateful components: If a stateful session bean that has an extended persistence context calls (effectively instantiates) another stateful session bean that also has a persistence context, the second stateful session bean inherits the persistence context from the caller. The lifecycle of the persistence context is bound to the first stateful session bean; it s closed when both session beans have been removed. This behavior is recursive if more stateful session beans are involved. This behavior is also independent of any transaction rules and transaction propagation.
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Windows 2003 server web - CHAPTER 11 Implementing conversations Disabling flushing by disabling

Thursday, November 29th, 2007

CHAPTER 11 Implementing conversations Disabling flushing by disabling transactions The official solution, according to the EJB 3.0 specification, mixes these two concerns. You prevent automatic flushing by making all steps of the conversation (except the last one) nontransactional: @Stateful @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public class ManageAuctionBean implements ManageAuction { @PersistenceContext(type = PersistenceContextType.EXTENDED) EntityManager em; public Item getAuction(Long itemId) { return em.find(Item.class, itemId); } public boolean sellerHasEnoughMoney(User seller) { boolean sellerCanAffordIt = (Boolean) em.createQuery(”select…”).getSingleResult(); return sellerCanAffordIt; } @Remove @TransactionAttribute(TransactionAttributeType.REQUIRED) public void endAuction(Item item, User buyer) { // Set winning bid // Charge seller // Notify seller and winner item.setBuyer(…); } } In this implementation, you switch to a different default for all methods, TransactionAttributeType.NOT_SUPPORTED, and require a transaction only for the endAuction() method. This last transaction also flushes the persistence context at commit time. All methods that now call the EntityManager without transactions are effectively running in autocommit mode, which we discussed in the previous chapter. Complex transaction assemblies You ve now used a few different TransactionAttributeType annotations; see the complete list of available options in Table 11.1. The most commonly used transaction attribute type is REQUIRED, which is the default for all stateless and stateful EJB methods. To disable automatic flushing of the extended persistence context for a method in a stateful session bean, switch to NOT_SUPPORTED or even NEVER.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Conversations with EJB 3.0 EntityManager em; public Item

Wednesday, November 28th, 2007

Conversations with EJB 3.0 EntityManager em; public Item getAuction(Long itemId) { return em.find(Item.class, itemId); } public boolean sellerHasEnoughMoney(User seller) { boolean sellerCanAffordIt = (Boolean) em.createQuery(”select…”).getSingleResult(); return sellerCanAffordIt; } @Remove public void endAuction(Item item, User buyer) { // Set winning bid // Charge seller // Notify seller and winner item.setBuyer(…); em.flush(); } } This bean implements the three methods of the ManageAuction interface (we don t have to show you this interface). First, it s a stateful EJB; the container creates and reserves an instance for a particular client. When a client obtains a handle to this EJB for the first time, a new instance is created and a new extended persistence context is injected by the container. The persistence context is now bound to the lifecycle of the EJB instance and is closed when the method marked as @Remove returns. Notice how you can read the methods of the EJB like a story of your conversation, one step after another. You can annotate several methods with @Remove; for example, you can add a cancel() method to undo all conversation steps. This is a strong and convenient programming model for conversations, all built-in for free with EJB 3.0. Next is the problem of automatic flushing. All methods of the ManageAuction- Bean require a transaction; you declare this on the class level. The sellerHasEnoughMoney() method, step two in the conversation, flushes the persistence context before executing the query and again when the transaction of that method returns. To prevent that, you declare that the injected persistence context should be in FlushMode.MANUAL, a Hibernate extension. It s now your responsibility to flush the persistence context whenever you want to write the queued SQL DML to the database you do this only once at the end of the conversation. Your transaction assembly is now decoupled from the flush behavior of the persistence engine.
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

CHAPTER 11 Implementing conversations (Web design course) 11.4.2 Extended persistence contexts

Tuesday, November 27th, 2007

CHAPTER 11 Implementing conversations 11.4.2 Extended persistence contexts with EJBs In the previous section, you only worked with persistence contexts that were scoped to the JTA system transaction. The container injected an EntityManager automatically, and it transparently handled the persistence context flushing and closing. If you want to implement a conversation with EJBs and an extended persistence context, you have two choices: You can write a stateful session bean as a conversation controller. The persistence context can be automatically scoped to the lifecycle of the stateful bean, which is a convenient approach. The persistence context is closed automatically when the stateful EJB is removed. You can create an EntityManager yourself with the EntityManagerFactory. The persistence context of this EntityManager is application-managed you must flush and close it manually. You also have to use the joinTransaction() operation to notify the EntityManager if you call it inside JTA transaction boundaries. You ll almost always prefer the first strategy with stateful session beans. You implement the same conversation as before in Java SE: three steps that must be completed as one atomic unit of work: Retrieval of an auction item for display and modification, a liquidity check of the seller account, and finally the closing of the auction. You again have to decide how you want to disable automatic flushing of the extended persistence context during the conversation, to preserve atomicity. You can choose between the Hibernate vendor extension with FlushMode.MANUAL and the official approach with nontransactional operations. Disabling flushing with a Hibernate extension Let s first write a stateful EJB, the conversation controller, with the easier Hibernate extension: @Stateful @TransactionAttribute(TransactionAttributeType.REQUIRED) public class ManageAuctionBean implements ManageAuction { @PersistenceContext( type = PersistenceContextType.EXTENDED, properties = @PersistenceProperty( name=”org.hibernate.flushMode”, value=”MANUAL”) )
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Conversations with EJB (Web site design) 3.0 If a container-provided

Monday, November 26th, 2007

Conversations with EJB 3.0 If a container-provided (through injection or obtained through lookup) EntityManager is invoked for the first time, a persistence context begins. If no system transaction is active at that time, the persistence context is short and serves only the single method call. Any SQL triggered by any such method call executes on a database connection in autocommit mode. All entity instances that are (possibly) retrieved in that EntityManager call become detached immediately. If a stateless component (such as ItemDAO) is invoked, and the caller has an active transaction and the transaction is propagated into the called component (because ItemDAO methods require or support transactions), any persistence context bound to the JTA transaction is propagated with the transaction. If a stateless component (such as ItemDAO) is invoked, and the caller doesn t have an active transaction (for example, ManageAuction.endAuction() doesn t start a transaction), or the transaction isn t propagated into the called component (because ItemDAO methods don t require or support a transaction), a new persistence context is created when the EntityManager is called inside the stateless component. In other words, no propagation of a persistence context occurs if no transaction is propagated. These rules look complex if you read only the formal definition; however, in practice they translate into a natural behavior. The persistence context is automatically scoped and bound to the JTA system transaction, if there is one you only have to learn the rules for transaction propagation to know how the persistence context is propagated. If there is no JTA system transaction, the persistence context serves a single EntityManager call. You used TransactionAttributeType.REQUIRED in almost all the examples so far. This is the most common attribute applied in transaction assemblies; after all, EJB is a programming model for transactional processing. Only once did we show TransactionAttributeType.NOT_SUPPORTED, when we discussed nontransactional data access with a Hibernate Session in chapter 10 section 10.3.3, Optional transactions with JTA . Also remember that you need nontransactional data access in JPA, to disable automatic flushing of the persistence context in a long conversation the problem of the missing FlushMode.MANUAL again. We now take a closer look at the transaction attribute types and how you can implement a conversation with EJBs and manual flushing of an extended persistence context.
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.

Web site design and hosting - CHAPTER 11 Implementing conversations // Notify seller and

Monday, November 26th, 2007

CHAPTER 11 Implementing conversations // Notify seller and winner … return item; } … } The EJB container injects the desired components based on your declaration of fields with @EJB the interface names ItemDAO and PaymentDAO are enough information for the container to find the required components. Let s focus on the transaction and persistence-context propagation rules that apply to this component assembly. Propagation rules First, a system transaction is required and is started if a client calls ManageAuction.endAuction(). The transaction is therefore committed by the container when this method returns. This is the scope of the system transaction. Any other stateless component methods that are called and that either require or support transactions (like the DAO methods) inherit the same transaction context. If you use an EntityManager in any of these stateless components, the persistence context you re working with is automatically the same, scoped to the system transaction. Note that this isn t the case if you use JPA in a Java SE application: The EntityManager instance defines the scope of the persistence context (we elaborated on this earlier). When ItemDAO and PaymentDAO, both stateless components, are invoked inside the system transaction, both inherit the persistence context scoped to the transaction. The container injects an EntityManager instance into itemDAO and paymentDAO with the current persistence context behind the scenes. (Internally, if a client obtains a ManageAuction controller, the container grabs an idle ManageAuctionBean instance from its pool of stateless beans, injects an idle stateless ItemDAOBean and PaymentDAOBean, sets the persistence context on all the components that need it, and returns the ManageAuction bean handle to the client for invocation. This is of course somewhat simplified.) These are the formalized rules for persistence-context scoping and propagation: If a container-provided (through injection or obtained through lookup) EntityManager is invoked for the first time, a persistence context begins. By default, it s transaction-scoped and closes when the system transaction is committed or rolled back. It s automatically flushed when the transaction is committed.
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Sri lanka web server - Conversations with EJB 3.0 private EntityManager em; @TransactionAttribute(TransactionAttributeType.REQUIRED)

Saturday, November 24th, 2007

Conversations with EJB 3.0 private EntityManager em; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Bid getMaxBid(Long itemId) { return (Bid) em.createQuery(”…”).getSingleResult(); } … } The EJB container injects an EntityManager when a client of this bean calls get- MaxBid(). The persistence context for that EntityManager is the current persistence context (more about this soon). If no transaction is in progress when getMaxBid() is called, a new transaction is started and committed when getMax- Bid() returns. NOTE Many developers didn t use EJB session beans for DAO classes with EJB 2.1. In EJB 3.0, all components are plain Java objects and there is no reason you shouldn t get the container s services with a few simple annotations (or an XML deployment descriptor, if you don t like annotations). Wiring EJB components Now that ItemDAO is an EJB component (don t forget to also refactor PaymentDAO if you follow the examples from earlier conversation implementations with Hibernate), you can wire it into the also refactored ManageAuction component through dependency injection and wrap the whole operation in a single transaction: @Stateless public class ManageAuctionBean implements ManageAuction { @EJB ItemDAO itemDAO; @EJB PaymentDAO paymentDAO; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Item endAuction(Item item) { // Merge item itemDAO.makePersistent(item); // Set winning bid Bid winningBid = itemDAO.getMaxBid( item.getId() ); item.setWinningBid(winningBid); item.setBuyer( winningBid.getBidder() ); // Charge seller Payment payment = new Payment(item); paymentDAO.makePersistent(payment);
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

CHAPTER 11 Implementing conversations the official solution, you (Vps web hosting)

Friday, November 23rd, 2007

CHAPTER 11 Implementing conversations the official solution, you can t get repeatable-read database transaction isolation and at the same time disable automatic flushing. The persistence-context cache can provide repeatable read only for entity queries, not for scalar queries. We highly recommend that you consider Hibernate s FlushMode.MANUAL setting if you implement conversations with JPA. We also expect that this problem will be fixed in a future release of the specification; (almost) all JPA vendors already include a proprietary flush mode setting with the same effect as org.hibernate.FlushMode.MANUAL. You now know how to write JPA conversations with detached entity instances and extended persistence contexts. We laid the foundation in the previous sections for the next step: the implementation of conversations with JPA and EJBs. If you now have the impression that JPA is more cumbersome than Hibernate, we think you may be surprised at how easy conversations are to implement once you introduce EJBs. 11.4 Conversations with EJB 3.0 We have to go through our list again: persistence context propagation, handling of detached objects, and extended persistence contexts that span the whole conversation. This time, you ll add EJBs to the mix. We don t have much more to say about detached entity instances and how you can merge modifications between persistence contexts in a conversation the concept and the API to use are exactly the same in Java SE and with EJBs. On the other hand, persistence-context propagation and extended persistence- context management with JPA become much easier when you introduce EJBs and then rely on the standardized context propagation rules and the integration of JPA with the EJB 3.0 programming model. Let s first focus on the persistence-context propagation in EJB invocations. 11.4.1 Context propagation with EJBs JPA and EJB 3.0 define how the persistence context is handled in an application and the rules that apply if several classes (or EJB components) use an EntityManager. The most common case in an application with EJBs is a container-managed and injected EntityManager. You turn the ItemDAO class into a managed stateless EJB component with an annotation, and rewrite the code to use EntityManager: @Stateless public class ItemDAOBean implements ItemDAO { @PersistenceContext
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Photography web hosting - Conversations with JPA Don t forget that em.flush() must

Thursday, November 22nd, 2007

Conversations with JPA Don t forget that em.flush() must be called manually, in the last transaction in the third event otherwise no modifications are made persistent: public static class ManageAuctionExtended { … public Item endAuction(Item item) { EntityTransaction tx = em.getTransaction(); tx.begin(); // Merge item … // Set winning bid … em.flush(); // Commit the conversation tx.commit(); return mergedItem; } } The official architectural solution relies on nontransactional behavior. Instead of a simple FlushMode setting, you need to code your data-access operations without transaction boundaries. One of the reasons given by expert group members about the missing FlushMode is that a transaction commit should make all modifications permanent. So, you can only disable flushing for the second step in the conversation by removing transaction demarcation: public class ManageAuction { … public boolean sellerHasEnoughMoney(User seller) { boolean sellerCanAffordIt = (Boolean) em.createQuery(”select …”).getSingleResult(); return sellerCanAffordIt; } … } This code doesn t trigger a flush of the persistence context, because the Entity- Manager is used outside of any transaction boundaries. The EntityManager that executes this query is now working in autocommit mode, with all the interesting consequences we covered earlier in section 10.3, Nontransactional data access. Even worse, you lose the ability to have repeatable reads: If the same query is executed twice, the two queries each execute on their own database connection in autocommit mode. They can return different results, so the database transaction isolation levels repeatable read and serializable have no effect. In other words, with
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

CHAPTER 11 Implementing conversations boolean sellerCanAffordIt = (Boolean)

Wednesday, November 21st, 2007

CHAPTER 11 Implementing conversations boolean sellerCanAffordIt = (Boolean) em.createQuery(”select…”).getSingleResult(); tx.commit(); return sellerCanAffordIt; } … } The code snippet even includes two calls that trigger the flushing of the Entity- Manager s persistence context. First, FlushMode.AUTO means that the execution of the query triggers a flush. Second, the transaction commit triggers another flush. This obviously isn t what you want you want to make the whole conversation atomic and prevent any flushing before the last event is completed. Hibernate offers org.hibernate.FlushMode.MANUAL, which decouples transaction demarcation from the synchronization. Unfortunately, due to disagreements among the members of the JSR-220 expert group, javax.persistence.FlushMode only offers AUTO and COMMIT. Before we show you the official solution, here is how you can get FlushMode.MANUAL by falling back to a Hibernate API: // Prepare Hibernate-specific EntityManager parameters Map params = new HashMap(); params.put(”org.hibernate.flushMode,” “MANUAL”); // Begin persistence context with custom parameters EntityManager em = emf.createEntityManager(params); // Alternative: Fall back and disable automatic flushing ((org.hibernate.Session)em.getDelegate()) .setFlushMode(org.hibernate.FlushMode.MANUAL); // Begin conversation ManageAuction controller = new ManageAuction(em); // First event Item item = controller.getAuction( 1234l ); // Item is displayed on screen and modified… item.setDescription(”[SOLD] An item for sale”); // Second event if ( !controller.sellerHasEnoughMoney(seller) ) throw new RuntimeException(”Seller can’t afford it!”); // Third event controller.endAuction(item); // End persistence context and conversation em.close();
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.