Pages

Thursday, May 24, 2007

The new Java EE5 Persistence provides some really cool functionality. It was designed with backwards compatibility in mind. This has resulted in some interesting issues. For example if you perform a Query which returns more than one object, the objects are returned as a List. The List is a very generic Object List. Generally we know that if we perform a query that it should return specific types back to us. For example we expect it to return a collection of the entity itself. If we accept the default which is to return a List, the compiler will report an unchecked warning. If you use the -Xlint:unchecked option, it will point out that we are getting a generic Object List and it can not verify the type of the Object.

I am taking the time to create some cliff notes from a wonderful presentation at JavaOne TS-4593 "Guidelines, Tips, and Tricks for Using Java EE5 from Java Blueprints" (Inderjeet Singh, Marina Vatkina, et.al.) and an article in JavaOne Today by Rick Palkovic. When the technical sessions are released to the general public, it would be a good read for any Java EE5 developer.

The problem has to do with thread safety on web applications. If you are using servlets, or a technology based on servlets like JSF, you must consider thread safety for persistence. A common injection is to use the container managed persistence context. This usually takes the following form

@PersistenceContext(unitName="myContext")private EntityManager em;

The problem is this is NOT thread safe if used in a servlet. A servlet processes many requests and the container handles many requests. Even if you use an injection to get a UserTransaction, it lulls the programmer into a false sense of safety.

In their talk, they mentioned four methods to ensure thread safety on servlet based technologies

Use a request-scoped bean Java Server Faces. The life of the bean is a single transaction.

Another alternative is to use the @PersistenceUnit annotation to get an EntityManagerFactory. The EntityManagerFactory is a thread-safe object. From the factory we create the EntityManager. This is also thread-safe since it creates a new EntityManager per call.

Wednesday, May 23, 2007

There are some really cool new woodstock components available in the new VWP and from the java.net site. Woodstock is the original code name for the components which are used in VWP. These new components provide a new level of reusable web components which were not available before. One of the new components which is used extensively in project glassfish on the administrative console is the common tasks component.

This component provides a number of visual queues to what a user can do with the component. The component looks like this currently in Netbeans 6.0 M8. The component is not on the visual palette in 6.0 M9 Preview. The component does not necessarily look appealing in the current form on the palette, but renders perfectly.

As you can see from the images this is a really cool idea. The info button "i" allows you to provide additional information about what the task does. You can add a URL for additional information and a separate title for the info that is displayed. Here is an example from project glassfish where you can see another example of its use.

This shows you some of the capabilities.

The trick is how do you use it?

On project glassfish, when you click on the task it directs you to the appropriate category you selected. The details are scarce. So let me give you one solution. Use the actionExpression tag to set an action for the component. Here is mine for the example I showed above

<webuijsf:commontaskactionExpression="#{CommonTasksExample.masterDetailTaskAction}" binding="#{CommonTasksExample.commonTask1}"id="commonTask1" infoLinkUrl="/faces/Page3.jsp"infoText="This example shows how to use a DropDown component to set values in a table" infoTitle="Master-Detail Example"style="height: 48px; width: 250px" target="_self" text="Master Detail Example"/>

The actionExpression is bound to a method which uses a navigation handler to navigate to the appropriate page.

This allows me to navigate as required. I hope this example will inspire you to try out the new components. They will add a level of professionalism to your web pages and make your users have a better experience on your site. Implemented fully, it may even avoid a call to the help desk, or you to figure out what a task is supposed to do.