Best Practices for EJB: A jndi optimization of industrial strength

Source: Internet
Author: User

In this tip, we'll look at some of the most common JNDI optimizations. In particular, we'll show you how to combine the cache and generic helper classes to create a factory-style solution for JNDI overhead.

Reducing context instances

Listing 1 shows a typical EJB code that requires multiple JNDI lookups. Take a moment to study the code, and then we'll optimize it to get better performance.

Listing 1. A typical EJB lookup

public boolean buyitems (Paymentinfo paymentinfo, String storename,
List items) {
//Load up initial C Ontext
Context ctx = new InitialContext ();
//Look up a Bean´s home interface
Object obj = Ctx.looku P ("Java:comp/env/ejb/purchasehome");
Purchasehome purchasehome =
(purchasehome) portableremoteobject.narrow (obj, purchasehome.class);
Purchase Purchase = purchasehome.create (Paymentinfo);
//Work on the Bean
for (Iterator i = Items.iterator (); I.hasnext ();) {
Purchase.additem ((Item) I.next ());
}
//Look up another bean
Object obj = ctx.lookup ("Java:comp/env/ejb/inventoryhome");
Inventoryhome inventoryhome =
(inventoryhome) portableremoteobject.narrow (obj, inventoryhome.class);
Inventory Inventory = Inventoryhome.findbystorename (storename);
//Work on the Bean
for (Iterator i = Items.iterator (); I.hasnext ();)
Inventory.markassold ((Item) I.next ());
}
//Do some the other stuff
}

Although this example is somewhat deliberate, it does reveal some of the most obvious problems with JNDI use. For starters, you should ask yourself if it is necessary to create a new InitialContext object. It's possible that this context has been loaded elsewhere in the application code, and we've created a new one here. The cache InitialContext instance immediately prompts performance improvements, as shown in Listing 2:

Listing 2. Cache InitialContext Instance

public static Context getInitialContext() {
if (initialContext == null) {
initialContext = new InitialContext();
}
return initialContext;
}

By using the helper class for Getinitialcontext () instead of instantiating a new initialcontext for each operation, we reduce the number of contexts that are spread across the application to one.

What happens to threading?

If you're worried about the line Chenghua of the solution presented here, that's not necessary. It is absolutely possible for two threads to Getinitialcontext () at the same time (thereby creating two contexts at a time), but this type of error occurs only the first time the method is called. Because the problem occurs at most once, synchronization is unnecessary, and in fact, synchronization introduces more complexity than it solves.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.