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");
while(rs.next()){
System.out.println(rs.getString("FIRST_NAME") +" " + rs.getString("LAST_NAME"));
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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">
</description>
<res-ref-name>jdbc/helloWorldDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
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="http://www.omg.org/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"/>
</resRefBindings>
<ejbRefBindings xmi:id="EjbRefBinding_1251663116984" jndiName="ejb/ejbs/HelloWorldHome">
<bindingEjbRef href="META-INF/ejb-jar.xml#EjbRef_1251663116812"/>
</ejbRefBindings>
</ejbBindings>
</ejbbnd:EJBJarBinding>
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
No comments:
Post a Comment