Monday, November 12, 2007

At Oracle Open World in San Francisco

Oracle Open World started yesterday. Today I've a session on Java EE with my coworkers Dan Hynes and Dana Singleterry. It is at 3:15pm at Hilton San Francisco. We will present how to build Java EE 5 applications and deploy and manage with Oracle Application Server. If you have a copy of EJB 3 In Action then you can bring it to the session and I can sign it if you want.

I will be hanging around the Oracle Enterprise Manager (Middleware and Apps management booth J04, J06, etc) booths in the demo ground where we are demoing our new product Oracle Application Diagnostics for Java (Oracle AD4J) that help diagnose problems in production environment. Please stop by and learn how Oracle AD4J makes your life simple in diagnosing problems in your production J2EE applications. You can also see some cool demos on SOA management, Application Server Config and Provisioning, SLA management and many more.

Friday, November 02, 2007

Spring and EJB 3: All-Star Team or Neighbors with Good Fences

I presented a Spring / EJB 3 Comparison/Integration talk at the IndicThreads Java conference in Poona. It was very well received. Here is the link to the presentation for your reading pleasure.

It is primarily based on the contents on our book EJB 3 In Action and my research with my coauthor Reza Rehman on this topic.

All EJB 3 books in the market ignore existence Spring considering it as a competitor. Even the classic Java Persistence on Hibernate says nothing about Spring framework. However EJB 3 In Action takes a pragmatic approach and tries to recommend whenever Spring makes sense. Although Spring and EJB 3 are viewed as competitors it can be used complementary technologies and a whole chapter of EJB 3 In Action is dedicated to this integration story. So why wait .. buy a copy for yourself :)

Monday, October 29, 2007

Using Dependency Injection from Web Container in OC4J 10.1.3.x

OC4J 10.1.3.1 and later supports dependency injection from web container. I've seen this question from many customers via email or in OTN forum that using dependency injection from web container generates NPE. If you are running into such problems, make sure that you have specified version as "2.5" in your deployment descriptor (web.xml) as follows:


<?xml version = '1.0'?>?
<?web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">?
...

Monday, September 03, 2007

IndicThread Java Conference















I will be speaking at the IndicThread Java Conference in Pune, India (Oct 26-27). Two of my talks on (Spring / EJB 3 Integration) and SOA management has been accepted. If you are in the Indian subcontinent then it's the only independent Java conference and I highly recommend this conference.

Tuesday, July 24, 2007

More praises for EJB 3 In Action

EJB 3 In Action gets Five STARs rating from Cicero Zandoná of JavaLobby.
Here is some excerpt from his review:

If you want an EJB 3.0 book to read from cover-to-cover this is the one. You will learn how to create an EJB application, the concepts related to them and receive a lot of useful advice.
I really can't give this book less then five "stars".

Here is an interesting comment from the book:

One of the authors, Debu Panda is a Lead Product Manager of the Oracle Application Server development team, and I was afraid this could lead to a biased book. Fortunately this doesn't happen at all, he is completely impartial along the book and always warns the reader about vendor-dependent features.

I'm really happy that everyone seems to like the Sadhu story that I started the book with. Here is what Cicero puts in his review:

In an unusual beginning for a technical book, the authors start talking about ladybugs, elephants and cows (one of the authors name is Panda by the way). But I can't think in a more gentle and creative way to illustrate the specification evolution than the analogy they made (you can read the tale in Meera's review).

Read the detailed review at JavaLobby

Thursday, July 05, 2007

Automatically Executing a Client Application Archive


Many applications have a requirement that they want to automatically execute an application client archive(CAR) packaged as a part of enterprise application (EAR) when server starts up or an application is restarted. This is a Java EE 5 standard compliant alternative to Startup class about which I blogged about 2 and half years back!

In this article, I will outline steps to do this in OC4J.



  • Develop and compile your application client class. This class must contain a main method that will be called by your server during startup.


  • Create a META-INF/Manifest.mf file for the application-client, for example
    Manifest-Version: 1.0
    Main-Class: OracleASStartup


  • Create a deployment descriptor for the application client (application-client.xml) as follows:
    <application-client>
    <display-name>OracleASStartup</display-name>
    </application-client>


  • Create the application client archive (CAR) file for your startup class with the above Manifest and deployment descriptor.


  • Create an EAR file for the application client module with the an OC4J-specific deployment descriptor orion-application.xml as follows:
    <orion-application>
    <client-module path="startup-client.jar" start="true" user="anonymous" >
    </orion-application >


  • Build your EAR and deploy your application with auto start set to "true" as follows in the server.xml:<application name="startup"
    path="../applications\startup.ear" parent="default" start="true" >


Whenever your application is deployed or if you restart your server, the application client will be executed automatically!

Hope this helps!

Wednesday, June 27, 2007

JDJ Article: Java EE 5 and Spring

JDJ just published second part of my article on Java EE 5/ EJB 3 integration with Spring. You can read the article in the June 2007 issue. Here is link to the article for your reading pleasure.

Thursday, June 21, 2007

EJB 3 In Action is #1 spot of hot new released Java Books


EJB 3 In Action got the #1 spot of best selling new and future release Java books. Note that Amazon keeps on changing the rank every hour


Here is the screen shot that shows that it's #1

Wednesday, June 06, 2007

IndicThreads Java Coding Contest and EJB 3 In Action

India's premier Java Developer portal IndicThreads is holding a Java coding contest. This week winner gets a copy of my book EJB 3 In Action!

Also EJB 3 In Action has been reprinted in India by DreamTech press and is available now at most computer book stores!

Monday, June 04, 2007

Injecting POJOs and using resource injection in POJOs

Java EE 5 greatly simplifies using resources and EJBs by using dependency injection. However this support limits only to managed classes such as EJBs, Interceptors, Servlets, etc. Many applications use helper classes those are regular POJOs. However Java EE 5 / EJB 3 does not require support of dependency injection with regular POJOs or injecting POJOs into managed classes.


In this article, I will discuss about Oracle’s extension of managed POJOs and describes how to use dependency injection such as @Resource or @PersistenceContext with regular POJOs (in OC4J 11 Preview).


This example has a session bean named EmployeeFacade that injects a regular POJO named EmployeeHelper. The EmployeeHelper POJO uses @PersistenceContext to inject an instance of EntityManager.


Using Annotations in regular POJOs


To use dependency injection with regular POJO it does not require any Oracle specific extension. The only requirement is that it must be injected to another managed class by using @oracle.j2ee.annotation.InjectedObject or using Oracle specific descriptor e.g. orion-ejb-jar.xml.



Following is an example of regular POJO that uses dependency injection.


public class EmployeeHelper {
@PersistenceContext
private EntityManager em;
private Employee emp;
public Employee findById(int empNo) {
return ((Employee) em.find(Employee.class, empNo));
}
public Employee saveEmployee(String eName, double sal) {
..
return emp;
}
}

Injecting a POJO
OC4J 11 allows injecting POJOs to a managed class or to another POJO using @oracle.j2ee.annotation.InjectedObject. To inject the EmployeeHelper class into the EmployeeFacade EJB we need the following code:


@Stateless
public class EmployeeFacadeBean implements EmployeeFacade {
@InjectedObject
private EmployeeHelper eh;

public Employee findEmployeeByEmpNo(int empNo) {
return eh.findById(empNo);
}
public int addEmployee(String eName, double sal) {
return eh.saveEmployee(eName,sal).getEmpNo();
}
}


Conclusion

OC4J 11 allows injecting one POJO to another POJO. You can try this with OC4J 11 Preview and let us know what other features you want to see.

Another Five STAR rating for EJB 3 In Action

Another reviewer of JavaLobby (Michael Smolyak ) gave five star rating to EJB 3 In Action. Here is what he says:

"The book is enjoyable, extremely well organized and covers a wide range of EJB subjects. I would recommend EJB 3 in Action to both old hands and newly-initiated in the EJB craft. My only complaint is the size of the book; but I think the logical, unhurried and deliberate manner, with which the authors approached the non-trivial subjects addressed in this book, making them accessible to EJB novices, and the breadth of material covered in the volume do make up for its weightiness."

Read the detailed review at JavaLobby

Monday, May 21, 2007

Thursday, May 10, 2007

EJB 3 Action @JavaOne bookstore!

Few guys told me that their local B & N or Borders do not carry EJB 3 In Action as yet. The Bookstore at JavaOne is selling the EJB 3 In Action. It is offering 20% discount so it's a steal! Also the publisher is offering 35% discount on the book for the JavaOne attendees if ordered from it's website. So get a flyer with a coupon code. Some of these are in the bookstore or around the Oracle's EJB 3/ OC4J booth in the Exhibition Hall!

Wednesday, May 09, 2007

Using Dependency Injection to invoke EJB deployed in a remote container

Many applications require invocation of EJBs deployed in a remote container. EJB 3 and Java EE 5 support injection of remote EJB references only in the application client container. Let us say you want to invoke an EJB based on a remote container from your web client (JSF backing bean) or an EJB then you have to resort to the JNDI lookup. Unfortunately injection of remote references have been left as an extension to application server vendors.

In this blog I will demonstrate how OC4J 11g preview supports injection of EJB references that is deployed in a remote container.

Let us assume that we have two Session EJBs HelloWorld and Namaste deployed in two different OC4J instances. The HelloWorld EJB uses dependency injection to get a reference to Namaste EJB located in a remote container.

Stateless Session Bean example using EJB 3.0


We will deploy Namaste EJB into second OC4J instance that will be remotely invoked by the HelloWorld EJB. The following is the coded for the Namaste EJB.

@StatelessDeployment(location="Namaste")
@Stateless
public class NamasteBean implements Namaste 
{
public void sayHello(String name)
{
System.out.println("Namaste "+name +" from Remote EJB");
}

Note that the location parameter of @oracle.j2ee.annotation.ejb.StatelessDeployment is used to configure global JNDI name for the Namaste EJB.

Injection Code to Invoke the Remote EJB

OC4J allows injecting remote EJB references (deployed on remotre server) by using a proprietary annotation @oracle.j2ee.annotation.ejb.EJBRefMapping, as shown in the following code. Note that HelloWorld EJB is deployed in OC4J instance 1.

@Stateless

public class HelloWorldBean implements HelloWorld {

@EJBRefMapping(remoteServerRef="true",location="Namaste",

jndiPropertiesFile="META-INF/jndi.properties")

@EJB

private Namaste namaste;

public void sayHello(String name) {

System.out.println("Hello " + name+ " from your first EJB 3.0 component ... ");

System.out.println("Using Injection to invoke method of Namaste Bean! See console of remote container ");

namaste.sayNamaste(name);

}

}

The location element specifies the global JNDI for the remote interface of the EJB and jndiProperties file specifies the JNDI properties such as provider URL, principal, credential, etc to look up the remote EJB as follows:



java.naming.factory.initial=oracle.j2ee.rmi.RMIInitialContextFactory
java.naming.provider.url=ormi://localhost:23792/remoteDI
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome


You can use @EJBRefMapping annotation in any manage classes in the container such as Servlet, listeners or managed POJOs. If you are not comfortable using Oracle's extension, you can use the equivalent in oracle-ejb-jar.xml.

Client Code

The client code runs in Oracle’s application client container and uses dependency injection to invoke methods on HelloWorld EJB as follows:.

public class HelloWorldClient {

@EJB

private static HelloWorld helloWorld;

helloWorld.sayHello(name);

}

Conclusion

Dependency Injection of remote EJB references in not standardized. You can use Oracle's proprietary extension to injection remote EJB references.

Should EJB 3 contain support for EJB 2? Yes, ALWAYS !!

I read the interesting blog by Andreas where he makes argument that EJB 2 should have been optional in EJB 3!


In my opinion, “No, never”. There have been countless EJB 2 applications in production and I still see many developers actively developing/deploying EJB 2 applications because many application servers such as IBM Websphere still do not have support for EJB 3 in their production servers. Many of EJB 2 applications will migrate to use EJB 3 but many will probably hang around for years. Support for EJB 2 and EJB 3 together allows applications to interoperate between applications using two different versions of applications deployed in the same container. This also enables customers to do a phased migration of applications. Having said that EJB 3 de-supported EJB 1.1 and I still want to see support for EJB 2.1 in EJB 3.1. I think it makes sense for a standard to support at least two earlier releases. I’m okay if EJB 4 (probably it will at least 2-3 years from now) de-supports EJB 2.1. If Sun decides to EJB 2.1 in EJB 3.1 then most customers will probably be mad and decide to dump EJB :) Anyway vendors like Oracle will keep supporting EJB 2.1 with EJB 3.1 to keep their customers up and running!

Tuesday, May 08, 2007

Oracle releases Java EE 5 compatible container!

Oracle released Java EE 5 compliant Oracle Application Server 11 technology preview. This preview has several extensions to the Java EE 5 that includes the POJO injection, injection remote EJB references, embeddable Spring container and many more. I'm at JavaOne currently and I will blog about these features in coming days!

Thursday, May 03, 2007

EJB 3 In Action: Book Signing

I will do a book-signing at JavaOne next week. You can drop by at the DigitalGuru Bookstores at Moscone between 4:00-4:30pm on Tuesday(8th May) with your copy of the EJB 3 In Action or buy one at the bookstore and get it autographed!

See you at San Francisco and have a great JavaOne!

Thursday, April 26, 2007

JDJ Article: Spring and Java EE 5

JDJ published part-1 of my article Spring and Java: Simplicity and Power Combined in their latest issue. In the first part of article I discuss the Simplicity brought by Java EE 5 and integration between Spring and JPA. Stay tuned for the second part of the article in which you will discover how the Spring Framework integrates with other components such as an EJB, Java Message Service (JMS), and transaction manager.

Monday, April 23, 2007

Amazon finally started shipping!

I got few queries from readers about Amazon.com giving a shipping date in mid-May for my book EJB 3 In Action. I had put a test order and Amazon.com notified I that they shipped the order. Both Amazon.com and Amazon UK have started shipping. Amazon Canada still shows pre-order. I think it is now available in bookstores such as Barnes and Noble and Borders by now!

Thursday, April 12, 2007

Google Alert Works sometime: Great Review from JavaLobby!

Google's alert works sometimes! I've an alert set for "EJB 3 In Action" and I mostly ignore because mostly I get alerts for web pages that were created couple of years back. But it was an early morning suprise. We have a great review from DZone / JavaLobby. We have got five smileys (the highest) in all respects: relevance, readability and overall. It was good to know that the reviewer (Meera) had reviewed two other EJB 3 books (O'Reilly and Apress) earlier and she gives us highest marks!

Here is her concluding remark:

"When you start reading a technical book within the first few pages itself you really know whether you like the book or not. This book was very interesting to me and kept the interest all the way until the end. I found this book to be well worth the time I spent reading it in spite of me being quite familiar with EJB3 as well as JPA. If you are planning on using EJB3 or even migrating your existing EJB 2 applications, I would highly recommend this book.

Last but not the least, Part 4 and Part 5 of this book, and the performance considerations and best practices for EJB's at the end of a few chapters in itself is worth the price of the book."


Take a look at the complete review!

Wednesday, April 11, 2007

My Copies of EJB 3 In Action Arrived


My copies of EJB 3 In Action arrived yesterday. I've been waiting and waiting because the binders shipped those on 4th. And it was misdelivered to at my neighbor's door by UPS a couple of days ago!
Anyway here is a picture of the book after I opened!


Tuesday, April 10, 2007

Article:Packaging EJB 3 Applications

Developer.com published an article Packaging EJB 3 Applications that is an excerpt from the Chapter 11 of our book EJB 3 In Action!

This article provides:
1. Overview of Java EE packages such as EAR, WAR
2. An intro to Classloading and classloading in Java EE
3. Packaging Session beans and MDBs
4. Discussion on annotations and overriding with descriptors

Friday, April 06, 2007

EJB 3 In Action Now available!

Print copies of EJB 3 In Action have left the printers and are now available. It will take about 10 days-to 2 weeks for Amazon and book stores to stock up and start shipping. The fastest way to get your print copy is to order at the publisher's web site http://www.manning.com/panda/!

Friday, March 23, 2007

Oracle Acquires Tangosol

Oracle announced that it will acquire Tangosol. Congrats Cameron and gang and welcome to Oracle!

Wednesday, March 21, 2007

Using @EJB annotation across different ejb-jar module

I got some questions from customers on how to use @EJB annotation to inject instance of a session packaged in another EJB-JAR. In this blog entry I will outline how to do that.

Let us assume that you have two stateless beans SessionEJB1 and SessionEJB2 and those are packaged in two different EJB modules ejb1-ejb.jar and ejb2-ejb.jar respectively. You want to use inject SessionEJB2 (in ejb2-ejb.jar) from SessionEJB1 (packaged in ejb1-ejb.jar) then you can just do the following:

@Stateless
public class SessionEJB1Bean implements SessionEJB1, SessionEJB1Local {
@EJB
testejb2.SessionEJB2 ejb2;

This works great in OC4J 10.1.3.1.

Optionally you may do as follows, following the ejb-link convention that we used in EJB 2.

@EJB(beanName="ejb2-ejb.jar#SessionEJB2Bean",
beanInterface=testejb2.SessionEJB2.class)
testejb2.SessionEJB2 ejb2;


I've seen this problem when I deploy from JDeveloper. If you are using JDeveloper 10.1.3.1 and getting NameNotFoundException then make sure that your dependent ejb-jar module does package duplicate classes. By default when you declare a dependency on another project the classes are automatically packaged in the dependent jar e.g. ejb1-ejb.jar in our case. You have to verify it using the deployment profile for your EJB module. Make sure that you have unchecked the classes by selecting filters.

Hope this works for you!

Tuesday, March 20, 2007

EJB 3 In Action goes to press: eBook now available!

EJB 3 In Action went to press late last week. The e-Book has been published at the Manning web site . Print copies of the book will be available at Manning web site for order and hit the shelves at the brick and mortar stores such as Barnes and Noble and Borders in another 2-3 weeks.

Two sample chapters (chapter 1 and 11) are available free at the book site. You can take a look at the preface, table of contents and index.

You can order the book from Amazon.com at http://www.amazon.com/Ejb-3-Action-Debu-Panda/dp/1933988347

The global sites at Amazon screwed up the listing and show two books. It was due to the change in ISBN. Note the correct ISBN is: 1-933988-34-7 and book is of 700 pages.

Here are links to the correct book at Amazon sites for few countries

Canada : http://www.amazon.ca/Ejb-3-Action-Debu-Panda/dp/1933988347

France : http://www.amazon.fr/Ejb-3-Action-Debu-Panda/dp/1933988347

Germany: http://www.amazon.de/Ejb-3-Action-Debu-Panda/dp/1933988347

Japan: http://www.amazon.jp/Ejb-3-Action-Debu-Panda/dp/1933988347

UK : http://www.amazon.co.uk/Ejb-3-Action-Debu-Panda/dp/1933988347

With the release of the book I’m relieved and looking forward to reviews by readers. We have a great draft review from JavaRanch and hope many good reviews will pour in!

Tuesday, March 13, 2007

EJB 3 In Action: Book Promotion

JavaRanch has arranged a promotional event for our book EJB 3 In Action (http://www.manning.com/panda/) . You can ask some questions about our book or EJB 3 technology in general in their EJB and Technologies forum and get a chance to win a copy of the book.


The book goes to press today. eBook will be available on Mar 15 and print copies will be available Mar 30. You can buy eBoook or print book from Manning web site.

You can also order copies from Amazon!

Tuesday, March 06, 2007

EclipseLink FAQ

Oracle proposed the EclipseLink Persistence framework based on its TopLink product. I forgot to link the FAQ for this. Here it is !

Oracle open sourced complete TopLink product as an Eclipse project

Oracle donates the complete TopLink product to Eclipse.

You may remember that Oracle had earlier contributed TopLink Essentials (a part of TopLink) to Sun’s Glassfish project that became the reference implementation for the EJB 3 JPA spec. However today's announcement is for proposing new Java persistence project based on TopLink product that includes support for ORM, Object-XML Mapping, JAXB, SDO, etc.

See the complete press release

Friday, March 02, 2007

Customizing JNDI name for Session beans

Every vendor provides its own way of registering Session bean interfaces into their global JNDI tree. If you care about portability then you should NOT depend upon the global JNDI name and use the ref-name by using ejb-ref or ejb-local-ref and lookup the EJB from environment naming context using java:comp/env/<ref-name>. However many have their own reasons not to use the standard convention and use the vendor specific JNDI name.

If you are such a soul and want to customize the global JNDI name for a session bean in OC4J you can package an orion-ejb-jar.xml and specify location attribute (for remote interface) and local-location (for local interface) as follows:

<session-deployment location="PlaceOrderBean" name="PlaceOrder"></SESSION-DEPLOYMENT>

Note that OC4J uses the ejb-name element in the ejb-jar.xml or name parameter in @Stateless or @Stateful annotations as the default JNDI name for the remote interface.

If you are using EJB 3 and annotation fanatic then you can use either @StatelessDeployment or @StatefulDeployment annotation to specify the JNDI names as follows:

@StatefulDeployment(location="PlaceOrder",
localLocation="PlaceOrderBeanLocal")

Even if I want you to marry to our platform and use the global JNDI name so that your migration becomes difficult but I will advise against using global JNDI names and recommend that you to use ejb-ref for portability reasons.

Monday, February 19, 2007

OC4J Connection Pooling and non-Oracle databases

In my last entry I blogged about using MySql database with OC4J. I provided an example of native datasource that connects to the MySql database. Native data sources do not use connection pooling capabilities of OC4J. If you want use OC4J connection pooling with third-party databases you have to use a managed data source.

Here is an example managed data source for MySql database.


<connection-pool name="test Connection Pool">
factory-class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"

user="user1"
password="user1"
url="jdbc:mysql://144.25.134.24/Test">
</CONNECTION-FACTORY>
</CONNECTION-POOL>

<managed-data-source>
connection-pool-name="test Connection Pool"
jndi-name="jdbc/mysql"
name="mysql"/>


You have to make sure that the factory-class in the connection-factory tag points to the DataSource class for your your database.



Wednesday, February 14, 2007

Using MySQL with OC4J 10.1.3.x

When you have OracleXE available for free you would probably not use MySQL, but you may have a valid requirement to use MySQL with OC4J 10.1.3.

1. Upload JDBC Driver You have to make JDBC drivers (e.g. mysql-connector-java-3.0.11-stable-bin.jar) for your database available to Oracle Application Server. You have to copy those to %ORACLE_HOME%/j2ee/home/applib directory.

2. Create a Native DataSource

You can use the ASC to create a native data source as follows:


jndi-name="jdbc/MyDS"
description="Native DataSource" data-source-class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
user="user"
password="password"
url="jdbc:mysql://localhost:3306/mydatabase">


Now you should be able to access the DataSource connecting to MySQL database with a JNDI location "jdbc/MyDS".

Wednesday, February 07, 2007

EJB 3 In Action Book Update

Here is an update for book EJB 3 In Action. We are done writing and editing and going thru final round of proof reading before getting published. The book will be published next month. The eBook will be available first week of March and print book will be available Mar 15.

You can purchase and read the final draft of the book using Manning's MEAP program or pre-order from Amazon .

If you have any questions about the book or found any technical errors please report to the Author forum

Here is Table of contents of the book:


1. What's what in EJB
2. A First taste of EJB
3. Building Business Logic with Session beans
4. Messaging and Developing Message Driven Beans
5. Learning Advanced EJB Concepts
6. Transactions and Security
7. Implementing Domain Models with EJB 3
8. Object-Relationship Mapping using EJB 3 JPA
9. Manipulating entities with EntityManager API
10. Using the Query API and the Java Persistence Query Language
11. Packaging EJB 3 Applications
12. Effectively Integrating EJB 3 across Your Application Tiers
13. Taming Wild EJBs: Performance and Scalability
14. Migrating to EJB 3
15. Exposing EJBs as Web Services
16. EJB 3 and Spring

Appendix A. RMI and JNDI
Appendix B. Reviewing relational databases
Appendix C. Annotations reference
Appendix D. Deployment descriptors reference
Appendix E. Installing and configuring the Java EE 5 SDK

Tuesday, January 30, 2007

Using Hibernate as a Pluggable EJB 3 JPA Provider

EJB 3 supports pluggable persistence contract that allows you to plug-in any persistence provider implementing JPA 1.0 with a container that supports JavaEE 5 / EJB 3 spec. It’s provides choice to users to choose their JPA provider of choice. I thought I would give a spin and check whether this plug-ability story really works and here is the result!


I used Oracle Application Server 10g 10.1.3.1 that has support for EJB 3.0 spec (not yet EJB 3 / Java EE 5 compliant) and Hibernate 3.2 that is JPA 1.0 compliant to see whether the plug ability story is real! Oracle Application Server by default uses TopLink Essentials as the JPA provider. Note that TopLink Essentials is the Reference Implementation for the JPA spec.

Loading the Hibernate Libraries in the server

In order to use Hibernate as a pluggable persistence provider, you have to make sure that Hibernate Entity Manager and related libraries are made available to the container class path.

I created a shared library in OC4J named Hibernate and uploaded the required jars. Following is the definition of the shared library:

<shared-library name="Hibernate" version="3.2">
<code-source path="hibernate-entitymanager.jar"/>
<code-source path="hibernate3.jar"/>
<code-source path="jboss-archive-browsing.jar"/>
<code-source path="dom4j-1.6.1.jar"/>
<code-source path="hibernate-annotations.jar"/>
<code-source path="javassist.jar"/>
<code-source path="commons-collections-2.1.1.jar"/>
<code-source path="ehcache-1.2.3.jar"/>
<code-source path="c3p0-0.9.1.jar"/>
<code-source path="concurrent-1.3.2.jar"/>
<code-source path="cglib-2.1.3.jar"/>
<code-source path="asm.jar"/>
<code-source path="asm-attrs.jar"/>
<code-source path="antlr-2.7.6.jar"/>
<code-source path="commons-logging-1.0.4.jar"/>
<code-source path="log4j-1.2.11.jar"/>
</shared-library>

Make sure that application imports the shared library. You can do so by either importing the library during deployment or packaging an orion-application.xml as follows:

<orion-application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-application-10_0.xsd" deployment-version="10.1.3.1.0" default-data-source="jdbc/OracleDS" component-classification="external"
schema-major-version="10" schema-minor-version="0" >
<imported-shared-libraries>
<import-shared-library name="Hibernate"/>
</imported-shared-libraries>
</orion-application>

Configuring the Persistence Unit

You need to configure the persistence unit to specify the persistence provider and other vendor specific properties in the persistence.xml. For my test application I created a simple entity class and packaged the following persistence.xml in my application that uses Hibernate 3.2 as the JPA provider.

<persistence>
<persistence-unit name="howto">
<jta-data-source>jdbc/OracleDS</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.OC4JTransactionManagerLookup"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true" />

</properties>
</persistence-unit>
</persistence>

Note that in above I set provider class to org.hibernate.ejb.HibernatePersistence that tells the container to use Hibernate 3.2 as the persistence provider instead of using TopLink Essentials as the JPA provider.

Access to Transaction Manager

The primary reason behind the pluggable persistence provider is to provide facility to use JPA with a container managed Entity Manager and hence Hibernate needs to access the JTA Transaction Manager of the container.


Hibernate 3.2 does not use the JTA TransactionSynchronizationRegistry whose sole purpose is for such integration.

Instead it provides classes for each application server that looks up the Transaction Manager.
For example,org.hibernate.transaction.OC4JtransactionManagerLookup is provided with Hibernate as a persistence property to lookup OC4J’s transaction manager as follows:

<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.OC4JTransactionManagerLookup"/>

If you want to use another application server then you have to the class for that server.


Deploy and Run Your Application

You can package your entities and then deploy to the server

Now after you deploy your application into OC4J you should be able to run your application. If you used the property hibernate.show_sql to true, you will see similar output from Hibernate in your console.

07/01/30 12:11:54 Hibernate: insert into EMP (ename, sal, EMPNO) values (?, ?, ?
)
07/01/30 12:11:55 Hibernate: select employee0_.EMPNO as EMPNO0_0_, employee0_.en
ame as ename0_0_, employee0_.sal as sal0_0_ from EMP employee0_ where employee0_

Conclusion

I did some testing and the application works as expected and hence I conclude EJB 3 delivers on the plug-ability story and Oracle Application Server 10g is Hot Pluggable!

Monday, January 22, 2007

Using global-jndi-lookup-enable to lookup EJBs across applications

OC4J 10.1.3.1 allows lookup of EJBs across application by enabling the global JNDI lookup.

To use the global JNDI feature in OC4J, first you have to enable it.
1. Using a text editor, open config/server.xml.
2. Within the <application-server> element, add the global-jndi-lookup-enabled attribute and set it to true as follows:

<application-server global-jndi-lookup-enabled="true">
</application-server>

3. Save and close server.xml.

4. Restart the application server.

5. Deploy application that contains the EJB. Let us say the application name is APP2 that contains a remote EJB with JNDI location “EmployeeFacadeBean”.

To find the JNDI location look at the generated orion-ejb-jar.xml in the server’s application deployment directory. To configure a different JNDI location package an orion-ejb-jar.xml as follows:

<session-deployment name="EmployeeFacadeBean">
name="EmployeeFacadeBean" location="EmployeeFacadeBean"

</session-deployment >

The default JNDI name is the name of the remote interface of the session bean configured using the ejb-name element in ejb-jar.xml or name element of @Stateless or @Stateful annotations.

The client application that looks up the EJB must package the remote, home, interfaces to look up the remote EJB.

You can lookup the EJB from the client application using the JNDI location as follows:

Context ctx = new InitialContext();
employeeFacade = (EmployeeFacade) ctx.lookup("EmployeeFacadeBean");


Now you deploy your client application and the application should be able to lookup the EJB in APP2!

If you want to use in OC4J 10.1.3.0.0 then make sure that you apply 10.1.3.1 patchset!

Thursday, January 18, 2007

EJB 3 In Action: Code Samples

The code samples of EJB 3 In Action are now available from the Manning web site. We have made the source code available for Java EE SDK (Glassfish) and Oracle Application Server. You can download the code samples from the book site. All chapters of the book are available at the book site through the Manning's MEAP program. So ahead an pre-order a copy for yourself and read the final drafts of the book from the Manning site

Monday, January 08, 2007

Blogger ATOM Woes and RSS URL for blogger !

My transition to Blogger has been smooth except few hiccups. Few weeks back Blogger switched to ATOM 1.0 by default and many blog aggregators that do not support ATOM yet (such as Javablogs.com) stopped taking updates from my Blog. After a lot of research I found that Blogger provides an URL with RSS 2.0 feeds. For example you can get RSS 2.0 feed for my blog at http://debupanda.blogspot.com/feeds/posts/default?alt=rss

Happy New Year!!

Wish you a very happy and prosperous 2007! Now that I’m back from 5 weeks trip to India and back from jet lag. Ready to Blog, Stay Tuned!