Data source refernece

In J2EE applications both EJBs and Web applications use Data Sources. As per the J2EE specification you should not reference the Data source directly in your application instead you should create a resource reference use it inside your code and then the administrator can map the resource reference to actual data source at the time of deployment

I wanted to try this part so i changed my HelloWorldEJB to access the DB and execute SQL query. This is how my EJB method looks like

public String sayHello(){
System.out.println("Inside HelloWorldBean.sayHello()");
try {
Context ctx = new InitialContext();
Object obj = ctx.lookup("java:comp/env/jdbc/helloWorldDB");
DataSource ds = (DataSource)obj;
System.out.println("Data Source " + ds);
Connection conn = ds.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM DBADMIN.CUSTOMER");
System.out.println(rs.getString("FIRST_NAME") +" " + rs.getString("LAST_NAME"));

} catch (NamingException e) {
} catch (SQLException e) {
// TODO Auto-generated catch block

return "Hello from HelloWorldEJB";

As you can see the EJB is accessing the Data source from application reference(It starts with java:comp/env)

So i had to define this resource reference, i used RAD define the resource reference by going to References tab in the EJB deployment descriptor editor like this

When i checked the EJB deployment descritpor i could see the resource reference element like this

<resource-ref id="ResourceRef_1252109087246">
<description id="Description_1252114941484">

Now if you know the JNDI name of the data source at the time of application assembly then you can map it to the resource reference in RAD like this

Once you bind the resource reference to data source name you will notice that ibm-ejb-jar-bnd.xmi file like this in the META-INF folder of your ejb jar

<?xml version="1.0" encoding="UTF-8"?>
<ejbbnd:EJBJarBinding xmi:version="2.0" xmlns:xmi="" xmlns:ejb="ejb.xmi" xmlns:ejbbnd="ejbbnd.xmi" xmi:id="EJBJarBinding_1251662932984">
<ejbJar href="META-INF/ejb-jar.xml#ejb-jar_ID"/>
<ejbBindings xmi:id="EnterpriseBeanBinding_1251662932984" jndiName="ejb/ejbs/HelloWorldHome">
<enterpriseBean xmi:type="ejb:Session" href="META-INF/ejb-jar.xml#HelloWorld"/>
<resRefBindings xmi:id="ResourceRefBinding_1252109087246" jndiName="jdbc/helloworlddb">
<bindingResourceRef href="META-INF/ejb-jar.xml#ResourceRef_1252109087246"/>
<ejbRefBindings xmi:id="EjbRefBinding_1251663116984" jndiName="ejb/ejbs/HelloWorldHome">
<bindingEjbRef href="META-INF/ejb-jar.xml#EjbRef_1251663116812"/>

Now deploy your .ear file in the WAS, it wont prompt you to map resource at the time of deployment but after deployment if you want to change the binding you can go to the application and go to resource reference page to change it like this

In my case i do have resource reference in both EJB and Web module for data source so i can see two mappings there.

If you want you can download sample code from here

1 comment:

Shalini Mudhalayar said...

I am a regular reader of your blog and much impressed in your way of written on Java recent updates. I would like to share with my friends. Thank you admin.
spring training in chennai
java training in velachery